【发布时间】:2015-07-20 11:31:06
【问题描述】:
我有一个包含超过一百万行集数据的数据库。当我执行这个查询需要几个小时,主要是由于 pageIOLatch_sh。目前没有索引。你能在where子句中建议可能的索引吗?我相信它应该在日期时间,因为它在 where 和 order by 中使用,如果是的话,使用哪个索引。
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
-
微软 SQL 服务器 2005
-
如果你没有索引,我猜问题是连接,你没有包含在问题中。
-
首先对这些 :shopping_product 和 shopping_category sub_shopping_list 进行索引,然后您可以尝试日期,然后查看执行计划。 (或者最好在时间列上创建分区)
-
OR 很难优化。尝试将条件拉入连接。可以去掉一个或者用 isnull(@var3, a.ca_id) = a.ca_id
标签: sql-server indexing query-optimization