以下查询可能的索引

时间:2018-05-12 18:25:04

标签: postgresql

select uid, user_id, email, mno, orgnztn, status, utype, state,
       to_char(cdate,'yyyy-mm-dd hh:mm:ss') as cdate
from schema.table_1
where puser in (with recursive rel_tree as (
                    select user_id, puser,1 as level,uid
                    from schema.table_1
                    where puser = 9
                    union all
                    select c.user_id, c.puser, p.level + 1 as level ,p.uid
                    from schema.table_1 c
                       join rel_tree p on c.puser = p.uid
                   )
                select uid
                from rel_tree
                union select 9
               )
group by uid, user_id, email, mno, orgnztn, status, utype, state,
         to_char(cdate,'yyyy-mm-dd hh:mm:ss');

1 个答案:

答案 0 :(得分:0)

可能会稍微快一点:

with recursive rel_tree as (
   select
      uid, user_id, email, mno, orgnztn, status, utype, state,
      to_char(cdate,'yyyy-mm-dd hh:mm:ss') as cdate,
      puser,1 as level
   from schema.table_1
   where puser = 9
   union all
   select
      uid, user_id, email, mno, orgnztn, status, utype, state,
      to_char(cdate,'yyyy-mm-dd hh:mm:ss') as cdate,
      puser, p.level + 1 as level
   from schema.table_1 c
   join rel_tree p on c.puser = p.uid
    )
select uid, user_id, email, mno, orgnztn, status, utype, state,
    cdate
from rel_tree
group by uid, user_id, email, mno, orgnztn, status, utype, state,
    cdate;

你想要的索引是

CREATE INDEX  table_1_puser on  schema.table_1(puser);