SQL語句的效能查詢

2021-08-10 04:49:46 字數 1245 閱讀 6705

本文**:

對於做管理系統和分析系統的程式設計師,複雜sql語句是不可避免的,面對海量資料,有時候經過優化的某一條語句,可以提高執行效率和整體執行效能。

如何選擇sql語句,本文提供了兩種方法,分別對多條sql進行量化的分析。

在測試sql效能的過程中。

一是通過設定statistics檢視執行sql時的系統情況。

選項有profile,io ,time。 set statistics profile on:顯示分析、編譯和執行查詢所需的時間(以毫秒為單位)。

set statistics io on:報告與語句內引用的每個表的掃瞄數、邏輯讀取數(在快取記憶體中訪問的頁數)和物理讀取數(訪問磁碟的次數)有關的資訊。

set statistics time on:顯示每個查詢執行後的結果集,代表查詢執行的配置檔案。

例項如下:

set statistics profile on

set statistics io on

set statistics time on

go

–你的sql指令碼開始

select [testcase] from [testcaseselect]

–你的sql指令碼結束

go

set statistics profile off

set statistics io off

set statistics time off

另外,也可以通過手工新增語句,計算執行時間來檢視執行語句花費了的時間,以判斷該條sql語句的效率如何:

declare @d datetime set @d=getdate()

–你的sql指令碼開始

select [testcase] from [testcaseselect]

–你的sql指令碼結束

select [語句執行花費時間(毫秒)]=datediff(ms,@d,getdate())

比較SQL查詢效能 語句

比較sql查詢效能 語句 優先使用set statistics io和set statistics time檢視效能調節是否有效。set statistics time on set statistics time on 在開始我們的例子前,先執行下面的這二條命令 不要在正在使用的伺服器上執行 這二...

如何查詢效能最差的SQL語句

總耗時最長 select top 10 total worker time 1000 as n 總消耗cpu 時間 ms execution count n 執行次數 qs.total worker time qs.execution count 1000 as n 平均消耗cpu 時間 ms la...

如何查詢效能最差的SQL語句

總耗時最長 select top 10 total worker time 1000 as n 總消耗cpu 時間 ms execution count n 執行次數 qs.total worker time qs.execution count 1000 as n 平均消耗cpu 時間 ms la...