Эффективный метод для запроса нескольких таблиц и объединения результатов - PullRequest
0 голосов
/ 28 апреля 2019

Я выполняю запрос для извлечения данных временного ряда из приблизительно 30 таблиц.Я сокращаю данные, чтобы уменьшить количество точек данных и время запроса.Каждая таблица содержит миллионы строк и более десятка столбцов.Меня интересует только пара столбцов, и я рассчитываю скользящее среднее и уменьшение с шагом в 1 час.Но я вижу, что когда я добавляю таблицы, для выполнения запроса начинает существовать достаточно времени.Это то, что я хочу иметь возможность часто запускать для обновления отчета, поэтому мне нужно, чтобы он работал намного быстрее.

Я пробовал использовать вложенные производные таблицы и функцию перекрестного применения для агрегированиярезультаты из таблиц.

Есть ли лучший способ уменьшить время запроса?

В настоящее время из этих 3 таблиц возвращается 735 строк (15 столбцов).

Select * from
(select
        min([theDate]) as theDate, 
        min([theTime]) as theTime, 
        max([Value]) as maxValue, 
        max([rolling_avg]) as maxDM,
        timeHour as timeHour

from( select [theDate], [theTime], [Value],
       avg(windowAvg) over(order by theDate DESC, theTime rows between 90 preceding and current row) as rolling_avg,
       datepart(hh,theTime) as timeHour
  from (select [theDate], [theTime], [Value], sum([Value]) as windowAvg
          from [Data].[dbo].[tOne]
          Where ([theDate] > convert(DAte,DATEADD(month, -1, GETDATE())))
          group by theDate, theTime, Value 

          )tOneTemp   
          )tOneTempTwo
          group by theDate, timeHour) tOne 
Cross Apply (select
        min([theDate]) as theDate, 
        min([theTime]) as theTime, 
        max([Value]) as maxValue, 
        max([rolling_avg]) as maxDM,
        timeHour as timeHour

from( select [theDate], [theTime], [Value],
       avg(windowAvg) over(order by theDate DESC, theTime rows between 90 preceding and current row) as rolling_avg,
       datepart(hh,theTime) as timeHour
  from (select [theDate], [theTime], [Value], sum([Value]) as windowAvg
          from [Data].[dbo].[tTwo]
          Where ([theDate] > convert(DAte,DATEADD(month, -1, GETDATE())))
          group by theDate, theTime, Value 

          )tTwoTemp   
          )tTwoTempTwo
          group by theDate, timeHour) tTwo

Cross Apply (select
        min([theDate]) as theDate, 
        min([theTime]) as theTime, 
        max([Value]) as maxValue, 
        max([rolling_avg]) as maxDM,
        timeHour as timeHour

from( select [theDate], [theTime], [Value],
       avg(windowAvg) over(order by theDate DESC, theTime rows between 90 preceding and current row) as rolling_avg,
       datepart(hh,theTime) as timeHour
  from (select [theDate], [theTime], [Value], sum([Value]) as windowAvg
          from [Data].[dbo].[tThree]
          Where ([theDate] > convert(DAte,DATEADD(month, -1, GETDATE())))
          group by theDate, theTime, Value 

          )tThreeTemp   
          )tThreeTempTwo
          group by theDate, timeHour) tThree

where tOne.timeHour = tTwo.timeHour and tOne.theDate = tTwo.theDate and tOne.timeHour = tThree.timeHour and tOne.theDate = tThree.theDate
 order by tOne.theDate DESC, tOne.theTime DESC  
...