慢SQL查询返回 - 如何获得更好的结果?

时间:2015-12-21 15:05:21

标签: sql-server-2008 common-table-expression

我被要求编写一个返回仅包含特定操作代码(内部名称)的帐户的查询。帐户上有6个代码,其他代码无法启用。该表存储在名为tblTrans的表中(见下文):

AccountNo   ActionCode  TransactionNo
1234        Code1       45646453
1234        Code2       88758475
1234        Code3       48978978
1234        Code4       15687898
1234        Code5       59878988
1234        Code6       12345677
2548        Code1       45464533
2548        Code2       89789489
2548        Code3       89789781
2548        Code4       16878983
2548        Code5       59889884
2548        Code6       12456776
2548        Code12      12348887

因此,所需的输出只会返回帐户1234。

目前正在使用像

这样的查询
SELECT AccountNo, ActionCode, TransactionNo     
FROM tblTrans AS t1 
INNER JOIN
        tblTrans AS t2 ON t1.AccountNo = t2.AccountNo
        tblTrans AS t3 ON t2.AccountNo = t3.AccountNo
        tblTrans AS t4 ON t3.AccountNo = t4.AccountNo
        tblTrans AS t5 ON t4.AccountNo = t5.AccountNo
        tblTrans AS t6 ON t5.AccountNo = t6.AccountNo
WHERE t1.ActionCode = 'Code1' 
  AND t2.ActionCode = 'Code2'
  AND t3.ActionCode = 'Code3'
  AND t4.ActionCode = 'Code4'
  AND t5.ActionCode = 'Code5'
  AND t5.ActionCode = 'Code6'
  AND t6.AccountNo NOT IN (SELECT ActionCode 
                           FROM tblTrans 
                           WHERE ActionCode IN ('Code12'))

很抱歉,如果语法不完整,我出于安全原因不得不更改一些细节!

这实际上运行速度非常慢,并且很少扼杀系统。我的问题是,有没有更好的方法来编写这种类型的查询。我对CTE的了解不多,但听起来很合适吗?

1 个答案:

答案 0 :(得分:1)

这应该返回AccountNo,其中只有您想要的代码。

SELECT AccountNo
FROM   tblTrans
GROUP BY AccountNo
HAVING  Sum(Case When ActionCode = 'Code1' Then 1 End) > 0
        And Sum(Case When ActionCode = 'Code2' Then 1 End) > 0
        And Sum(Case When ActionCode = 'Code3' Then 1 End) > 0
        And Sum(Case When ActionCode = 'Code4' Then 1 End) > 0
        And Sum(Case When ActionCode = 'Code5' Then 1 End) > 0
        And Sum(Case When ActionCode = 'Code6' Then 1 End) > 0
        And Sum(Case When ActionCode IN ('Code1', 'Code2', 'Code3', 'Code4', 'Code5', 'Code6') Then 0 Else 1 End) = 0

我修改了查询。这个应该表现得更好。如果性能仍然不可接受,那么您应该考虑在表中添加索引。

返回所有数据......

; With FilteredData As
(
    SELECT AccountNo
    FROM   tblTrans
    GROUP BY AccountNo
    HAVING  Sum(Case When ActionCode = 'Code1' Then 1 End) > 0
            And Sum(Case When ActionCode = 'Code2' Then 1 End) > 0
            And Sum(Case When ActionCode = 'Code3' Then 1 End) > 0
            And Sum(Case When ActionCode = 'Code4' Then 1 End) > 0
            And Sum(Case When ActionCode = 'Code5' Then 1 End) > 0
            And Sum(Case When ActionCode = 'Code6' Then 1 End) > 0
            And Sum(Case When ActionCode IN ('Code1', 'Code2', 'Code3', 'Code4', 'Code5', 'Code6') Then 0 Else 1 End) = 0
)
Select  TblTrans.AccountNo, ActionCode, TransactionNumber
From    TblTrans
        Inner Join FilteredData
            On tblTrans.AccountNo = FilteredData.AccountNo