проблемный запрос к серверу sql - PullRequest
1 голос
/ 31 августа 2011

У меня следующий запрос, который работает медленно:

 WITH AcdTran
     AS (select SequenceNo,
                ReqID,
                PolNumber,
                transaction_id,
                application_data,
                trans_type,
                retries,
                status,
                direction
         from   dbo.acord_transaction_benchmark with (nolock)
         where  direction = 'OUT')
select top 1 *
from   AcdTran a
where  a.transaction_id = (select top 1 transaction_id
                           from   AcdTran b
                           where  b.PolNumber = a.PolNumber
                           order  by ReqID,
                                     SequenceNo,
                                     transaction_id)
       and ( status = 'New'
              or status = 'Resubmit' )
       and retries > 0  

Как я могу оптимизировать это?Чтобы быстрее бегать?

Спасибо

Ответы [ 2 ]

2 голосов
/ 31 августа 2011

Должно быть быстрее с оконной функцией ROW_NUMBER:

 WITH AcdTran AS (
         SELECT SequenceNo,
                ReqID,
                PolNumber,
                transaction_id,
                application_data,
                trans_type,
                retries,
                status,
                direction,
                ROW_NUMBER() OVER(PARTITION BY transaction_id ORDER BY ReqID, SequenceNo, transaction_id) N
           FROM dbo.acord_transaction_benchmark with (nolock)
          WHERE direction = 'OUT')
SELECT *
  FROM AcdTran
 WHERE (status = 'New'
            OR status = 'Resubmit')
       AND retries > 0
       AND N = 1;

Поскольку у меня нет ни структуры вашей таблицы, ни данных, я, очевидно, не проверял ее, поэтому вам, возможно, придется немного изменить запрос, но у вас есть идея.

0 голосов
/ 31 августа 2011

Что если вы вытянули свой подзапрос в объединение:

WITH AcdTran
     AS (select SequenceNo,
                ReqID,
                PolNumber,
                transaction_id,
                application_data,
                trans_type,
                retries,
                status,
                direction
         from   dbo.acord_transaction_benchmark with (nolock)
         where  direction = 'OUT')
select top 1 *
from   AcdTran a
inner join AcdTran b on a.SequenceNo = b.SequenceNo --or whatever the PK is
where a.transaction_id = b.transaction_id and
      a.PolNumber = b.PolNumber and
      (a.status = 'New' or a.status = 'Resubmit' ) and
      a.retries > 0
order by b.ReqID,
         b.SequenceNo,
         b.transaction_id
...