SQL索引優化方法

2021-10-08 14:33:59 字數 1594 閱讀 5396

sql索引優化方法

以下是**片段:

select top 50

row_number() over(order by resumecreatetime desc) as [rowid]

,[topdegree]

,[degreerankid]

,[userresume].[userresumeid]

,[userresume].[userid]

,[resumename]

,[birthdate]

,[workstarteddate]

,[salaryneeded]

,[buffertimespanid]

,[resumecreatetime]

from [dbo].[userresume] inner join [dbo].[workexperience] on [workexperience].[userresumeid] = [userresume].[userresumeid]

where

(contains([workexperience].[worksummary],"經理") or contains([userresume].[resumename],"簡歷"))

第一次執行:沒有建立任何索引。

執行效果:

表 "worktable"。掃瞄計數 0,邏輯讀取 0 次

表 "userresume"。掃瞄計數 1,邏輯讀取 18524 次

表 "workexperience"。掃瞄計數 1,邏輯讀取 8679 次

(1 行受影響)

cpu 時間 = 2152 毫秒,占用時間 = 3126 毫秒。

第二次執行:

檢視執行計畫workexperience表是表掃瞄,建立ix_workexperience索引(在id列和join 參照的id列上)。

調整後效果:

表 "workexperience"。掃瞄計數 1,邏輯讀取 1071 次

表 "userresume"。掃瞄計數 1,邏輯讀取 18524 次

(1 行受影響)

cpu 時間 = 1638 毫秒,占用時間 = 2045 毫秒。

第三次執行:

檢視執行計畫,userresume表是表掃瞄,建立ix_userresume索引。

調整後效果:

表 "workexperience"。掃瞄計數 11,邏輯讀取 48 次

表 "userresume"。掃瞄計數 1,邏輯讀取 3095 次

(1 行受影響)

cpu 時間 = 1248 毫秒,占用時間 = 1568 毫秒。

第四次執行:

檢視執行計畫,在ix_userresume索引掃瞄後,產生了createtime列的重新排序,調整了ix_userresume索引中createtime的位置和排序規則。

調整後效果:

(50 行受影響)

表 "workexperience"。掃瞄計數 11,邏輯讀取 48 次

表 "userresume"。掃瞄計數 1,邏輯讀取 3 次

(1 行受影響)

cpu 時間 = 15 毫秒,占用時間 = 404 毫秒。

效能優化 索引優化SQL的方法

增加索引會有利於查詢效率,但會降低insert,update,delete的效率,但實際上往往不是這樣的,過多的索引會不但會影響使用效率,同時會影響查詢效率,這是由於資料庫進行查詢分析時,首先要選擇使用哪乙個索引進行查詢,如果索引過多,分析過程就會越慢,這樣同樣的減少查詢的效率,因此我們要知道如何增...

SQL優化 索引優化

一 發現哪些sql語句有效能問題 開啟mysql慢查詢日誌對sql語句進行監控 show variables like slow query log 檢視是否開啟慢查詢日誌 set global slow query log on 開啟慢查詢日誌 set global log queries not...

SQL優化(SQL 索引)

檢視表定義 show create table users 檢視表的索引 show index from users 你要獲取第乙個表的所有資訊,你說全表掃瞄快呢還是索引掃瞄快呢?所以當你查詢庫 包括left join中的臨時庫 的所有資訊時,資料庫會選擇最優方法 全表掃瞄!s表dept id na...