Sql语句查询优化总结:
2007-02-05 23:59
866 查看
Sql语句查询优化总结:
Date:2007.1.10
t_supervise_job_assign表如下:
id | building_id
1 | 101
2 | 101
3 | 102
4 | 103
5 | 103
为了取出(select count(*) from t_supervise_job_assign where building_id=a.id)同一building_id不同id的building_id数量,得到如下的表ct:
building_id | total
101 2
102 1
103 2
下面的查询用了子查询,通过building_id=a.id连外层的表,这样的查询效率超低,甚至查死数据库:
select distinct a.id,a.building_name,f.record_person ,d.address,d.begin_date,
(select count(*) from t_supervise_job_assign where building_id=a.id) status,
a.create_user
from t_building a ,
t_building_info d,t_monitor_plan e,t_monitor_record f
where a.id=d.building_id
AND e.f1_id=a.id
AND e.stream_id=f.f1_stream_id
AND a.modify_flag<>'D'
AND a.building_name like '%顺德%' order by status,d.begin_date desc
修改后:
主要优化这条语句:
select count(*) from t_supervise_job_assign where building_id=a.id
优化后:
select t.building_id,count(t.id) as total from t_supervise_job_assign t group by t.building_id
整合为一个表:
再得到最终优化后的查询语句:
select distinct a.id,a.building_name,f.record_person ,d.address,d.begin_date,
ct.status,
a.create_user
from t_building a ,
(select t.building_id,count(t.id) as status from t_supervise_job_assign t group by t.building_id) ct,
t_building_info d,t_monitor_plan e,t_monitor_record f
where a.id=d.building_id
AND e.f1_id=a.id
AND ct.building_id(+)=a.id
AND e.stream_id=f.f1_stream_id
AND a.modify_flag<>'D'
AND a.building_name like '%顺德%' order by status,d.begin_date desc
Date:2007.1.10
t_supervise_job_assign表如下:
id | building_id
1 | 101
2 | 101
3 | 102
4 | 103
5 | 103
为了取出(select count(*) from t_supervise_job_assign where building_id=a.id)同一building_id不同id的building_id数量,得到如下的表ct:
building_id | total
101 2
102 1
103 2
下面的查询用了子查询,通过building_id=a.id连外层的表,这样的查询效率超低,甚至查死数据库:
select distinct a.id,a.building_name,f.record_person ,d.address,d.begin_date,
(select count(*) from t_supervise_job_assign where building_id=a.id) status,
a.create_user
from t_building a ,
t_building_info d,t_monitor_plan e,t_monitor_record f
where a.id=d.building_id
AND e.f1_id=a.id
AND e.stream_id=f.f1_stream_id
AND a.modify_flag<>'D'
AND a.building_name like '%顺德%' order by status,d.begin_date desc
修改后:
主要优化这条语句:
select count(*) from t_supervise_job_assign where building_id=a.id
优化后:
select t.building_id,count(t.id) as total from t_supervise_job_assign t group by t.building_id
整合为一个表:
再得到最终优化后的查询语句:
select distinct a.id,a.building_name,f.record_person ,d.address,d.begin_date,
ct.status,
a.create_user
from t_building a ,
(select t.building_id,count(t.id) as status from t_supervise_job_assign t group by t.building_id) ct,
t_building_info d,t_monitor_plan e,t_monitor_record f
where a.id=d.building_id
AND e.f1_id=a.id
AND ct.building_id(+)=a.id
AND e.stream_id=f.f1_stream_id
AND a.modify_flag<>'D'
AND a.building_name like '%顺德%' order by status,d.begin_date desc
相关文章推荐
- sql语句查询优化总结,建议及写法技巧(汇总)
- SQL高性能查询优化语句(总结)
- SQL查询语句优化的实用方法总结
- SQL查询语句优化的实用方法总结
- 关于过去工作的一些总结:常用sql语句以及数据库查询优化
- T-SQL查询语句常用优化技巧总结
- sql 语句中的时间查询总结
- ORACLE EXPLAIN PLAN的总结 (优化SQL语句)
- SQL优化查询建议(个人总结 仅供参考)
- sql查询语句优化
- 浅谈MySQL中优化sql语句查询常用的30种方法
- 优化SQL查询:如何写出高性能SQL语句
- 解析Oracle数据扫描 Oracle SQL查询优化 总结篇
- SQL查询语句常用总结
- SQL查询语句总结
- mysql5.7官网直译SQL语句优化--where条件查询优化
- sql查询语句优化需要注意的几点
- sql查询执行过程,语句优化,查询优化
- Mysql常用30种SQL查询语句优化方法