我已经创建了一个存储过程来获取数据。在这个存储过程中,我返回了1个表和表,存储了10万数据以上的数据。所以现在我已经运行了存储过程,以至于我在1分钟以上的时间内获取数据。我只想在1秒钟内获取数据。我也设置了SET NOCOUNT ON;
并且还创建了缺失的索引。尽管如此,我还是得到了获取数据的时间。如何提高使用SQL Server查询的速度?
这是我的查询:
DECLARE @CurMon int
DECLARE @year nvarchar(max)
SELECT @CurMon = month(getdate())
SELECT @year = year(getdate())
SELECT
FORMAT(dateadd(MM, T.i, getdate()),'MMM-yy') AS DateColumn,
ISNULL(uf.TotalCount, 0) as TotalCount
FROM
(VALUES ([email protected]),([email protected]),([email protected]),([email protected]),([email protected]),([email protected]),([email protected]), ([email protected]), ([email protected]), ([email protected]), ([email protected]), ([email protected])) AS T(i)
OUTER APPLY
(SELECT DISTINCT
COUNT(datepart(MM,UF.InsertDateTime)) OVER (partition by datepart(MM,UF.InsertDateTime)) AS TotalCount
FROM dbo.UserFollowers UF
INNER JOIN dbo.Users U on U.UserId = UF.FollowerId
WHERE DATEDIFF(mm,UF.InsertDateTime, DATEADD(mm, T.i, GETDATE())) = 0 and UF.IsFollowed = 1
) uf
order by DATEPART(MM,convert(datetime,FORMAT(dateadd(MM, T.i, getdate()),'MMMM') +'01 '[email protected],110))
我也是尝试查询的提高速度的一些其他的查询,但我仍得到相同的时间。这里这个查询也打印。
declare @StartDate datetime = dateadd(year , datediff(year , 0, getdate()) , 0)
declare @tempT2 table
(
MNo int,
[Month] datetime,
NextMonth datetime)
;with Months as (
select top (12)
MNo = row_number() over (order by number)
,[Month] = dateadd(month, row_number() over (order by number) -1, @StartDate)
, NextMonth = dateadd(month, row_number() over (order by number), @StartDate)
from master.dbo.spt_values
)
insert into @tempT2
select * from Months
select
m.MNo
, Month = format(m.Month, 'MMM-yy')
, tally = count(UF.InsertDateTime)
from @tempT2 m
left join dbo.UserFollowers UF
INNER JOIN dbo.Users U on U.UserId = UF.FollowerId
on UF.InsertDateTime >= m.Month
and UF.InsertDateTime < m.NextMonth where UF.IsFollowed = 1
group by m.MNo,format(m.Month, 'MMM-yy')
order by MNo
这里这是我的两个查询我有尝试,但我仍然没有得到提高查询速度的成功。抱歉,但我不能在这里看到我的执行计划的查询,实际上我没有这方面的许可。
请使用措施的**国际理解**单位:十万,上百万,数十亿... –
问题寻求帮助的性能应包括DDL,DML的表一起参与测试数据..如果测试数据很大,请尝试脚本化该表的模式和统计信息('右键单击数据库 - >生成脚本 - >选择特定数据库对象 - >在下一个屏幕中选择高级并选择脚本统计信息)'并粘贴它有问题。有了这个信息任何一个repro你面临同样的问题。否则它变得很难回答你的问题.Pasting服务器v ersion也有助于 – TheGameiswar
@TheGameiswar,但我无法为此生成脚本和执行计划显示权限。你想要一些数据样本,我的o/p想要在这里我可以做到这一点?只是这个查询需要越来越多的时间来获取服务器上的数据。 – Edit