我有了的SQL Server查询性能问题
- 非唯一ID列的表 各种数据
的
我有一个查询,给定一个日期范围,它获取在该日期范围内更新的条目列表以及它刚刚找到的最后一个更新之前的条目列表。通常日期范围在一天之内,因此07-10-2013 00:00:000 - 7-11-2013 00:00:000。
例如。鉴于2013年7月10日00:00:000 - 2013年7月11日00:00:000,查询结果2项
id new data updatedate old data last updatedate
3 randomdata 7-10-2013 03:30:343 randomdata 7-05-2013 06:34:764
4 randomdata 7-10-2013 13:30:343 randomdata 6-09-2013 04:37:376
这是结果,我想获得。目前,我已经有了一个查询,但是查询速度很慢,因为表中有3个内部联接,这些联接有很多条目,我想知道是否有人可以想出一种更快的查询方式。使用SQL Server 2000
表信息
- 非唯一ID
- 唯一的ID(这仅仅是一个自动递增ID)
- 将收集的各种数据
- 更新日期
编辑:
我ndexes目前都在id
和updatedate
查询我目前使用(概括的话)::警告::这不是漂亮::警告::
select *
from
(select distinct
s1.id as id,
s1.randData1, s2.randData1, s1.randData2, s2.randData2,...,
s1.updatedate as newupdatedate,
s2.updatedate as prevupdatedate,
datediff(second, s1.updatedate ,s2.updatedate) as maxdate
from
(select *
from updates
where updatedate >= '{0}' and updatedate < '{1}'
and id in ('{3}')) as s1
inner join
updates s2 on s1.id = s2.id and s1.updateid != s2.updateid) as t1
inner join
(select
s1.id, max(datediff(second, s1.updatedate, s2.updatedate)) as maxdate2
from updates s1
inner join updates s2 on s1.id in ('{3}') and s1.id = s2.id and s1.updateid != s2.updateid
where datediff(second, s1.updatedate, s2.updatedate) < 0
and s1.updatedate < '{1}' and s2.updatedate < '{1}'
group by
s1.id) as t2 on t1.id = t2.id and t1.maxdate = t2.maxdate2
{0 } {1}和{2}是传入的参数。
编辑:如果有什么区别,查询正在C#中执行。如有必要,2个查询也会很好。我最终寻找的是在所选日期的数据中发生了什么变化。
你能发布查询,并告诉我们在什么地方指标? –