2017-08-29 134 views
0

我已經創建了一個存儲過程來獲取數據。在這個存儲過程中,我返回了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 

這裏這是我的兩個查詢我有嘗試,但我仍然沒有得到提高查詢速度的成功。抱歉,但我不能在這裏看到我的執行計劃的查詢,實際上我沒有這方面的許可。

+2

請使用措施的**國際理解**單位:十萬,上百萬,數十億... –

+0

問題尋求幫助的性能應包括DDL,DML的表一起參與測試數據..如果測試數據很大,請嘗試腳本化該表的模式和統計信息('右鍵單擊數據庫 - >生成腳本 - >選擇特定數據庫對象 - >在下一個屏幕中選擇高級並選擇腳本統計信息)'並粘貼它有問題。有了這個信息任何一個repro你面臨同樣的問題。否則它變得很難回答你的問題.Pasting服務器v ersion也有助於 – TheGameiswar

+0

@TheGameiswar,但我無法爲此生成腳本和執行計劃顯示權限。你想要一些數據樣本,我的o/p想要在這裏我可以做到這一點?只是這個查詢需要越來越多的時間來獲取服務器上的數據。 – Edit

回答

1

您可以通過切換到一個臨時表,而不是表變量獲得的性能一點點,並擺脫format()

declare @StartDate datetime = dateadd(year , datediff(year , 0, getdate()) , 0) 
create table #Months (
    MNo int not null primary key 
    , Month char(6) not null 
    , MonthStart datetime not null 
    , NextMonth datetime not null 
) 
;with Months as (
select top (12) 
    MNo = row_number() over (order by number) 
    , MonthStart = 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 #Months (MNo, Month, MonthStart, NextMonth) 
select 
    MNo 
    , Month = stuff(convert(varchar(9),MonthStart,6),1,3,'') 
    , MonthStart 
    , NextMonth 
from Months; 

select 
    m.MNo 
, m.Month 
, tally = count(UF.InsertDateTime) 
from @tempT2 m 
    inner join dbo.Users U 
    on UF.InsertDateTime >= m.MonthStart 
    and UF.InsertDateTime < m.NextMonth 
    inner join dbo.UserFollowers UF 
    on U.UserId = UF.FollowerId 
    and UF.IsFollowed = 1 
group by 
    m.MNo 
    , m.Month 
order by MNo 

之後,你應該評估的執行計劃,以確定是否你需要一個更好的索引策略。

如果你仍然需要它走得更快,你可以創建一個實際的日曆表,並着眼於創建一個索引視圖。索引視圖可能是一件繁瑣的事情,根據你的sql server版本,它可以正常運行,但速度會更快。

參考:

+1

非常感謝你的幫助,這是驚人的解決方案。 – Edit

+0

@編輯樂意幫忙! – SqlZim