问答

同样的sql只换了一个条件的值,导致sql执行不出来?

作者:admin 2021-08-16 我要评论

我用的是postgresql,sql中有个province条件,我把province的值更换后,出现了某个值为条件时可以执行成功(速度较快),然后province为其他值时执行失败(长时...

在说正事之前,我要推荐一个福利:你还在原价购买阿里云、腾讯云、华为云服务器吗?那太亏啦!来这里,新购、升级、续费都打折,能够为您省60%的钱呢!2核4G企业级云服务器低至69元/年,点击进去看看吧>>>)

我用的是postgresql,sql中有个province条件,我把province的值更换后,出现了某个值为条件时可以执行成功(速度较快),然后province为其他值时执行失败(长时间执行不出结果),这可能是什么原因?

with first_condition as ( 
                    select  monitor_point, monitor_info, company, count(distinct(month)) as continuous_month  
                    from craw_day_merge where day>='2020-02-01' and day<= '2020-03-31'   and valid_complete < 0.75  and province='安徽'  
                    group by company, monitor_point, monitor_info having sum(exceed_count)=0 
                    and count(distinct(month)) >= '1'           
              )
            select merge.company, 
            merge.monitor_point, merge.monitor_info, merge.province, first_condition.continuous_month
             from craw_day_merge merge inner join first_condition  
            on merge.day>='2019-02-01' and merge.day<= '2020-03-31' and 
            merge.monitor_point=first_condition.monitor_point and merge.monitor_info=first_condition.monitor_info and 
            merge.company=first_condition.company 
            group by  merge.company, 
            merge.monitor_point, merge.monitor_info, merge.province, first_condition.continuous_month;
###

explain + sql可以看到执行过程,注意fliter,recheck index,lossy这些会导致慢查询的因素,换sql写法或者加索引去解决。
如果执行过程没有问题,可以尝试reindex索引或者vaccum这个表。

版权声明:本文转载自网络,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。本站转载出于传播更多优秀技术知识之目的,如有侵权请联系QQ/微信:153890879删除

相关文章
  • 同样的sql只换了一个条件的值,导致sql

    同样的sql只换了一个条件的值,导致sql

  • v-for 循环两个不一样的tr

    v-for 循环两个不一样的tr

  • MongoDB aggregate $sum 数据不对,Int

    MongoDB aggregate $sum 数据不对,Int

  • docker搭建了PHP和redis环境,但PHP在

    docker搭建了PHP和redis环境,但PHP在

腾讯云代理商
海外云服务器