简体   繁体   English

执行SQL查询需要很长时间

[英]Execution of sql query takes a long time

I have this problem with my query that it is really slow. 我的查询存在这个问题,它确实很慢。 I am using MSSQL server 2008 and have 3 DB with hundreds of sample data in it. 我正在使用MSSQL Server 2008,并且具有3个DB,其中包含数百个示例数据。 The query will return a name and value and a computed percentage based on the 3 DBs. 查询将返回名称和值以及基于3个DB的计算百分比。 But the query I have will take almost 10mins to execute which is really a long time to take. 但是我要执行的查询将花费将近10分钟的时间,这确实需要很长时间。 I am still learning SQL and still not that good so I think the query I have is not using the best practice and not organized. 我仍在学习SQL,但仍然不够好,因此我认为我所使用的查询未使用最佳实践并且没有组织。 Can anybody point to me where or how I can improve my query to run faster? 有人可以指出我在哪里或如何改善查询的运行速度吗?

SELECT data.Ret,
    case 
        when @group_by= 'site' OR (@group_by='attribute' AND @attribute_id = '5') and (data.rowid % 50) = 0 then (data.rowid / 50)-1
        when @group_by= 'site' OR (@group_by='attribute' AND @attribute_id = '5') then (data.rowid / 50) 
        else 0 end as batchStore
        ,data.MajorName,data.MinorName,data.MajorVal,data.MinorVal,data.Version
        ,data.A_Percent,data.T_Percent,data.F_Percent
from 
(
    SELECT report.Ret,
        CASE when @group_by= 'site' OR (@group_by='attribute' AND @attribute_id = '5') 
        then row_number() over (PARTITION BY report.Ret,report.Version order by report.Ret, report.MajorName)
        else 0 end  as rowid
        ,report.MajorName,report.MinorName,report.MajorVal,report.MinorVal,report.Version
        ,report.GTotal_A,report.GTotal_T,report.GTotal_F
        ,ISNULL(sum(report.Abn) / NULLIF(cast(report.GTotal_A as decimal),0),0) * 100 as A_Percent
        ,ISNULL(sum(report.Trn) / NULLIF(cast(report.GTotal_T as decimal),0),0) * 100 as T_Percent
        ,ISNULL(sum(report.Fld)/ NULLIF(cast(report.GTotal_F as decimal),0) * 100,0) as F_Percent
    From 
    (
        Select
            CASE @group_by
            WHEN 'object' THEN csl.s_name
            WHEN 'site' THEN csl.s_name
            WHEN 'year' THEN CAST(YEAR(dy.Day_Stamp) AS VARCHAR(50))
            WHEN 'attribute' THEN CAST(coalesce(attrib.AttributeName,'') AS VARCHAR(50))
            ELSE ''
            END as MajorName,
            CASE @group_by
            WHEN 'object' THEN l.l_name
            WHEN 'site' THEN ''
            WHEN 'attribute' THEN CAST(coalesce(attrib.AttributeName,'') AS VARCHAR(50))
            ELSE ''
            END as MinorName,
            CASE @group_by
            WHEN 'object' THEN csl.s_name
            WHEN 'site' THEN csl.s_name
            WHEN 'year' THEN CAST(YEAR(dy.Day_Stamp) AS VARCHAR(50))
            WHEN 'attribute' THEN CAST(coalesce(attrib.AttributeValue,'') AS VARCHAR(50))
            ELSE ''
            END as MajorVal,
            CASE @group_by
            WHEN 'object' THEN l.l_name
            WHEN 'site' THEN ''
            WHEN 'attribute' THEN CAST(coalesce(attrib.AttributeValue,'') AS VARCHAR(50))
            ELSE ''
            END as MinorVal,
            csl.Cust_Name as Ret,l.SWVersion as Version
            ,d.Abn,d.Trn,d.Fld,data.GTotal_A ,data.GTotal_T,data.GTotal_F
        From db_mon.dbo.CustSL csl
            join db_tax.dbo.vwLane l 
                on (l.externalid = csl.custsl_id)
            join db_mon.dbo.DaySummary dy 
                on (dy.Str = csl.s_name and dy.Lne = csl.l_name and year(dy.day_stamp) = year(@time_start_date) and year(dy.day_stamp) =year(@time_end_date))
            Left Outer Join
                (
                    Select a.id As AttributeId, a.attribute_name As AttributeName,
                    (Case When a.attribute_value_type = 'string' Then ea.string_value
                        Else (Case When a.attribute_value_type = 'integer' Then cast(ea.integer_value as nvarchar(100))
                            Else (Case When a.attribute_value_type = 'date' Then cast(ea.date_value as nvarchar(100))
                                Else (Case When a.attribute_value_type = 'boolean' Then cast(ea.boolean_value as nvarchar(100))
                                    Else (Case When a.attribute_value_type = 'entity' Then cast(ea.ref_entity_id as nvarchar(100)) Else null End)
                                    End)
                                End)
                            End)
                        End) As AttributeValue,
                     e.id As EntityId
                     From db_tax.dbo.entity_type et
                     Inner Join db_tax.dbo.entity As e on et.id = e.entity_type_id
                     Inner Join db_tax.dbo.entity_attribute As ea on e.id = ea.entity_id
                     Inner Join db_tax.dbo.attribute As a on ea.attribute_id = a.id
                     WHERE et.entity_type_name in ('Sticker','Label') And
                     a.id = (case WHEN @attribute_id = '' then 1 else cast(@attribute_id as int) end)
                ) AS attrib 
                    On attrib.EntityId = l.L_Id
             inner join db_mon.dbo.DaySummary d 
                on (csl.Cust_Name = d.Ret and csl.s_name = d.stckr and csl.l_name = d.label and l.SWVersion = d.Version)
             join ( 
                SELECT Ret,version,sum(Abn) as GTotal_A,sum(Trn) as GTotal_T,sum(Fld) as GTotal_F
                from db_mon.dbo.DaySummary
                where day_stamp >= @time_start_date and day_stamp <=@time_end_date
                GROUP BY Ret,version   
             ) data 
                on (d.Ret = data.Ret and l.SWVersion = data.Version)
        WHERE (CHARINDEX(',' + CONVERT(VARCHAR,l.S_Id) + ',','xxx,' + @entities + ',xxx')>0 OR CHARINDEX(',' + CONVERT(VARCHAR,l.L_Id) + ',','xxx,' + @entities + ',xxx')>0)
        and d.day_stamp >= @time_start_date 
        and d.day_stamp <=@time_end_date
    ) As report
Group By report.Ret,report.Version,report.MajorName,report.MinorName,report.MajorVal,report.MinorVal
,report.GTotal_A,report.GTotal_T,report.GTotal_F
)data
order By data.Ret,data.Version,batchStore,data.MajorName,data.MinorName,data.MajorVal,data.MinorVal

Does using a lot of join causes the slow execution? 使用大量的联接是否会导致执行缓慢?

SUB Select queries will always be slower then proper joins. SUB Select查询总是比适当的联接慢。

You are running 3 sub selects deep. 您正在运行3个子选择深度。 This is going to impact your performance regardless of changing indexes etc. 无论更改索引等如何,这都会影响您的性能。

You need to rewrite the whole thing to stop using sub selects. 您需要重写整个内容以停止使用子选择。

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

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