猫狗jamal 2019-06-13 15:06 采纳率: 0%
浏览 328

请问这句select语句怎么优化?

select null id, cast(a.acc_set as varchar(50)) key1, cast(a.years as varchar(50)) key2,cast(a.months as varchar(50)) key3, a.acc_name key4, '' key5, b.distNo bh, b.distName mc, b.distNo, b.ztid, CAST(12 as numeric) lvalue,cast(isnull(zzny,0)-isnull(fsny,0) as numeric) as rvalue,cast(isnull(zzny,0)-isnull(fsny,0)-12 as numeric) as zvalue, '' writer, '' writeDate from ( select acc_set,acc_name,years,months,(years*12+months) fsny from hd_bjAccountnew..balance where exists( select * from (select acc_set,acc_name,MAX(years*12+months) fsny from hd_bjAccountnew..balance where acc_code like '154%' and (ISNULL(debit_month,0)<>0 or ISNULL(credit_month,0)<>0) and exists(select acc_set,acc_code from hd_bjAccountnew..acc_code where lastflag=1 and acc_code like '154%' and exists(select years,acc_set from hd_bjAccountnew..acc_set where acc_set.years=acc_code.years and acc_set.acc_set=acc_code.acc_set) and acc_code.acc_set=balance.acc_set and acc_code.acc_code=balance.acc_code)and (ISNULL(debit_month,0)<>0 or ISNULL(credit_month,0)<>0) group by acc_set,acc_name )a where a.acc_set=balance.acc_set and a.acc_name=balance.acc_name and a.fsny=(balance.years*12+balance.months) ) )a join (select acc_set,years,months,years*12+months zzny from hd_bjAccountnew..acc_set)c on a.acc_set=c.acc_set join zcjy_hd..zt b on a.acc_set=b.BS_ztNo where zzny-fsny>12

  • 写回答

1条回答 默认 最新

  • 憧憬blog 2023-03-15 06:34
    关注

    这个select语句的优化可以从以下几个方面入手:

    1. 建立合适的索引:考虑在关键字段上创建合适的索引,如acc_set、years、months、acc_code等,可以大大提高查询效率。

    2. 减少子查询:尽量减少子查询,可以考虑将子查询中的内容放到join中处理。

    3. 避免使用函数:尽量避免在select语句中使用函数,比如cast、isnull等,这些函数会降低查询的效率。

    4. 分步查询:将查询拆分成多个较小的查询,可以避免对大表进行全表扫描造成的性能影响。

    5. 合理使用where条件:尽量在where条件中使用等值条件进行过滤,避免使用模糊查询和范围查询。

    综上所述,优化这个select语句的关键在于合理的索引建立和子查询的优化,同时需要注意避免使用函数和尽量减小查询结果集的大小,这样可以显著提高查询效率。

    评论

报告相同问题?

悬赏问题

  • ¥15 在线请求openmv与pixhawk 实现实时目标跟踪的具体通讯方法
  • ¥15 八路抢答器设计出现故障
  • ¥15 opencv 无法读取视频
  • ¥15 用matlab 实现通信仿真
  • ¥15 按键修改电子时钟,C51单片机
  • ¥60 Java中实现如何实现张量类,并用于图像处理(不运用其他科学计算库和图像处理库))
  • ¥20 5037端口被adb自己占了
  • ¥15 python:excel数据写入多个对应word文档
  • ¥60 全一数分解素因子和素数循环节位数
  • ¥15 ffmpeg如何安装到虚拟环境