SQL server 查詢效能的問題

2021-06-16 10:38:48 字數 3218 閱讀 3848

use [spoofspam2]

go/****** object: storedprocedure [dbo].[getmessagelist] script date: 04/09/2013 18:00:34 ******/

set ansi_nulls on

goset quoted_identifier on

gocreate procedure [dbo].[getmessagelist]

@status_code varchar(20),

@kana_instance int = -1,

@message_type nvarchar(64),

@numrows int,

@datelimit int

as/* @@decommissioned due to performance issue on

d.status_code in (select c1 from func_splitstring(@status_code, ','))

--set transaction isolation level read uncommitted

select top (@numrows) d.message_detail_id, d.message_id, d.subject,

d.email_from, d.email_to, d.status_code, isnull(d.action_failed_times,0) action_failed_times,

d.score, c.body, c.headers, c.status, c.source, d.user_from_status,

d.site_id,t.action_id,ad.type,ad.value, ad.exclude_reply

from message_detail d with(readpast)

inner join message_clob c with(readpast) on d.message_detail_id= c.message_detail_id

left join message_tracking t with(readpast) on t.message_detail_id= c.message_detail_id

left join action_detail ad on t.action_id= ad.id

where

d.status_code in (select c1 from func_splitstring(@status_code, ','))

and (@kana_instance = -1 or d.kana_instance = @kana_instance )

and d.message_type = @message_type

and d.date_entered > dateadd(day,-@datelimit ,getdate())

and c.date_entered > dateadd(day,-@datelimit ,getdate())

--and d.email_to = '[email protected]' --test only, to be removed later; in kana104_dev is '[email protected]'; in stkana5_macro is '[email protected]'

order by d.message_detail_id asc

*/ declare @sql nvarchar(max)

set @sql='

select top (' + convert(varchar(10),@numrows) + ') d.message_detail_id, d.message_id, d.subject,

d.email_from, d.email_to, d.status_code, isnull(d.action_failed_times,0) action_failed_times,

d.score, c.body, c.headers, c.status, c.source, d.user_from_status,

d.site_id,t.action_id,ad.type,ad.value, ad.exclude_reply

from message_detail d with(readpast)

inner join message_clob c with(readpast) on d.message_detail_id= c.message_detail_id

left join message_tracking t with(readpast) on t.message_detail_id= c.message_detail_id

left join action_detail ad on t.action_id= ad.id

where

d.status_code in (' + @status_code + ')

and d.message_type = ''' + @message_type + '''

and (' + convert(varchar(10),@kana_instance) + ' = -1 or d.kana_instance = ' + convert(varchar(10),@kana_instance) + ' )

and d.date_entered > dateadd(day,-' + convert(varchar(10),@datelimit) + ' ,getdate())

and c.date_entered > dateadd(day,-' + convert(varchar(10),@datelimit) + ' ,getdate())

order by d.message_detail_id asc

' exec(@sql)

return

go

以上**中的條件

d.status_code in (select c1 from func_splitstring(@status_code, ','))

會影響執行效能。不知為何,d.status_code的索引不起作用,導致資料返回很慢!貌似用函式的條件,sql server不會應用索引,哪怕是建了索引。

於是,改了另一種做法:

d.status_code in (' + @status_code + ')

效能問題馬上解決!執行計畫馬上正確使用了d.status_code的索引。

SQL SERVER效能優化 查詢速度提高

近段時間去面試,很多都會問到關於sql語句優化及大資料量資料查詢速度提公升的問題,但是由於我個人之前開發都是一知半解的狀態,很多東西都沒能掌握明白,感覺自己就是菜鳥一枚,暈死,和朋友閒聊聊到查詢效率的提公升可以通過建立聚集索引分割槽。所以,就好好了解下這其中的原理。一.索引的概念 資料庫索引,是資料...

SQLServer效能優化之查詢提示

資料庫於週日被重啟了,剛好看看優化後的效果,順便再找找新的需要優化的sql 剛好找到了類似的幾條語句,如下 select from tablea where id not in select id from tableb 從執行時間20秒 70秒不等。開始分析 首先是否用上索引,兩個id均是主鍵所以...

SQL Server 查詢效能優化 相關文章

來自 sql server 查詢效能優化 堆表 碎片與索引 一 sql server 查詢效能優化 堆表 碎片與索引 二 sql server 查詢效能優化 覆蓋索引 一 sql server 查詢效能優化 覆蓋索引 二 sql server 查詢效能優化 建立索引原則 一 sql server 查...