我有一個SQL查詢需要大約1分鐘才能獲得75,000條記錄。SQL查詢1分鐘可獲得75,000條記錄
我想知道是否有辦法通過改變任何東西來使其更快。
SELECT c.CampaignID,
c.Description,
c.Active,
c.CampaignTypeID,
CASE c.CampaignTypeID
WHEN 1 THEN 'TEST1'
WHEN 2 THEN 'TEST2'
WHEN 3 THEN 'TEST3'
WHEN 5 THEN 'TEST4'
WHEN 6 THEN 'TEST5'
END AS CampaignType,
CASE c.Active
WHEN 1 THEN 'TEST6'
ELSE 'TEST7'
END AS style,
CASE c.Active
WHEN 1 THEN 'Deactivate'
ELSE 'Activate'
END AS stat,
REPLACE(REPLACE(c.Description,'\\','\\\\'),'''','\\') AS LinkDescription,
@CompanyID AS CompanyID
FROM Campaign c WITH(nolock)
LEFT OUTER JOIN UserPermissions
ON up.CampaignID=c.CampaignID
AND up.UserID = @UserID
WHERE c.CompanyID = @CompanyID
and c.IsCampaignHidden = 0
AND (@IsAdmin = 0
and case when (@IsAdmin = 0) then up.UserID else 0 end is not null
and case when (@IsAdmin = 0) then up.[View] else 1 end = 1
感謝任何幫助。
您是否查看查詢分析器的結果? – Namphibian
您有[執行計劃](http://www.sqlshack.com/sql-server-query-execution-plans-viewing-plans/)嗎? – DaveShaw
基於你過濾到左邊的連接表,我假設你不需要左邊的部分。我會嘗試刪除它。任何可能添加索引到CompanyID,CampaignID,UserId字段。 –