SQL Sum语句很慢

时间:2014-06-26 15:20:59

标签: sql vb.net

SQL新手并且遇到此代码问题。似乎“SUM”函数真的很慢。如果日期范围超过5天,它会超时。我必须循环这115次,并涵盖180天的日期范围。关于我能做些什么来加速它的任何想法?字符串在SQL企业管理器中也会超时。 我听说过使用“pivot”命令,但不知道这是答案还是如何实现它。 TIA

strSQLRead = "SELECT SUM(BatchRecordDetail.BRD_Actual) AS LbsBatched, BatchRun.BAT_CoatingLBS AS     CoatingLbs, " _
& "BatchRun.BAT_ID AS RunID " _
& "FROM BatchRun " _
& "INNER JOIN BatchRecordMaster ON BatchRun.BAT_HmiRunID = BatchRecordMaster.BRM_BatchRunID " _
& "AND BatchRun.BAT_Area = BatchRecordMaster.BRM_Area " _
& "INNER JOIN BatchRecordDetail ON BatchRecordMaster.BRM_Area = BatchRecordDetail.BRD_Area " _
& "AND BatchRecordMaster.BRM_HmiBatchID = BatchRecordDetail.BRD_BatchRecordID " _
& "WHERE (BatchRun.BAT_ExtruderEnd BETWEEN CONVERT(DATETIME, '" & Format(ToDate, "yyyy-MM-dd")     & " 00:00:00', 102) " _
& "AND CONVERT(DATETIME, '" & Format(FromDate, "yyyy-MM-dd") & " 00:00:00', 102)) AND     (BatchRun.BAT_RunComplete = 1) " _
& "AND BatchRun.BAT_FormulaCode = '" & sFormula(i) & "'" _
& "GROUP BY BatchRun.BAT_FormulaCode,BatchRun.BAT_ID, BatchRun.BAT_CoatingLBS " _
& "HAVING (BatchRun.BAT_CoatingLBS Is Not Null)"

这是在代码中捕获的字符串。只是想知道是否有人有任何想法。 THX

SELECT Sum(batchrecorddetail.brd_actual) AS LbsBatched, 
       batchrun.bat_coatinglbs           AS CoatingLbs, 
       batchrun.bat_id                   AS RunID 
FROM   batchrun 
       INNER JOIN batchrecordmaster 
               ON batchrun.bat_hmirunid = batchrecordmaster.brm_batchrunid 
                  AND batchrun.bat_area = batchrecordmaster.brm_area 
       INNER JOIN batchrecorddetail 
               ON batchrecordmaster.brm_area = batchrecorddetail.brd_area 
                  AND batchrecordmaster.brm_hmibatchid = 
                      batchrecorddetail.brd_batchrecordid 
WHERE  ( batchrun.bat_extruderend BETWEEN 
         CONVERT(DATETIME, '2014-06-26 00:00:00', 102) 
                AND 
                CONVERT(DATETIME, '2014-06-26 00:00:00' 
                , 102) ) 
       AND ( batchrun.bat_runcomplete = 1 ) 
       AND batchrun.bat_formulacode = '3100007' 
GROUP  BY batchrun.bat_formulacode, 
          batchrun.bat_id, 
          batchrun.bat_coatinglbs 
HAVING ( batchrun.bat_coatinglbs IS NOT NULL ) 

我无法发布图片因为我没有足够的声望点。 我如何展示还有什么需要帮助回答这个问题?

0 个答案:

没有答案