【发布时间】:2012-11-28 05:35:31
【问题描述】:
我目前正在努力提高 Microsoft SQL Server 2008 R2 的性能。在分析查询时,Microsoft 数据库引擎优化工具会使用此查询创建索引:
CREATE NONCLUSTERED INDEX [samplelocation1] ON [dbo].[sample_location]
(
[sample_id] ASC,
[sample_code] ASC
) WITH (SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF) ON [PRIMARY]
这将使用从备份恢复的数据库在测试服务器(运行相同版本的 SQL Server,2008 R2)上的查询执行时间从 17 秒缩短到 5 秒。
然而,在生产服务器上,执行时间从 17 秒增加到 1 分 40 秒。怎么回事?
查询:
select *
from sample_view
where version_date >= '<date>'
and version_date - 0.9999999 <= '<date>'
and (cus_id in (select company_id
from company_emp_relation_view
where user_id = '<userid>')
or
fac_id in (select company_id
from company_emp_relation_view
where user_id = '<userid>'))
and sample_code in (select min(sample_code)
from sample_location
group by sample_id)
and (rfq_status<>'I')
and location <> 'D'
order by
version_date desc
查询不是我编写的,看起来过于复杂,但我想在不更改任何查询的情况下解决这个问题。我最大的惊喜是索引的效果在不同系统之间是不一样的。
【问题讨论】:
-
嗯,您的测试与生产系统上的数据量和分布可能完全不同 - 因此,您在测试系统上确定一百行可能不在具有数十万行的生产系统上以相同的方式工作.....
-
在 marc 上的位置 - 你不能那样做,除非 test 是最近的生产副本。此外,DTA 很好,但并不总是正确,尤其是如果您聚集的时间太短。
标签: sql-server indexing