Answer the question
In order to leave comments, you need to log in
Why is subquery execution slower?
Why is running a query as a subquery slower than running it sequentially the same number of times?
select top 1 login from SiemensLicUsage
where CONVERT(DATE,SiemensLicUsage.logDateTime)=DATEADD(DAY,-1,CONVERT(DATE, GETDATE()))
and SiemensLicUsage.licId='cam_base' and SiemensLicUsage.filename='109012981';
DECLARE @StartDate DATE = DATEADD(DAY,-10,CONVERT(DATE,GETDATE()))
, @EndDate DATE = CONVERT(DATE,GETDATE())
select dt,
(
select top 1 login from SiemensLicUsage where SiemensLicUsage.licId='cam_base' and
SiemensLicUsage.filename='109012981' and CONVERT(DATE,SiemensLicUsage.logDateTime)=CONVERT(DATE, dt)
) LicUsageCount
from
-- Генерация диапазона дат между заданными значениями------------------
(
SELECT DATEADD(DAY, nbr - 1, @StartDate) dt
FROM ( SELECT ROW_NUMBER() OVER ( ORDER BY c.object_id ) AS nbr
FROM sys.columns c
) nbrs
WHERE nbr - 1 <= DATEDIFF(DAY, @StartDate, @EndDate)
) dates
------------------------------------------------------------------------
order by dt desc;
select top 1 login from SiemensLicUsage where CONVERT(DATE,SiemensLicUsage.logDateTime)=CONVERT(DATE, GETDATE())
and SiemensLicUsage.licId='cam_base' and SiemensLicUsage.filename='109012981';
select top 1 login from SiemensLicUsage where CONVERT(DATE,SiemensLicUsage.logDateTime)=DATEADD(DAY,-1,CONVERT(DATE, GETDATE()))
and SiemensLicUsage.licId='cam_base' and SiemensLicUsage.filename='109012981';
select top 1 login from SiemensLicUsage where CONVERT(DATE,SiemensLicUsage.logDateTime)=DATEADD(DAY,-2,CONVERT(DATE, GETDATE()))
and SiemensLicUsage.licId='cam_base' and SiemensLicUsage.filename='109012981';
select top 1 login from SiemensLicUsage where CONVERT(DATE,SiemensLicUsage.logDateTime)=DATEADD(DAY,-3,CONVERT(DATE, GETDATE()))
and SiemensLicUsage.licId='cam_base' and SiemensLicUsage.filename='109012981';
select top 1 login from SiemensLicUsage where CONVERT(DATE,SiemensLicUsage.logDateTime)=DATEADD(DAY,-4,CONVERT(DATE, GETDATE()))
and SiemensLicUsage.licId='cam_base' and SiemensLicUsage.filename='109012981';
select top 1 login from SiemensLicUsage where CONVERT(DATE,SiemensLicUsage.logDateTime)=DATEADD(DAY,-5,CONVERT(DATE, GETDATE()))
and SiemensLicUsage.licId='cam_base' and SiemensLicUsage.filename='109012981';
select top 1 login from SiemensLicUsage where CONVERT(DATE,SiemensLicUsage.logDateTime)=DATEADD(DAY,-6,CONVERT(DATE, GETDATE()))
and SiemensLicUsage.licId='cam_base' and SiemensLicUsage.filename='109012981';
select top 1 login from SiemensLicUsage where CONVERT(DATE,SiemensLicUsage.logDateTime)=DATEADD(DAY,-7,CONVERT(DATE, GETDATE()))
and SiemensLicUsage.licId='cam_base' and SiemensLicUsage.filename='109012981';
select top 1 login from SiemensLicUsage where CONVERT(DATE,SiemensLicUsage.logDateTime)=DATEADD(DAY,-8,CONVERT(DATE, GETDATE()))
and SiemensLicUsage.licId='cam_base' and SiemensLicUsage.filename='109012981';
select top 1 login from SiemensLicUsage where CONVERT(DATE,SiemensLicUsage.logDateTime)=DATEADD(DAY,-9,CONVERT(DATE, GETDATE()))
and SiemensLicUsage.licId='cam_base' and SiemensLicUsage.filename='109012981';
select top 1 login from SiemensLicUsage where CONVERT(DATE,SiemensLicUsage.logDateTime)=DATEADD(DAY,-10,CONVERT(DATE, GETDATE()))
and SiemensLicUsage.licId='cam_base' and SiemensLicUsage.filename='109012981';
Answer the question
In order to leave comments, you need to log in
Of course, you need to look at the intended and actual query execution plan.
But try adding ORDER BY
by clustered index. Or is there an index on login
?
No subqueries andSELECT TOP
DECLARE @StartDate DATE = DATEADD(DAY,-10,CONVERT(DATE,GETDATE())),
@EndDate DATE = CONVERT(DATE,GETDATE())
SELECT CONVERT(DATE, logDateTime), [login]
FROM SiemensLicUsage
WHERE (licId = 'cam_base') AND ([filename] = '109012981')
AND CONVERT(DATE, logDateTime) BETWEEN @StartDate AND @EndDate
GROUP BY CONVERT(DATE, logDateTime), [login]
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question