MSSQL優化之索引優化

2021-06-05 06:54:11 字數 695 閱讀 8283

1。用count(*)統計比用count(字段)快。用count(主鍵)速度最快!

2。select cid,title,cnt from table where cid > 9999999 or riqi > '2004-9-16'

select cid,title,cnt from table where cid > 9999999

union

select cid,title,cnt from table where riqi > '2004-9-16'

當 or 兩邊不同列名時,union方法比or快;當or兩邊同列名時or方法比union快!

3。order by按聚集索引列排序效率最高

a、select top 99999 title,cnt from table  b、select top 99999 title,cnt from table order by cid  c、select top 99999 title,cnt from table order by riqi

速度:c > a >b

注:為每張表都新增一datetime型別字段,預設為getdate() ,並將其設成唯一索引、建立成聚集的。以後的查詢記錄集都加order by 聚集索引。

要將聚集索引建立在:

1、最頻繁使用的、用以縮小查詢範圍的字段上;

2、最頻繁使用的、需要排序的字段

參考:

效能優化之mysql索引優化

sql及索引優化 如何通過慢查詢日誌發現有問題的sql?查詢次數多且每次查詢占用時間長的sql 通常為pt query digest分析的前幾個查詢 io大的sql 注意pt query digest分析中的rows examine項 未命中索引的sql 注意pt query digest分析中ro...

MySQL優化 之 索引

四種索引 主鍵索引,唯一索引,普通索引,全文索引 對查詢語句會提高效率 對增刪改語句會降低效率,因為還要對索引進行增刪改!建立索引會佔磁碟空間 對頻繁查詢的字段應建立索引,對頻繁更新的字段不適合建立索引 1 新增 1.1 主鍵索引新增 1.當一張表,把某個列設為主鍵的時候,則該列就是主鍵索引 cre...

SQL優化之索引

sql優化有很多方法,今天來說一說資料庫索引。舉例說明 假設有乙個圖書book表,裡面有欄位id,name,isbn等。如果圖書數量巨大的話,我們通過isbn查詢通常是比較慢的。新增索引 create index index isbn on book isbn 查詢時間從0.134縮短到0.001,...