27,579
社区成员
发帖
与我相关
我的任务
分享
/****** Script for SelectTopNRows command from SSMS ******/
SELECT [CardNo]
,[CardID]
,[CardSave]
,[TollAmount]
,[ExTime]
,[ExRoad]
,[ExStation]
,[ExShiftDate]
,[EnTime]
,[EnRoad]
,[EnStation]
,[ExVehPlate]
,[VerifyCode]
,[DealStatus]
,[SendTime]
FROM [QTKCenter].[dbo].[SpendHistory]
where [CardNo] ='6212262409000028515' and [ExTime] between '2013-3-1' and '2013-3-31'
create index idx_SpendHistory_cc on SpendHistory(CardNo,ExTime)
然后,在运行下面的语句,看看:
select [CardNo],[CardID],[CardSave],[TollAmount],[ExTime],[ExRoad],[ExStation]
,[ExShiftDate],[EnTime],[EnRoad],[EnStation],[ExVehPlate],[VerifyCode]
,[DealStatus],[SendTime]
from
(
select *,
row_number() over(partition by Extime,ExStantion
order by sendTime desc) rownum
from
(
SELECT [CardNo]
,[CardID]
,[CardSave]
,[TollAmount]
,[ExTime]
,[ExRoad]
,[ExStation]
,[ExShiftDate]
,[EnTime]
,[EnRoad]
,[EnStation]
,[ExVehPlate]
,[VerifyCode]
,[DealStatus]
,[SendTime]
FROM [QTKCenter].[dbo].[SpendHistory]
where [CardNo] ='6212262409000028515' and
[ExTime] between '2013-3-1' and '2013-3-31'
) t
)t
where rownum=1
[/quote]
嗯,明天我试试[/quote]
方法2,
SELECT [CardNo]
,[CardID]
,[CardSave]
,[TollAmount]
,[ExTime]
,[ExRoad]
,[ExStation]
,[ExShiftDate]
,[EnTime]
,[EnRoad]
,[EnStation]
,[ExVehPlate]
,[VerifyCode]
,[DealStatus]
,[SendTime]
FROM [QTKCenter].[dbo].[SpendHistory] a
where a.[CardNo]='6212262409000028515' and a.[ExTime] between '2013-3-1' and '2013-3-31'
and not exists
(select 1 from [QTKCenter].[dbo].[SpendHistory] b
where b.[CardNo]='6212262409000028515' and b.[ExTime] between '2013-3-1' and '2013-3-31'
and b.[Extime]=a.[Extime] and b.[ExStantion]=a.[ExStantion] and b.[SendTime]>a.[SendTime])
[/quote]
如果where后面有多个条件,是不是说有索引或者是主键的放前面,速度会快一点?create index idx_SpendHistory_cc on SpendHistory(CardNo,ExTime)
然后,在运行下面的语句,看看:
select [CardNo],[CardID],[CardSave],[TollAmount],[ExTime],[ExRoad],[ExStation]
,[ExShiftDate],[EnTime],[EnRoad],[EnStation],[ExVehPlate],[VerifyCode]
,[DealStatus],[SendTime]
from
(
select *,
row_number() over(partition by Extime,ExStantion
order by sendTime desc) rownum
from
(
SELECT [CardNo]
,[CardID]
,[CardSave]
,[TollAmount]
,[ExTime]
,[ExRoad]
,[ExStation]
,[ExShiftDate]
,[EnTime]
,[EnRoad]
,[EnStation]
,[ExVehPlate]
,[VerifyCode]
,[DealStatus]
,[SendTime]
FROM [QTKCenter].[dbo].[SpendHistory]
where [CardNo] ='6212262409000028515' and
[ExTime] between '2013-3-1' and '2013-3-31'
) t
)t
where rownum=1
[/quote]
嗯,明天我试试create index idx_SpendHistory_cc on SpendHistory(CardNo,ExTime)
然后,在运行下面的语句,看看:
select [CardNo],[CardID],[CardSave],[TollAmount],[ExTime],[ExRoad],[ExStation]
,[ExShiftDate],[EnTime],[EnRoad],[EnStation],[ExVehPlate],[VerifyCode]
,[DealStatus],[SendTime]
from
(
select *,
row_number() over(partition by Extime,ExStantion
order by sendTime desc) rownum
from
(
SELECT [CardNo]
,[CardID]
,[CardSave]
,[TollAmount]
,[ExTime]
,[ExRoad]
,[ExStation]
,[ExShiftDate]
,[EnTime]
,[EnRoad]
,[EnStation]
,[ExVehPlate]
,[VerifyCode]
,[DealStatus]
,[SendTime]
FROM [QTKCenter].[dbo].[SpendHistory]
where [CardNo] ='6212262409000028515' and
[ExTime] between '2013-3-1' and '2013-3-31'
) t
)t
where rownum=1
SELECT [CardNo]
,[CardID]
,[CardSave]
,[TollAmount]
,[ExTime]
,[ExRoad]
,[ExStation]
,[ExShiftDate]
,[EnTime]
,[EnRoad]
,[EnStation]
,[ExVehPlate]
,[VerifyCode]
,[DealStatus]
,[SendTime]
FROM [QTKCenter].[dbo].[SpendHistory] a
where a.[CardNo]='6212262409000028515' and a.[ExTime] between '2013-3-1' and '2013-3-31'
and not exists
(select 1 from [QTKCenter].[dbo].[SpendHistory] b
where b.[CardNo]='6212262409000028515' and b.[ExTime] between '2013-3-1' and '2013-3-31'
and b.[Extime]=a.[Extime] and b.[ExStantion]=a.[ExStantion] and b.[SendTime]>a.[SendTime])
select [CardNo],[CardID],[CardSave],[TollAmount],[ExTime],[ExRoad],[ExStation]
,[ExShiftDate],[EnTime],[EnRoad],[EnStation],[ExVehPlate],[VerifyCode]
,[DealStatus],[SendTime]
from
(
select *,
row_number() over(partition by Extime,ExStantion
order by sendTime desc) rownum
from
(
SELECT [CardNo]
,[CardID]
,[CardSave]
,[TollAmount]
,[ExTime]
,[ExRoad]
,[ExStation]
,[ExShiftDate]
,[EnTime]
,[EnRoad]
,[EnStation]
,[ExVehPlate]
,[VerifyCode]
,[DealStatus]
,[SendTime]
FROM [QTKCenter].[dbo].[SpendHistory]
where [CardNo] ='6212262409000028515' and
[ExTime] between '2013-3-1' and '2013-3-31'
) t
)t
where rownum=1
select [CardNo],[CardID],[CardSave],[TollAmount],[ExTime],[ExRoad],[ExStation]
,[ExShiftDate],[EnTime],[EnRoad],[EnStation],[ExVehPlate],[VerifyCode]
,[DealStatus],[SendTime] from
(SELECT [CardNo]
,[CardID]
,[CardSave]
,[TollAmount]
,[ExTime]
,[ExRoad]
,[ExStation]
,[ExShiftDate]
,[EnTime]
,[EnRoad]
,[EnStation]
,[ExVehPlate]
,[VerifyCode]
,[DealStatus]
,[SendTime]
,row_number() over(partition by Extime,ExStantion order by sendTime desc) 'rn'
FROM [QTKCenter].[dbo].[SpendHistory]
where [CardNo] ='6212262409000028515' and [ExTime] between '2013-3-1' and '2013-3-31'
) t where rn=1
select a.*
from
(SELECT [CardNo]
,[CardID]
,[CardSave]
,[TollAmount]
,[ExTime]
,[ExRoad]
,[ExStation]
,[ExShiftDate]
,[EnTime]
,[EnRoad]
,[EnStation]
,[ExVehPlate]
,[VerifyCode]
,[DealStatus]
,[SendTime]
FROM [QTKCenter].[dbo].[SpendHistory]
where [CardNo] ='6212262409000028515' and [ExTime] between '2013-3-1' and '2013-3-31'
)a
inner join
(
SELECT Extime,ExStantion,max(sendTime) as sendTime
FROM [QTKCenter].[dbo].[SpendHistory]
where [CardNo] ='6212262409000028515' and [ExTime] between '2013-3-1' and '2013-3-31'
group by Extime,ExStantion
)b on a.Extime=b.Extime and a.ExStantion=b.ExStantion and a.sendTime=b.sendTime