0

我的查询有这个问题,它真的很慢。我正在使用 MSSQL server 2008 并且有 3 个数据库,其中包含数百个示例数据。该查询将返回名称和值以及基于 3 个 DB 计算的百分比。但是我的查询需要将近 10 分钟才能执行,这确实需要很长时间。我仍在学习 SQL,但仍然不是那么好,所以我认为我的查询没有使用最佳实践并且没有组织。谁能指出我可以在哪里或如何改进我的查询以更快地运行?

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

使用大量连接会导致执行缓慢吗?

4

1 回答 1

0

SUB Select 查询总是比正确的连接慢。

您正在运行 3 个子选择深度。无论更改索引等如何,这都会影响您的性能。

您需要重写整个内容以停止使用子选择。

于 2013-04-19T09:42:40.437 回答