SQL查询比较处理效率,任何更好的解决方案?

时间:2017-06-30 12:53:41

标签: sql sql-server tsql processing-efficiency

我正在处理大约1.34亿行的大量数据,我想在表格中插入一个select查询。 这是我的表SQL script (SQL Fiddle)

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Id | Emitter    |   EmitterIBAN                         |  Receiver    |   ReceiverIBAN                           |         Adresss                          |     Value 
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1,   Ernst,         HR53 8827 2118 4692 8207 5,           Kimbra,         CH20 1042 6T0N MDTG JT47 U,                     3256 Arrowood Point         0002,        121.72
2,   Keene,         SK81 1004 7484 7505 6308 9259,        Torrance,       RO23 ZWTR OJKK VAU9 T5P4 2GDY,                  35197 Green Ridge Way,                   82.52
3,   Ernst,         HR53 8827 2118 4692 8207 5,           Kimbra,         CH20 1042 6T0N MDTG JT47 U,                     3256 Arrowood Point         0048,        51.81
4,   Korie,         ME43 9833 9830 7367 4239 60,Roy,      IL69            9686 1536 8102 2219 165,                        5 Swallow Alley,                         88.01
5,   Ernst,         HR53 8827 2118 4692 8207 5,           Kimbra,         CH20 1042 6T0N MDTG JT47 U,                     3256 Arrowood Point         0001,        133.99
6,   Charmine,      BG92 TOXX 8380 785I JKRQ JS,          Sarette,        MU67 RYRU 9293 5875 6859 7111 075X HR,          8 Sage Place,                            36.30
7,   Ernst,         HR53 8827 2118 4692 8207 5,           Kimbra,         CH20 1042 6T0N MDTG JT47 U,                     3256 Arrowood Point         0004,        186.99

我使用此查询选择我的数据


Select  count(1) as NumberOperation, 
        MAX(Emitter) as EmitterName, 
        EmitterIban, 
        MAX(Receiver) as ReceiverName, 
        ReceiverIban,
        MAX(ReceiverAddress) as ReceiverAddress,
        SUM([Value]) as SumValues
FROM TableEsperadoceTransaction
Group By EmitterIban,
         ReceiverIban

我得到以下结果

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
NumberOperation | Emitter    |   EmitterIBAN                         |  Receiver    |   ReceiverIBAN                           |         Adresss                          |     SumValue 
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
4,                Ernst,         HR53 8827 2118 4692 8207 5,           Kimbra,         CH20 1042 6T0N MDTG JT47 U,                     3256 Arrowood Point           0002,      494,51
1,                Keene,         SK81 1004 7484 7505 6308 9259,        Torrance,       RO23 ZWTR OJKK VAU9 T5P4 2GDY,                  35197 Green Ridge Way,                   82.52
1,                Korie,         ME43 9833 9830 7367 4239 60,Roy,      IL69            9686 1536 8102 2219 165,                        5 Swallow Alley,                         88.01
1,                Charmine,      BG92 TOXX 8380 785I JKRQ JS,          Sarette,        MU67 RYRU 9293 5875 6859 7111 075X HR,          8 Sage Place,                            36.30

我也有这个解决方案


SELECT DISTINCT *
FROM   (SELECT Count(1)     AS NumberOperation, 
               emitteriban  AS _EmitterIban, 
               receiveriban AS _ReceiverIban, 
               Sum([value]) AS SumValues 
        FROM   tableesperadocetransaction 
        GROUP  BY emitteriban, 
                  receiveriban) tmp_T 
       LEFT JOIN tableesperadocetransaction 
              ON tableesperadocetransaction.emitteriban = tmp_T._emitteriban 
                 AND tableesperadocetransaction.receiveriban = 
                     tmp_T._receiveriban  

我想知道这两者之间的最佳解决方案是什么,如果有更高效的查询?

由于

3 个答案:

答案 0 :(得分:1)

第二个查询较慢,因为:

  1. 它有LEFT JOIN
  2. 它有一个子查询
  3. 它有SELECT DISTINCT
  4. 拥有*而不是列名
  5. 第一个是最自然的方式。

    有很多关于如何提高查询性能以及要避免的内容。例如,请参阅:MSDN on improving queries

答案 1 :(得分:0)

第一个查询应该更有效率。

如果您真的想加快速度,那么您需要确保使用EmitterIban,ReceiverIban作为关键字覆盖索引。

答案 2 :(得分:0)

你可以试试这个。

使用它进入INNER JOIN后得到MIN(id)。这也是一种方式。

SELECT
tmp.NumberOperation
,tb.Emitter
,tmp.EmitterIban
,tb.Receiver
,tmp.ReceiverIban
,tb.Adresss
,tmp.SumValues
FROM   (SELECT Count(1)     AS NumberOperation, 
               emitteriban  AS EmitterIban, 
               receiveriban AS ReceiverIban, 
               Sum([value]) AS SumValues,
               MIN(Id)      AS Id
        FROM   tableesperadocetransaction 
        GROUP  BY emitteriban, 
                  receiveriban) tmp
       INNER JOIN tableesperadocetransaction tb
              ON tableesperadocetransaction.id = tmp.Id