如何在 C# 中为 SQL 查询的执行计时?
How do I time the execution of an SQL Query in C#?
我正在尝试使用 c# 找出 SQL 服务器查询的执行时间。我认为计时器会起作用;但是我是 c# 的新手,一直无法弄清楚。我在这个站点和其他站点上搜索了几个问题,试图找出如何为查询的执行计时。
这是我的代码:
using System;
using System.Data.SqlClient;
class Program
{
static void Main()
{
Console.WriteLine("Executing query...");
string customerID = "ID_HERE";
using (SqlConnection connection = new SqlConnection("CONNECTION_STRING"))
{
connection.Open();
using (SqlCommand command = new SqlCommand(
"SELECT col0, col1, col2, col3, col4, col5, col6, col7, col8, col9 FROM Table_name WHERE col1 LIKE @ID", connection))
{
command.Parameters.Add(new SqlParameter("ID", customerID));
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
int col0 = reader.GetInt32(0);
int col1 = reader.GetInt32(1);
string col2 = reader.GetString(2);
string col3 = reader.GetString(3);
int col4 = reader.GetInt32(4);
int col5 = reader.GetInt32(5);
short col6 = reader.GetInt16(6);
string col7 = reader.GetString(7);
string col8 = reader.GetString(8);
int col9 = reader.GetInt32(9);
Console.WriteLine("col0 = {0}, col1 = {1}, col2 = {2}, col3 = {3}, col4 = {4}, col5 = {5}, col6 = {6}, col7 = {7}, col8 = {8}, col9 = {9}",
col0,
col1,
col2,
col3,
col4,
col5,
col6,
col7,
col8,
col9
);
}
}
}
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
}
}
我不确定如何为此添加一个计时器,它只对查询的执行进行计时,甚至不知道将它放在哪里。我试过寻找其他关于它的帖子,但我发现的 none 与此类似;我也可能真的不擅长搜索。任何形式的帮助将不胜感激,如果您需要更多信息,请告诉我。为了安全起见,我重命名了一些东西,但这是代码的其他方式。注意:我将它用于测试目的,而不是用于生产,所以只有少数人会真正看到它,但这是必要的。
我想如果您在 SQL 执行之前和之后获得服务器时间 (DateTime.Now) 并找到可以让您知道经过时间的差异。
我也想完成同样的事情。我见过 StopWatch()
但一直无法让它发挥作用。所以我自己组装了:
TimeSpan ts = DateTime.Now.TimeOfDay;
Debug.Print("\n\nProcess Started---- " + processName + " ---- " + ts + "\n\n");
/*
* code here
*/
TimeSpan fts = DateTime.Now.TimeOfDay;
Debug.Print("\n\nProcess Ended---- " + processName + " ---- " + fts + "\n");
Debug.Print("Time Elapsed---- " + (fts - ts) + " \n\n");
它可能不是最快或最干净的。但它告诉我我想知道的。您还需要 using System.Diagnostics;
语句才能打印到 debug window
.
注意:如果您能让 StopWatch()
为您工作,那么我绝对会推荐它。这只是我的代码解决方案。
你要找的是StopWatch
,这是一个例子:
var watch = System.Diagnostics.Stopwatch.StartNew();
// Run your query
watch.Stop();
//This is the time it took in miliseconds
var elapsedTime = watch.ElapsedMilliseconds;
检查 this question 为什么你不应该使用 DateTime。
SQL 查询从 SqlCommand.ExecuteReader()
开始执行,在 SqlDataReader.Read()
returns false 后结束执行。请注意,如果 SQL 服务器的网络速度较慢或有大量结果,这将无法准确测量在 SQL 服务器上等待的时间。
所以
using System;
using System.Data.SqlClient;
using System.Diagnostics;
class Program
{
static void Main()
{
Console.WriteLine("Executing query...");
string customerID = "ID_HERE";
using (SqlConnection connection = new SqlConnection("CONNECTION_STRING"))
{
connection.Open();
using (SqlCommand command = new SqlCommand(
"SELECT col0, col1, col2, col3, col4, col5, col6, col7, col8, col9 FROM Table_name WHERE col1 LIKE @ID", connection))
{
command.Parameters.Add(new SqlParameter("ID", customerID));
var sw = new Stopwatch();
sw.Start();
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
int col0 = reader.GetInt32(0);
int col1 = reader.GetInt32(1);
string col2 = reader.GetString(2);
string col3 = reader.GetString(3);
int col4 = reader.GetInt32(4);
int col5 = reader.GetInt32(5);
short col6 = reader.GetInt16(6);
string col7 = reader.GetString(7);
string col8 = reader.GetString(8);
int col9 = reader.GetInt32(9);
Console.WriteLine("col0 = {0}, col1 = {1}, col2 = {2}, col3 = {3}, col4 = {4}, col5 = {5}, col6 = {6}, col7 = {7}, col8 = {8}, col9 = {9}",
col0,
col1,
col2,
col3,
col4,
col5,
col6,
col7,
col8,
col9
);
}
var elapsed = sw.Elapsed;
Console.WriteLine($"Query Executed and Results Returned in {elapsed.Seconds}sec");
}
}
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
}
}
DECLARE @Time1 DATETIME
DECLARE @Time2 DATETIME
SET @Time1 = GETDATE()
-- Insert query here
SET @Time2 = GETDATE()
SELECT DATE DIFF(MILLISECOND,@Time1,@Time2) AS Elapsed_MS
此代码允许您在 sql 查询中显示特定代码段的准确执行时间。将您想要获取执行时间的代码放在下面脚本的中心。具体时间会显示在结果中。
我正在尝试使用 c# 找出 SQL 服务器查询的执行时间。我认为计时器会起作用;但是我是 c# 的新手,一直无法弄清楚。我在这个站点和其他站点上搜索了几个问题,试图找出如何为查询的执行计时。
这是我的代码:
using System;
using System.Data.SqlClient;
class Program
{
static void Main()
{
Console.WriteLine("Executing query...");
string customerID = "ID_HERE";
using (SqlConnection connection = new SqlConnection("CONNECTION_STRING"))
{
connection.Open();
using (SqlCommand command = new SqlCommand(
"SELECT col0, col1, col2, col3, col4, col5, col6, col7, col8, col9 FROM Table_name WHERE col1 LIKE @ID", connection))
{
command.Parameters.Add(new SqlParameter("ID", customerID));
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
int col0 = reader.GetInt32(0);
int col1 = reader.GetInt32(1);
string col2 = reader.GetString(2);
string col3 = reader.GetString(3);
int col4 = reader.GetInt32(4);
int col5 = reader.GetInt32(5);
short col6 = reader.GetInt16(6);
string col7 = reader.GetString(7);
string col8 = reader.GetString(8);
int col9 = reader.GetInt32(9);
Console.WriteLine("col0 = {0}, col1 = {1}, col2 = {2}, col3 = {3}, col4 = {4}, col5 = {5}, col6 = {6}, col7 = {7}, col8 = {8}, col9 = {9}",
col0,
col1,
col2,
col3,
col4,
col5,
col6,
col7,
col8,
col9
);
}
}
}
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
}
}
我不确定如何为此添加一个计时器,它只对查询的执行进行计时,甚至不知道将它放在哪里。我试过寻找其他关于它的帖子,但我发现的 none 与此类似;我也可能真的不擅长搜索。任何形式的帮助将不胜感激,如果您需要更多信息,请告诉我。为了安全起见,我重命名了一些东西,但这是代码的其他方式。注意:我将它用于测试目的,而不是用于生产,所以只有少数人会真正看到它,但这是必要的。
我想如果您在 SQL 执行之前和之后获得服务器时间 (DateTime.Now) 并找到可以让您知道经过时间的差异。
我也想完成同样的事情。我见过 StopWatch()
但一直无法让它发挥作用。所以我自己组装了:
TimeSpan ts = DateTime.Now.TimeOfDay;
Debug.Print("\n\nProcess Started---- " + processName + " ---- " + ts + "\n\n");
/*
* code here
*/
TimeSpan fts = DateTime.Now.TimeOfDay;
Debug.Print("\n\nProcess Ended---- " + processName + " ---- " + fts + "\n");
Debug.Print("Time Elapsed---- " + (fts - ts) + " \n\n");
它可能不是最快或最干净的。但它告诉我我想知道的。您还需要 using System.Diagnostics;
语句才能打印到 debug window
.
注意:如果您能让 StopWatch()
为您工作,那么我绝对会推荐它。这只是我的代码解决方案。
你要找的是StopWatch
,这是一个例子:
var watch = System.Diagnostics.Stopwatch.StartNew();
// Run your query
watch.Stop();
//This is the time it took in miliseconds
var elapsedTime = watch.ElapsedMilliseconds;
检查 this question 为什么你不应该使用 DateTime。
SQL 查询从 SqlCommand.ExecuteReader()
开始执行,在 SqlDataReader.Read()
returns false 后结束执行。请注意,如果 SQL 服务器的网络速度较慢或有大量结果,这将无法准确测量在 SQL 服务器上等待的时间。
所以
using System;
using System.Data.SqlClient;
using System.Diagnostics;
class Program
{
static void Main()
{
Console.WriteLine("Executing query...");
string customerID = "ID_HERE";
using (SqlConnection connection = new SqlConnection("CONNECTION_STRING"))
{
connection.Open();
using (SqlCommand command = new SqlCommand(
"SELECT col0, col1, col2, col3, col4, col5, col6, col7, col8, col9 FROM Table_name WHERE col1 LIKE @ID", connection))
{
command.Parameters.Add(new SqlParameter("ID", customerID));
var sw = new Stopwatch();
sw.Start();
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
int col0 = reader.GetInt32(0);
int col1 = reader.GetInt32(1);
string col2 = reader.GetString(2);
string col3 = reader.GetString(3);
int col4 = reader.GetInt32(4);
int col5 = reader.GetInt32(5);
short col6 = reader.GetInt16(6);
string col7 = reader.GetString(7);
string col8 = reader.GetString(8);
int col9 = reader.GetInt32(9);
Console.WriteLine("col0 = {0}, col1 = {1}, col2 = {2}, col3 = {3}, col4 = {4}, col5 = {5}, col6 = {6}, col7 = {7}, col8 = {8}, col9 = {9}",
col0,
col1,
col2,
col3,
col4,
col5,
col6,
col7,
col8,
col9
);
}
var elapsed = sw.Elapsed;
Console.WriteLine($"Query Executed and Results Returned in {elapsed.Seconds}sec");
}
}
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
}
}
DECLARE @Time1 DATETIME
DECLARE @Time2 DATETIME
SET @Time1 = GETDATE()
-- Insert query here
SET @Time2 = GETDATE()
SELECT DATE DIFF(MILLISECOND,@Time1,@Time2) AS Elapsed_MS
此代码允许您在 sql 查询中显示特定代码段的准确执行时间。将您想要获取执行时间的代码放在下面脚本的中心。具体时间会显示在结果中。