效能優化 找到SQL SERVER中的書籤查詢

2021-08-13 12:11:27 字數 2081 閱讀 9135

我們在建立索引的時候,對於調節篩選列是大家都能夠注意到的。但是對於包含列檢查會被忽略。從而導致大量的lookup ,也就是書籤查詢。

那麼我如何才能找出某個表是不是執行了書籤查詢,執行了多少次書籤查詢呢?

好訊息是,sql server 有各種各樣的動態管理檢視,其中 sys.dm_db_index_operational_stats 檢視就能查詢到我們需要的資訊。 這個檢視的singleton_lookup_count列 就會返回執行了多少個查詢操作。

先建立乙個新錶,並在單個列上建立非聚集索引。

create table customers

(customerid int not null primary key clustered,

customername char(100) not null,

customeraddress char(100) not null,

comments char(185) not null,

value int not null)go

create nonclustered index idx_test on customers(value)

go

在表中插入80000行:

declare @i int = 1

while (@i <= 80000)

begin

insert into customers values

(@i,

'customername' + cast(@i as char),

'customeraddress' + cast(@i as char),

'comments' + cast(@i as char),@i)

set @i += 1

endgo

我們執行乙個簡單的查詢,並使用查詢提示 強制使用索引

select * from customers with (index(idx_test))

where value < 80001

go如下圖可以看到,語句對錶customer 執行了look up 書籤查詢

如下圖所示,key lookup執行了8000次

當您現在查詢動態管理函式sys.dm_db_index_operational_stats時,sql server會精確返回您執行了多少次查詢操作

select singleton_lookup_count, *

from sys.dm_db_index_operational_stats(db_id(), object_id('customers'), 1, null)

go如下圖所示,使用查詢得到的lookup 次數和實際的次數完全一致。

書籤查詢look up 操作一般來說並沒有什麼問題。但是如果書籤查詢的次數非常多,就會對效能產生影響,導致查詢執行緩慢。 在對資料庫進行整體效能調優時,我們可以批量查詢出來。使用下面的sql 可以吧資料庫中所有的lookup全部查詢出來。

SQL Server效能優化

一 分析階段 一般來說,在系統分析階段往往有太多需要關注的地方,系統各種功能性 可用性 可靠性 安全性需求往往吸引了我們大部分的注意力,但是,我們必須注意,效能是很重要的非功能性需求,必須根據系統的特點確定其實時性需求 響應時間的需求 硬體的配置等。最好能有各種需求的量化的指標。另一方面,在分析階段...

SQL Server 效能優化

伺服器效能優化 1.建立效能基線 2.建立監視 3.分析監視結果 1.windows工具 perfmon,tskmgr,eventmon,netmon 2.sql server 工具 事件探查器 查詢優化顧問 sql 管理器 log執行計畫 活動監視 各種report tsql 效能優化 1.目標 ...

SqlServer效能優化(一)

一 資料儲存的方式 1.資料檔案 mdf或.ndf 2.日誌檔案 ldf 二 事務日誌的工作步驟 1.資料修改由應用程式發出 在緩衝區進行快取 2.資料頁位於快取區緩衝中,或者讀入緩衝區快取然後修改 3.修改記錄在磁碟上的事務日誌中 4.檢查點將提交的事物寫入資料庫中 三 手工效能收集項 系統要收集...