我有一個包含超過一百萬行集數據的數據庫。當我執行這個查詢時,需要幾個小時,主要是由於pageIOLatch_sh。目前沒有索引。你能建議where子句中可能的索引。我相信它應該在日期時間,因爲它在哪裏使用,以及如何使用哪個索引。基於日期時間字段的Where子句的索引和優化
if(<some condition>)
BEGIN
select <some columns>
From <some tables with joins(no lock)>
WHERE
((@var2 IS NULL AND a.addr IS NOT NULL)OR
(a.addr LIKE @var2 + '%')) AND
((@var3 IS NULL AND a.ca_id IS NOT NULL) OR
(a.ca_id = @var3)) AND
b.time >= @from_datetime AND b.time <= @to_datetime AND
(
(
b.shopping_product IN ('CX12343', 'BG8945', 'GF4543') AND
b.shopping_category IN ('online', 'COD')
)
OR
(
b.shopping_product = 'LX3454' and b.sub_shopping_list in ('FF544','GT544','KK543','LK5343')
)
OR
(
b.shopping_product = 'LK434434' and b.sub_shopping_list in ('LL5435','PO89554','IO948854','OR4334','TH5444')
)
OR
(
b.shopping_product = 'AZ434434' and b.sub_shopping_list in ('LL54352','PO489554','IO9458854','OR34334','TH54344')
)
)AND
ORDER BY
b.time desc
ELSE
BEGIN
select <some columns>
From <some tables with joins(no lock)>
where <similar where as above with slight difference>
請確定您正在使用哪個DBMS。你同時標記了兩個 - mysql和sqlserver – CeOnSql
Microsoft SQL server 2005 – aquesh
如果你沒有建立索引,我猜猜問題是聯接,你沒有包含在問題中。 –