调整慢速执行查询

时间:2014-04-29 11:48:26

标签: sql-server-2008

我的查询执行时间太长。

我使用数据库调优顾问程序检查此查询,并建议删除一些索引和统计信息。问题是我无法在这些表上创建缺失的索引和统计信息,因为它会减慢插入/更新速度并影响其他脚本。由于我的查询,他们无法牺牲脚本性能。

如果没有DTA的帮助或干扰其他脚本,我如何调整查询?我能把它分成小块吗?如果是这样,怎么样?

INSERT INTO #val 
SELECT lid.orgid, 
       lid.periodid, 
       lid.sourceid, 
       lid.statementtypecode, 
       lid.financialsbucketid, 
       lid.statementcurrencycodeiso, 
       lid.lineitemid, 
       lll.financialconceptidglobal, 
       lid.physicalmeasureid, 
       CASE 
         WHEN EXISTS(SELECT TOP 1 '1' 
                     FROM   trf.dbo.lineitemfundbdescription LIFD(nolock) 
                     WHERE  lll.orgid = lifd.orgid 
                            AND lll.lineitemid = lifd.lineitemid) THEN 
         (SELECT lifd.lineitemshortdescription 
          FROM   trf.dbo.lineitemfundbdescription LIFD(nolock) 
          WHERE  lll.orgid = lifd.orgid 
                 AND lll.lineitemid = lifd.lineitemid) 
         ELSE lll.lineitemname 
       END              AS LineItemName, 
       ( CASE 
           WHEN ( lid.reportedcurrencycodeiso IS NOT NULL 
                  AND fc.iscurrencydependent = 1 
                  AND 
           lid.statementcurrencycodeiso <> lid.reportedcurrencycodeiso 
                  AND fc.isflowitem = 1 ) THEN 
lid.lineiteminstancevalue * 
cds.dbo.Exrate(lid.reportedcurrencycodeiso, lid.statementcurrencycodeiso, p.periodenddate) 
  WHEN ( lid.reportedcurrencycodeiso IS NOT NULL 
         AND fc.iscurrencydependent = 1 
         AND lid.statementcurrencycodeiso <> lid.reportedcurrencycodeiso 
         AND fc.isflowitem = 0 
         AND p.periodlengthunitcode = 'M' ) THEN lid.lineiteminstancevalue 
* 
cds.dbo.Getaveragefxrate(lid.reportedcurrencycodeiso, lid.statementcurrencycodeiso, 
Dateadd("MONTH", -p.periodlength, p.periodenddate), p.periodenddate) 
  WHEN ( lid.reportedcurrencycodeiso IS NOT NULL 
         AND fc.iscurrencydependent = 1 
         AND lid.statementcurrencycodeiso <> lid.reportedcurrencycodeiso 
         AND fc.isflowitem = 0 
         AND p.periodlengthunitcode = 'W' ) THEN lid.lineiteminstancevalue 
* 
cds.dbo.Getaveragefxrate(lid.reportedcurrencycodeiso, lid.statementcurrencycodeiso, 
Dateadd("WEEK", -p.periodlength, p.periodenddate), p.periodenddate) 
  ELSE lid.lineiteminstancevalue 
END )            AS LineItemInstanceValue, 
( CASE 
    WHEN ( lid.reportedcurrencycodeiso IS NOT NULL 
           AND fc.iscurrencydependent = 1 
           AND lid.statementcurrencycodeiso <> lid.reportedcurrencycodeiso 
           AND fc.isflowitem = 1 ) THEN 
lid.adjustedforcorporateactionvalue * 
cds.dbo.Exrate(lid.reportedcurrencycodeiso, lid.statementcurrencycodeiso, p.periodenddate) 
  WHEN ( lid.reportedcurrencycodeiso IS NOT NULL 
         AND fc.iscurrencydependent = 1 
         AND lid.statementcurrencycodeiso <> lid.reportedcurrencycodeiso 
         AND fc.isflowitem = 0 
         AND p.periodlengthunitcode = 'M' ) THEN 
  lid.adjustedforcorporateactionvalue 
* 
cds.dbo.Getaveragefxrate(lid.reportedcurrencycodeiso, lid.statementcurrencycodeiso, 
Dateadd("MONTH", -p.periodlength, p.periodenddate), p.periodenddate) 
  WHEN ( lid.reportedcurrencycodeiso IS NOT NULL 
         AND fc.iscurrencydependent = 1 
         AND lid.statementcurrencycodeiso <> lid.reportedcurrencycodeiso 
         AND fc.isflowitem = 0 
         AND p.periodlengthunitcode = 'W' ) THEN 
  lid.adjustedforcorporateactionvalue 
* 
cds.dbo.Getaveragefxrate(lid.reportedcurrencycodeiso, lid.statementcurrencycodeiso, 
Dateadd("WEEK", -p.periodlength, p.periodenddate), p.periodenddate) 
  ELSE lid.adjustedforcorporateactionvalue 
END )            AS AdjustedForCorporateActionValue, 
lid.reportedcurrencycodeiso, 
lid.xbrlelementid, 
xbrlele.xbrlelementname, 
Cast(NULL AS CHAR(3)), 
Cast(NULL AS DATETIME), 
Cast(NULL AS DATETIME), 
Cast(NULL AS CHAR(1)), 
Cast(NULL AS DATETIME), 
Cast(NULL AS SMALLINT), 
src.dcn, 
src.docformat, 
lid.asreporteditemid, 
ari.docbyteoffset, 
ari.docbytelength, 
ari.bookmark, 
ari.itemdisplayednegativeflag, 
ari.itemscalingfactor, 
ari.itemdisplayedvalue, 
ari.reportedvalue, 
ari.reporteddescription, 
ari.editeddescription, 
src.documentid, 
lid.isderived, 
lid.statementsectioncode, 
IsMissMatchPhysicalMeasureID =0, 
lid.istotal, 
lid.isexcludedfromstandardization, 
si.isdetailed    AS IsDetailedSection, 
si.ispreliminary AS IsPreliminary, 
IsCreditSection =Cast(NULL AS BIT), 
IsCreditFCC =Cast(NULL AS BIT), 
si.isproforma, 
lll.instrumentndaid, 
InterimTypeID = CASE p.periodicitycode 
                  WHEN 'A' THEN 0 
                  WHEN 'S' THEN 2 
                  WHEN 'T' THEN 3 
                  WHEN 'Q' THEN 4 
                END, 
