如何在查询非常大的数据库表时获得快速结果?

时间:2011-03-07 05:19:37

标签: sql-server-2005

我的客户端有一个非常大的数据库。包含患者记录的两个表格超过10 million rows。当我为特定患者选择患者记录时,我会在2-3秒内获得记录。但是如果我在between date条件下通过where条款,我就不会在25-30分钟之前得到记录。我的查询 -

select convert(varchar(12),[DateTime],101) as [DateTime] , min(cast(Response as int)) as     [MinFallTotal], max(cast(Response as int)) as [MaxFallTotal]
from NurQueryResults
where VisitID = 'W3074332666' and QueryID = 'NURFALLZ'
Group by convert(varchar(12),[DateTime],101)

我在2-3秒内得到上层查询的结果,但不是为此 -

select VisitID, min(cast(Response as int)) as [MinFallTotal], max(cast(Response as int)) as [MaxFallTotal]
from NurQueryResults
where QueryID = 'NURFALLZ' and convert(varchar(12),[DateTime],101)='12/23/2010'
Group by convert(varchar(12),[DateTime],101), VisitID

实际要求是 -

select TOP 10 AV.VisitID as [UNIT], AV.AccountNumber as [ACCOUNT], AV.Name as [PATIENT NAME], convert(varchar(11),AV.BirthDateTime,101) as [DoB]
, convert(varchar(12),NQ.[DateTime],101) as [DateTime],
(cast(min(cast(NQ.Response as int)) as varchar(5))+ ' - ' + cast(max(cast(NQ.Response     as int)) as varchar(5)) ) as [FALL]
from AdmVisits AV join NurQueryResults NQ ON
AV.VisitID = NQ.VisitID
where   NQ.QueryID = 'NURFALLZ' AND convert(varchar(12),NQ.[DateTime],101) = '12/24/2010'
Group by convert(varchar(12),NQ.[DateTime],101), AV.VisitID, AV.AccountNumber, AV.Name, convert(varchar(11),AV.BirthDateTime,101)

任何人都可以告诉我为什么要花这么多时间执行以及解决方案是什么?

1 个答案:

答案 0 :(得分:2)

您确实需要检查性能不佳的代码的查询计划。将查询粘贴到窗口中并按CTRL + L.这应该告诉你它不喜欢什么。

如果您使用连接到SQL2005数据库的SSMS 2008 IDE并运行查询计划,我还应该为您提供有助于查询的任何缺失索引的建议。

根据所提供的有限信息,在不查看所涉及的表的结构,索引等的情况下,在大多数情况下,您的查询之间应该在不到一秒的时间内运行。