【问题标题】:Optimize nested sql query优化嵌套sql查询
【发布时间】:2018-07-24 02:17:15
【问题描述】:

我正在尝试优化以下每 50 秒运行一次的查询以提高性能。

select * from event se1  
where id = (select min(id) from event se2 where
se1.sub_id=se2.sub_id and se2.state = 'PENDING' and se2.il_flag= true)  
and not exists (select id from event se2 where se1.sub_id=se2.sub_id
and se2.state in ('ACCEPTED', 'FAILED', 'INPROCESS'))

关于提出更好的查询以提高其性能的任何方向? (postgres 9.6)。感谢帮助。

事件表

Id              sub_id              state       idl_flag 
1                 23009            Pending        true
2                 23009            Accepted       true
3                 23009            Pending        true
4                 23009            Pending        true
5                 23010            Pending        true
6                 23010            Pending        true
7                 23011            Pending        true
8                 23012            Pending        true

上表应该返回

       5                 23010            Pending       true
       7                 23011            Pending       true

【问题讨论】:

标签: sql postgresql query-optimization greatest-n-per-group


【解决方案1】:

我提出了这个查询,欢迎任何进一步改进查询的建议。

select se1.* from event se1 join 
  (select sub_id,min(id) as id from event  where state='PENDING' and
           il_flag=false group by sub_id)se2 
   on se1.id=se2.id 
  left join (select sub_id from 
  event se3 where se3.state in ('ACCEPTED', 'FAILED', 'INPROCESS'))se4 
on se1.sub_id=se4.sub_id where se4.sub_id is null

【讨论】:

    【解决方案2】:

    您在回答中所做的事情与您在问题中所做的不同 - 原始答案中没有关于“错误”状态的特殊情况。

    根据您最初的问题,我为您做了一些小技巧,它的运行速度并不比您原来的快,但我讨厌嵌套子查询。我已将其全部考虑到 CTE 中,因此您可以选择所需的状态,只是为了向您展示不同的方法。这可能只是个人喜好,但恕我直言,我的版本比原版更容易阅读!

    SQL Fiddle - CTE Example

    【讨论】:

      【解决方案3】:

      您可以使用事件表进行连接,但对这部分使用 is null 条件:

      and not exists (select id from event se2 where se1.sub_id=se2.sub_id
      and se2.state in ('ACCEPTED', 'FAILED', 'INPROCESS'))
      

      请检查How to write "not in ()" sql query using join

      但无论如何尽量避免使用嵌套查询。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-11-19
        • 1970-01-01
        • 2014-04-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多