si.isnotcomparabletopriorperiod, 
si.isfundbspecial, 
si.isderived     AS IsDerivedSI, 
lid.systemderivedtypecode 
--FBLog.tasktypeid, 
--FBLog.systemstartdatetime, 
--FBLog.issplit 
FROM   trf.dbo.lineiteminstance LID(nolock) 
       --INNER JOIN #fundbbackwardlog FBLog 
       --        ON FBLog.orgid = lid.orgid 
       --           AND FBLog.periodid = lid.periodid 
       --           AND FBLog.sourceid = lid.sourceid 
       --           AND FBLog.statementtypecode = lid.statementtypecode 
       --           AND FBLog.financialsbucketid = lid.financialsbucketid 
       --           AND FBLog.statementcurrencycodeiso = 
       --               lid.statementcurrencycodeiso 
       --           AND lid.asreporteditemid IS NOT NULL 
       --           AND lid.lineiteminstanceasreporteditemid IS NULL 
       INNER JOIN trf.dbo.statementinstance SI(nolock) 
               ON lid.orgid = si.orgid 
                  AND lid.periodid = si.periodid 
                  AND lid.sourceid = si.sourceid 
                  AND lid.statementtypecode = si.statementtypecode 
                  AND lid.financialsbucketid = si.financialsbucketid 
                  AND lid.statementcurrencycodeiso = si.statementcurrencycodeiso 
       INNER JOIN trf.dbo.period P(nolock) 
               ON lid.orgid = p.orgid 
                  AND lid.periodid = p.periodid 
       INNER JOIN trf.dbo.lineitem LLL(nolock) 
               ON lll.orgid = lid.orgid 
                  AND lll.lineitemid = lid.lineitemid 
       INNER JOIN trf.dbo.financialconcept FC(nolock) 
               ON lll.financialconceptidglobal = fc.financialconceptid 
       LEFT OUTER JOIN trf.dbo.xbrlelement XBRLEle(nolock) 
                    ON lid.xbrlelementid = xbrlele.xbrlelementid 
       INNER JOIN trf.dbo.asreportedinstance ARI(nolock) 
               ON lid.orgid = ari.orgid 
                  AND lid.sourceid = ari.sourceid 
                  AND lid.asreporteditemid = ari.asreporteditemid 
       INNER JOIN trf.dbo.[source] SRC(nolock) 
               ON src.orgid = ari.orgid 
                  AND src.sourceid = ari.sourceid 
WHERE  ari.reportedvalue IS NOT NULL --AND SRC.DCN > ''         --AND SRC.DocFormat > ''         --AND ARI.BookMark > ''       

1 个答案:

答案 0 :(得分:0)

尝试删除查询中调用的函数,例如: Exrate() Getaveragefxrate()

同时更改case语句,其中使用子查询,如下所示。

在查询结束时,使用

left outer join trf.lineitemfundbdescription lifd 
    on lll.orgid = lifd.orgid and lll.lineitemid = lifd.lineitemid

然后案例将更改为

case
    when lifd.orgid is null then lll.lineitemname
    else lifd.lineitemshortdescription End As LineItemName

它会将您的查询微调到一个级别,并相应地为您提供正确的执行计划和建议。

请记住,执行计划不会显示在查询中调用的用户定义函数的计划。