[英]How do I time the execution of an SQL Query in C#?
我試圖找出使用c#的SQL Server查詢的執行時間。 我以為計時器會工作; 但是我是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();
}
}
我不確定如何為此添加計時器,該計時器僅對查詢的執行時間甚至放置時間進行計時。 我曾嘗試查找有關此內容的其他帖子,但是我發現的帖子都與此類似。 我的搜索也可能真的很糟糕。 任何幫助將不勝感激,如果您需要更多信息,請告訴我。 為了安全起見,我重命名了一些東西,但是代碼就是這樣的。 注意:我將其用於測試目的,而不是用於生產目的,因此實際上只有少數人會看到此信息,但這是必需的。
您在尋找的是StopWatch
,這是一個示例:
var watch = System.Diagnostics.Stopwatch.StartNew();
// Run your query
watch.Stop();
//This is the time it took in miliseconds
var elapsedTime = watch.ElapsedMilliseconds;
檢查此問題以了解為什么不應該使用DateTime。
我猜想您是否可以在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()
為您工作,那么我絕對會建議您這樣做。 這只是我的代碼解決方案。
SQL查詢從SqlCommand.ExecuteReader()
開始執行,並且在SqlDataReader.Read()
返回false之后完成執行。 請注意,如果SQL Server的網絡速度較慢或有大量結果,則這將無法准確地衡量等待SQL Server所花費的時間。
所以
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查詢中顯示執行特定代碼段的確切時間。 將您想要獲取執行時間的代碼放在以下腳本的中央。 確切的時間將顯示在結果中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.