执行sql查询需要很长时间

时间:2013-04-19 09:05:04

标签: sql sql-server-2008

我的查询有这个问题,它确实很慢。我正在使用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

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

1 个答案:

答案 0 :(得分:0)

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

您正在深入运行3个子选择。无论指标如何变化,这都会影响您的表现。

你需要重写整个事情以停止使用子选择。