I want to check if a for that loop between a DataTable be faster or do this operation in SQL Server. So, I create a table like this:
CREATE TABLE [dbo].[tbl_Test_Data](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](50) NOT NULL,
[LName] [nvarchar](50) NOT NULL,
[f_date] [datetime] NOT NULL,
[flag] [bit] NOT NULL,
[Age] [int] NOT NULL,
[amount] [decimal](23, 5) NOT NULL,
CONSTRAINT [PK_tbl_Test_Data] PRIMARY KEY CLUSTERED ([Id] ASC)
and then I create an index on the Age column:
CREATE NONCLUSTERED INDEX [idx_age] ON [dbo].[tbl_Test_Data]
(
[Age] ASC
)
INCLUDE ( [Id], [f_date])
and insert 100000 rows of test data:
DECLARE @a INT
SET @a = 100000;
WHILE @a >= 0
BEGIN
DECLARE @d DATETIME ;
SET @d = DATEADD(d,CAST(RAND() * 100 AS INT),GETDATE());
DECLARE @b BIT;
IF @a % 2 =0
SET @b=0;
ELSE
SET @b=1;
INSERT INTO tbl_Test_data
VALUES(N'nima',
N'agha',
DATEADD(d,CAST(RAND() * 100 AS INT),GETDATE()),
@b,
CAST(RAND() * 100 AS INT),
CAST(RAND() AS DECIMAL(23,5)) )
SET @a=@a -1;
END
Then I create a C# windows application with 2 buttons. For update from sql query I write this code:
private void button2_Click(object sender, EventArgs e)
{
Stopwatch sq = new Stopwatch();
sq.Start();
using (SqlConnection cn = new SqlConnection("Data Source=.;Initial Catalog=Northwind;Integrated Security=True"))
{
using (SqlCommand cmd = new SqlCommand())
{
string txt = "UPDATE tbl_Test_Data SET f_date=getdate() WHERE Age>80;";
txt += "SELECT Id,Name,LName,f_date,flag,Age,amount FROM [Northwind].[dbo].[tbl_Test_Data]";
cmd.CommandText = txt;
cmd.CommandType = CommandType.Text;
cmd.Connection = cn;
using (SqlDataAdapter da = new SqlDataAdapter())
{
da.SelectCommand = cmd;
DataTable dt = new DataTable();
cn.Open();
da.Fill(dt);
cn.Close();
dataGridView2.DataSource = dt;
}
}
}
sq.Stop();
label2.Text = sq.Elapsed.TotalMilliseconds.ToString();
}
and for using for loop I write this:
Stopwatch sq = new Stopwatch();
sq.Start();
using (SqlConnection cn = new SqlConnection("Data Source=.;Initial Catalog=Northwind;Integrated Security=True"))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "SELECT Id,Name,LName,f_date,flag,Age,amount FROM [Northwind].[dbo].[tbl_Test_Data]";
cmd.CommandType = CommandType.Text;
cmd.Connection = cn;
using (SqlDataAdapter da = new SqlDataAdapter())
{
da.SelectCommand = cmd;
DataTable dt = new DataTable();
cn.Open();
da.Fill(dt);
cn.Close();
for (int i = 0; i < dt.Rows.Count; i++)
{
if (int.Parse(dt.Rows[i]["Age"].ToString()) > 80)
{
dt.Rows[i]["f_date"] = DateTime.Now;
}
else
{
dt.Rows[i]["f_date"] = DateTime.Now.AddDays(-100);
}
}
dataGridView1.DataSource = dt;
}
}
}
sq.Stop();
label1.Text = sq.Elapsed.TotalMilliseconds.ToString();
but I see surprisingly that when I use for loop it takes shorter time than SQL Server. About(50%). I test it several times.
Why using for take shorter time that using an update?
The
UPDATEactually has to deal with locks and disk access at the server level, and it also has to update your indexes on the base table.The
forloop doesn’t have to deal with any of this overhead. However, changes made in theforloop are not visible to anything else besides your application.The
UPDATEwould make the change for all other queries to that table. Theforloop changes are only visible within the application scope with the data table.As an aside, it’s also possible that if you ran these queries in the order they are in posted, that the first query paid the IO cost to retrieve the pages from disk, and the second run was able to pull the cached pages from memory.