当前位置:网站首页>Slow database query optimization

Slow database query optimization

2022-07-07 00:47:00 Sun Zhongming

You can try to create nested subqueries first , Divide data into small-scale

original


select
  s.school_id,
  s.school_name,
  sum(
    case
      when o.role = 'student' then 1
      else 0
    end
  ) as stu_num
from
  t_school as s 
  left join t_organization as o on s.school_id = o.school_id
 
group by
  s.school_id,
  s.school_name,


      
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.

Replace


select
  s.school_id,
  s.school_name,
  sum(
    case
      when o.role = 'student' then 1
      else 0
    end
  ) as stu_num
from
  t_school as s
  left join (
    select
      school_id sum(
        case
          when o.role = 'student' then 1
          else 0
        end
      ) as stu_num
    from
      t_organization
  ) as o on s.school_id = o.school_id
group by
  s.school_id,
  s.school_name,


      
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
原网站

版权声明
本文为[Sun Zhongming]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202130947247450.html

随机推荐