我在SQL Server中有2400萬條記錄的數據庫2000SQL性能問題
當我運行此查詢
select * from cdr
where starttime between '2011-05-15 00:00:00.000' and '2011-05-16 00:00:00.000'
即使這樣
declare @MinDate char(30) ,@MaxDate char(30)
set @MinDate=substring(convert(char,(getdate()-1), 120),1,10)+' 00:00:00.000'
set @MaxDate=substring(convert(char,(getdate()), 120),1,10)+' 00:00:00.000'
select * from cdr
where starttime between '2011-05-15 00:00:00.000' and @MaxDate
它運行速度非常快和回報3500個記錄的冷杉10秒,注意starttime
是數據庫
但是,當我運行此查詢,它只是在10秒〜60秒
declare @MinDate char(30), @MaxDate char(30)
set @MinDate = substring(convert(varchar, (getdate()-1), 120),1,10)+' 00:00:00.000'
set @MaxDate = substring(convert(varchar, (getdate()), 120),1,10)+' 00:00:00.000'
select * from cdr
where starttime between @MinDate and @MaxDate
返回32條記錄:: @MinDate值爲2011-05-15 00:00:00.000
注意starttime
在索引我的數據庫
我想知道什麼是我的問題嗎?
好吧,一個很明顯的問題是,你說你的'StartTime'列是'VARCHAR(30)' - 然而,你的標準,你用'@ MinDate'和'@ MaxDate'被定義爲'CHAR(30) '這意味着它們將被填充到具有空格的限定長度。這既浪費空間,也需要'CHAR'和'VARCHAR'之間的轉換 - 如果你的列的類型是'VARCHAR(30)',我也會使搜索條件的數據類型爲'VARCHAR(30)' – 2011-05-16 09:06:09
第二個明顯的問題:你真的**需要你的'cdr'表中的所有列?如果沒有 - **明確指定您需要的列 - 並且只列出您真正需要的列。這會減少查詢和數據傳輸時間 – 2011-05-16 09:06:55
您是否嘗試過在顯示中明確將MinDate/MaxDate轉換回DateTime? – Runonthespot 2011-05-16 09:07:28