C#实习生 2022-03-17 11:18 采纳率: 60%
浏览 35
已结题

百万级别sql server 数据库 sql语句优化

这条sql语句是查询每月最后一天的各项值,现在查询时间为9秒左右,表没有加索引,请朋友们给优化优化到2秒左右


select A.ID, 
       A.BZ, A.JH, A.JB, A.YT, A.SFXT, A.ZDXYY, A.ZYNR, A.DXFL, A.TSQKMS, A.XXXT, A.SFTSZL,
       A.SJRQ, A.WJRQ,A.TCRQ, B.GLYMC, C.DEPTNAME, 
       datediff(day, SJRQ, WJRQ) DXZTS, 
       MAX(case when year(D.RQ) = 2022 and month(D.RQ) = 1 and day(D.RQ) = 31 then D.DYM end) DYM1, 
       MAX(case when year(D.RQ) = 2022 and month(D.RQ) = 1 and day(D.RQ) = 31 then D.RY end) RY1, 
       MAX(case when year(D.RQ) = 2022 and month(D.RQ) = 1 and day(D.RQ) = 31 then D.RYOU end) RYOU1, 
       MAX(case when year(D.RQ) = 2022 and month(D.RQ) = 1 and day(D.RQ) = 31 then D.HS end) HS1, 
       MAX(case when year(D.RQ) = 2022 and month(D.RQ) = 1 and day(D.RQ) = 31 then D.RZY end) LZY1, 
       MAX(case when year(D.RQ) = 2022 and month(D.RQ) = 2 and day(D.RQ) = 28 then D.DYM end) DYM2, 
       MAX(case when year(D.RQ) = 2022 and month(D.RQ) = 2 and day(D.RQ) = 28 then D.RY end) RY2, 
       MAX(case when year(D.RQ) = 2022 and month(D.RQ) = 2 and day(D.RQ) = 28 then D.RYOU end) RYOU2, 
       MAX(case when year(D.RQ) = 2022 and month(D.RQ) = 2 and day(D.RQ) = 28 then D.HS end) HS2, 
       MAX(case when year(D.RQ) = 2022 and month(D.RQ) = 2 and day(D.RQ) = 28 then D.RZY end) LZY2, 
       MAX(case when year(D.RQ) = 2022 and month(D.RQ) = 3 and day(D.RQ) = 31 then D.DYM end) DYM3, 
       MAX(case when year(D.RQ) = 2022 and month(D.RQ) = 3 and day(D.RQ) = 31 then D.RY end) RY3, 
       MAX(case when year(D.RQ) = 2022 and month(D.RQ) = 3 and day(D.RQ) = 31 then D.RYOU end) RYOU3, 
       MAX(case when year(D.RQ) = 2022 and month(D.RQ) = 3 and day(D.RQ) = 31 then D.HS end) HS3, 
       MAX(case when year(D.RQ) = 2022 and month(D.RQ) = 3 and day(D.RQ) = 31 then D.RZY end) LZY3, 
       MAX(case when year(D.RQ) = 2022 and month(D.RQ) = 4 and day(D.RQ) = 30 then D.DYM end) DYM4, 
       MAX(case when year(D.RQ) = 2022 and month(D.RQ) = 4 and day(D.RQ) = 30 then D.RY end) RY4, 
       MAX(case when year(D.RQ) = 2022 and month(D.RQ) = 4 and day(D.RQ) = 30 then D.RYOU end) RYOU4, 
       MAX(case when year(D.RQ) = 2022 and month(D.RQ) = 4 and day(D.RQ) = 30 then D.HS end) HS4, 
       MAX(case when year(D.RQ) = 2022 and month(D.RQ) = 4 and day(D.RQ) = 30 then D.RZY end) LZY4, 
       MAX(case when year(D.RQ) = 2022 and month(D.RQ) = 5 and day(D.RQ) = 31 then D.DYM end) DYM5, 
       MAX(case when year(D.RQ) = 2022 and month(D.RQ) = 5 and day(D.RQ) = 31 then D.RY end) RY5, 
       MAX(case when year(D.RQ) = 2022 and month(D.RQ) = 5 and day(D.RQ) = 31 then D.RYOU end) RYOU5, 
       MAX(case when year(D.RQ) = 2022 and month(D.RQ) = 5 and day(D.RQ) = 31 then D.HS end) HS5, 
       MAX(case when year(D.RQ) = 2022 and month(D.RQ) = 5 and day(D.RQ) = 31 then D.RZY end) LZY5, 
       MAX(case when year(D.RQ) = 2022 and month(D.RQ) = 6 and day(D.RQ) = 30 then D.DYM end) DYM6, 
       MAX(case when year(D.RQ) = 2022 and month(D.RQ) = 6 and day(D.RQ) = 30 then D.RY end) RY6, 
       MAX(case when year(D.RQ) = 2022 and month(D.RQ) = 6 and day(D.RQ) = 30 then D.RYOU end) RYOU6, 
       MAX(case when year(D.RQ) = 2022 and month(D.RQ) = 6 and day(D.RQ) = 30 then D.HS end) HS6, 
       MAX(case when year(D.RQ) = 2022 and month(D.RQ) = 6 and day(D.RQ) = 30 then D.RZY end) LZY6, 
       MAX(case when year(D.RQ) = 2022 and month(D.RQ) = 7 and day(D.RQ) = 31 then D.DYM end) DYM7, 
       MAX(case when year(D.RQ) = 2022 and month(D.RQ) = 7 and day(D.RQ) = 31 then D.RY end) RY7, 
       MAX(case when year(D.RQ) = 2022 and month(D.RQ) = 7 and day(D.RQ) = 31 then D.RYOU end) RYOU7, 
       MAX(case when year(D.RQ) = 2022 and month(D.RQ) = 7 and day(D.RQ) = 31 then D.HS end) HS7, 
       MAX(case when year(D.RQ) = 2022 and month(D.RQ) = 7 and day(D.RQ) = 31 then D.RZY end) LZY7, 
       MAX(case when year(D.RQ) = 2022 and month(D.RQ) = 8 and day(D.RQ) = 31 then D.DYM end) DYM8, 
       MAX(case when year(D.RQ) = 2022 and month(D.RQ) = 8 and day(D.RQ) = 31 then D.RY end) RY8, 
       MAX(case when year(D.RQ) = 2022 and month(D.RQ) = 8 and day(D.RQ) = 31 then D.RYOU end) RYOU8, 
       MAX(case when year(D.RQ) = 2022 and month(D.RQ) = 8 and day(D.RQ) = 31 then D.HS end) HS8, 
       MAX(case when year(D.RQ) = 2022 and month(D.RQ) = 8 and day(D.RQ) = 31 then D.RZY end) LZY8, 
       MAX(case when year(D.RQ) = 2022 and month(D.RQ) = 9 and day(D.RQ) = 30 then D.DYM end) DYM9, 
       MAX(case when year(D.RQ) = 2022 and month(D.RQ) = 9 and day(D.RQ) = 30 then D.RY end) RY9, 
       MAX(case when year(D.RQ) = 2022 and month(D.RQ) = 9 and day(D.RQ) = 30 then D.RYOU end) RYOU9, 
       MAX(case when year(D.RQ) = 2022 and month(D.RQ) = 9 and day(D.RQ) = 30 then D.HS end) HS9, 
       MAX(case when year(D.RQ) = 2022 and month(D.RQ) = 9 and day(D.RQ) = 30 then D.RZY end) LZY9, 
       MAX(case when year(D.RQ) = 2022 and month(D.RQ) = 10 and day(D.RQ) = 31 then D.DYM end) DYM10, 
       MAX(case when year(D.RQ) = 2022 and month(D.RQ) = 10 and day(D.RQ) = 31 then D.RY end) RY10, 
       MAX(case when year(D.RQ) = 2022 and month(D.RQ) = 10 and day(D.RQ) = 31 then D.RYOU end) RYOU10, 
       MAX(case when year(D.RQ) = 2022 and month(D.RQ) = 10 and day(D.RQ) = 31 then D.HS end) HS10, 
       MAX(case when year(D.RQ) = 2022 and month(D.RQ) = 10 and day(D.RQ) = 31 then D.RZY end) LZY10, 
       MAX(case when year(D.RQ) = 2022 and month(D.RQ) = 11 and day(D.RQ) = 30 then D.DYM end) DYM11, 
       MAX(case when year(D.RQ) = 2022 and month(D.RQ) = 11 and day(D.RQ) = 30 then D.RY end) RY11, 
       MAX(case when year(D.RQ) = 2022 and month(D.RQ) = 11 and day(D.RQ) = 30 then D.RYOU end) RYOU11, 
       MAX(case when year(D.RQ) = 2022 and month(D.RQ) = 11 and day(D.RQ) = 30 then D.HS end) HS11, 
       MAX(case when year(D.RQ) = 2022 and month(D.RQ) = 11 and day(D.RQ) = 30 then D.RZY end) LZY11, 
       MAX(case when year(D.RQ) = 2022 and month(D.RQ) = 12 and day(D.RQ) = 31 then D.DYM end) DYM12, 
       MAX(case when year(D.RQ) = 2022 and month(D.RQ) = 12 and day(D.RQ) = 31 then D.RY end) RY12, 
       MAX(case when year(D.RQ) = 2022 and month(D.RQ) = 12 and day(D.RQ) = 31 then D.RYOU end) RYOU12, 
       MAX(case when year(D.RQ) = 2022 and month(D.RQ) = 12 and day(D.RQ) = 31 then D.HS end) HS12, 
       MAX(case when year(D.RQ) = 2022 and month(D.RQ) = 12 and day(D.RQ) = 31 then D.RZY end) LZY12
       from DXTJ A 
       left join DEPT_JBXXB C WITH (FORCESEEK) on A.SGDW = C.DEPEID 
       left join CSJING_SCSJB D on A.JH = D.JH 
       left join GLY_JBXXB B on A.GLY = B.ID 
       where JB = '油井' 
       GROUP BY A.ID, A.JH, JB, C.DEPTNAME, YT, SFXT, ZDXYY, ZYNR, DXFL, TSQKMS, XXXT, SFTSZL, B.GLYMC, SJRQ, WJRQ, TCRQ, A.BZ 
       OPTION (ORDER GROUP)

  • 写回答

1条回答 默认 最新

  • DarkAthena ORACLE应用及数据库设计方案咨询师 2022-03-17 13:53
    关注

    加个where条件先过滤出需要的数据,其实上次就和你说过了

    where JB = '油井' and D.RQ =EOMONTH(D.RQ)
    

    如果sqlserver版本老了没有EOMONTH这个函数的话,可以使用其他的替代方式,比如

    dateadd(month, datediff(month, 0, dateadd(month, 1, D.RQ)), -1)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 4月21日
  • 已采纳回答 4月21日
  • 修改了问题 3月17日
  • 修改了问题 3月17日
  • 展开全部

悬赏问题

  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用