简体   繁体   English

SQL Server查询需要3分40秒。 如何提高执行时间?

[英]SQL Server query is taking 3 min 40 Sec. How to improve the execution Time?

I have a query that is taking 3 min 40 sec. 我的查询时间为3分40秒。

Here except the AirESet and ELT_B tables, all other tables are lookup tables and there is a lot less data in these tables. 除了AirESetELT_B表之外,所有其他表都是查找表,这些表中的数据少得多。 Interestingly without these lookup tables execution time was around 30 sec. 有趣的是,如果没有这些查找表,执行时间大约为30秒。 My understanding is with lookup tables containing a lot less data, performance should not degrade. 我的理解是查找表包含的数据少得多,性能不会降低。

SELECT DISTINCT 
    RPGM.GroupID as RegionPerilGroupID,
    RPGM.GroupName as RPGroupName,
    LLGM.GroupID as LossLevelGroupID,
    LLG.GroupName As LLGroupName
FROM
    Acc.dbo.AIRESet ES 
JOIN 
    Acc.dbo.vw_RegionPerilGroup RPGM ON ES.RegionperilID = RPGM.RegionperilID 
                                     AND ModelLossFileID = 65 -- Line of Business
JOIN 
    AR.LA.ELT_B ELT WITH (NOLOCK) ON ES.EventNum = ELT.EventNum 
                                  AND ELT.Versionid = 215 
JOIN 
    AR.LA.LossLevelGroupMappings LLGM ON ELT.LossLevelID = LLGM. LossLevelID 
JOIN 
    AR.LA.LossLevelGroup LLG ON LLGM.GroupID = LLG.GroupID 
                             AND LLG.LosstypeId = 3 
JOIN 
    AR.LA.ReportMap RM ON RPGM.GroupId = RM.RegionPerilGroupID 
                       AND LLGM.GroupID = RM.LossLevelGroupID
JOIN 
    AR.LA.ReportMapFilterMappings RMFM ON RM.ReportMapID = RMFM.ReportMapID
                                       AND RMFM.FilterID = 15
ORDER BY 
    LossLevelGroupID, RegionPerilGroupID 

Let me take a guess. 让我猜一下。
Please add option (HASH JOIN) to your query. 请在查询中添加option (HASH JOIN)


Ps PS
SELECT distinct ... is usually an indication for query logic issues. SELECT distinct ...通常是查询逻辑问题的指示。
Please find the cause for the duplications. 请找出重复的原因。


... and - ......和 ​​-

  1. No. 没有。
    You don't need indexes for equality join operations. 您不需要用于等于连接操作的索引。
    This is SQL Server, not MySQL and it supports HASH JOIN. 这是SQL Server,而不是MySQL,它支持HASH JOIN。

    Furthermore - 此外 -
    The use of indexes for retrieval of large data volumes leads to significant degradation in performance. 使用索引检索大数据量会导致性能显着下降。


  1. No. 没有。
    GROUP BY / DISTINCT does not necessarily imply a SORT operation. GROUP BY / DISTINCT不一定意味着SORT操作。
    It can be also implemented using HASH with complexity of O(1) instead of O(n*log n). 它也可以使用复杂度为O(1)而不是O(n * log n)的HASH来实现。

The limitations of statistics 统计数据的局限性

select @@version

 Microsoft SQL Server 2014 - 12.0.4213.0 (X64) Jun 9 2015 12:06:16 Copyright (c) Microsoft Corporation Express Edition (64-bit) on Windows NT 6.1 <X64> (Build 7601: Service Pack 1) 

create table t (n int primary key)

;with   t (n) as (select 1 union all select n+1 from t where n < 1000)
insert into dbo.t select n from t option  (maxrecursion 0)

create statistics t_n on t (n)
update statistics t (t_n)

;with x as (select * from t where sqrt(n) = n)
select * from x x0,x x1,x x2,x x3,x x4,x x5

 +---+---+---+---+---+---+ | n | n | n | n | n | n | +---+---+---+---+---+---+ | 1 | 1 | 1 | 1 | 1 | 1 | +---+---+---+---+---+---+ 

在此输入图像描述

HASH JOIN Vs. HASH JOIN Vs. LOOP JOIN 循环加入

create table t (i int,j1 int,j2 int,j3 int,j4 int,j5 int)

;with   t (n) as (select 0 union all select n+1 from t where n < 999)
insert into dbo.t (i,j1,j2,j3,j4,j5) 
select      1+t0.n+1000*t1.n,1+t0.n,1+t0.n,1+t0.n,1+t0.n,1+t0.n
from        t t0,t t1 
option      (maxrecursion 0)

create table d1 (j int primary key,k int)
create table d2 (j int primary key,k int)
create table d3 (j int primary key,k int)
create table d4 (j int primary key,k int)
create table d5 (j int primary key,k int)

;with   t (n) as (select 0 union all select n+1 from t where n < 999)
insert into dbo.d1 (j,k) 
select      1+t0.n,1
from        t t0 
option      (maxrecursion 0)

insert into d2 select * from d1
insert into d3 select * from d1
insert into d4 select * from d1
insert into d5 select * from d1

select  sum (d1.k+d2.k+d3.k+d4.k+d5.k)

from            t 
        join    d1 on t.j1 = d1.j 
        join    d2 on t.j2 = d2.j 
        join    d3 on t.j3 = d3.j 
        join    d4 on t.j4 = d4.j 
        join    d5 on t.j5 = d5.j 

option (hash join)

select  sum (d1.k+d2.k+d3.k+d4.k+d5.k)

from            t 
        join    d1 on t.j1 = d1.j 
        join    d2 on t.j2 = d2.j 
        join    d3 on t.j3 = d3.j 
        join    d4 on t.j4 = d4.j 
        join    d5 on t.j5 = d5.j 

option (loop join)

HASH GROUP Vs. 哈希集团比。 ORDER GROUP 订单组

create table t (i int,j int)

;with   t (n) as (select 0 union all select n+1 from t where n < 9)
insert into dbo.t (i,j) 
select      t0.n,t0.n
from        t t0,t t1,t t2,t t3,t t4,t t5,t t6

select      distinct
            i,j
from        t
option      (HASH GROUP)
;

select      i,j
from        t
group by    i,j
option      (HASH GROUP)
;

select      distinct
            i,j
from        t
option      (ORDER GROUP)
;

select      i,j
from        t
group by    i,j
option      (ORDER GROUP)
;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM