非原创,看到以后发现自己在很多时候都忽略了这个问题一些基本的SQL优化
2012-08-16 10:55
513 查看
1、子查询的用法
子查询是一个 select 查询,它嵌套在 SELECT、INSERT、UPDATE、DELETE 语句或其它子查询中。任何允许使用表达式的地方都可以使用子查询。子查询可以使我们的编程灵活多样,可以用来实现一些特殊的功能。但是在性能上,往往一个不合适的子查询用法会形成一个性能瓶颈。
A、NOT IN、NOT EXISTS的相关子查询可以改用LEFT JOIN代替写法。比如:
select pub_name
from publishers
where pub_id not in
(select pub_id
from titles
where type = 'business')
--可以改写成:
select a.pub_name
from publishers a left join titles b
on b.type = 'business' and
a.pub_id=b. pub_id
where b.pub_id is null
select title
from titles
where not exists
(select title_id
from sales
where title_id = titles.title_id)
--可以改写成:
select title
from titles left join sales
on sales.title_id = titles.title_id
where sales.title_id is null
B、 如果保证子查询没有重复 ,IN、EXISTS的相关子查询可以用INNER JOIN 代替。比如:
select pub_name
from publishers
where pub_id in
(select pub_id
from titles
where type = 'business')
--可以改写成:
select distinct a.pub_name
from publishers a inner join titles b
on b.type = 'business' and
a.pub_id=b. pub_id
C、IN的相关子查询用EXISTS代替,比如
select pub_name
from publishers
where pub_id in
(select pub_id
from titles
where type = 'business')
--可以用下面语句代替:
select pub_name
from publishers
where exists
(select 1
from titles
where type = 'business' and
pub_id= publishers.pub_id)
D、不要用COUNT(*)的子查询判断是否存在记录,最好用LEFT JOIN或者EXISTS,比如有人写这样的语句:
select job_desc from jobs
where (select count(*) from employee where job_id=jobs.job_id)=0
--应该改成:
select jobs.job_desc from jobs left join employee
on employee.job_id=jobs.job_id
where employee.emp_id is null
select job_desc from jobs
where (select count(*) from employee where job_id=jobs.job_id)<>0
--应该改成:
select job_desc from jobs
where exists (select 1 from employee where job_id=jobs.job_id)
作为程序员还应该注意:
1、注意、关心各表的数据量。
2、编码过程和单元测试过程尽量用数据量较大的数据库测试,最好能用实际数据测试。
3、每个SQL语句尽量简单
4、不要频繁更新有触发器的表的数据
5、注意数据库函数的限制以及其性能1、子查询的用法
子查询是一个 select 查询,它嵌套在 SELECT、INSERT、UPDATE、DELETE 语句或其它子查询中。任何允许使用表达式的地方都可以使用子查询。子查询可以使我们的编程灵活多样,可以用来实现一些特殊的功能。但是在性能上,往往一个不合适的子查询用法会形成一个性能瓶颈。
A、NOT IN、NOT EXISTS的相关子查询可以改用LEFT JOIN代替写法。比如:
select pub_name
from publishers
where pub_id not in
(select pub_id
from titles
where type = 'business')
--可以改写成:
select a.pub_name
from publishers a left join titles b
on b.type = 'business' and
a.pub_id=b. pub_id
where b.pub_id is null
select title
from titles
where not exists
(select title_id
from sales
where title_id = titles.title_id)
--可以改写成:
select title
from titles left join sales
on sales.title_id = titles.title_id
where sales.title_id is null
B、 如果保证子查询没有重复 ,IN、EXISTS的相关子查询可以用INNER JOIN 代替。比如:
select pub_name
from publishers
where pub_id in
(select pub_id
from titles
where type = 'business')
--可以改写成:
select distinct a.pub_name
from publishers a inner join titles b
on b.type = 'business' and
a.pub_id=b. pub_id
C、IN的相关子查询用EXISTS代替,比如
select pub_name
from publishers
where pub_id in
(select pub_id
from titles
where type = 'business')
--可以用下面语句代替:
select pub_name
from publishers
where exists
(select 1
from titles
where type = 'business' and
pub_id= publishers.pub_id)
D、不要用COUNT(*)的子查询判断是否存在记录,最好用LEFT JOIN或者EXISTS,比如有人写这样的语句:
select job_desc from jobs
where (select count(*) from employee where job_id=jobs.job_id)=0
--应该改成:
select jobs.job_desc from jobs left join employee
on employee.job_id=jobs.job_id
where employee.emp_id is null
select job_desc from jobs
where (select count(*) from employee where job_id=jobs.job_id)<>0
--应该改成:
select job_desc from jobs
where exists (select 1 from employee where job_id=jobs.job_id)
作为程序员还应该注意:
1、注意、关心各表的数据量。
2、编码过程和单元测试过程尽量用数据量较大的数据库测试,最好能用实际数据测试。
3、每个SQL语句尽量简单
4、不要频繁更新有触发器的表的数据
5、注意数据库函数的限制以及其性能
子查询是一个 select 查询,它嵌套在 SELECT、INSERT、UPDATE、DELETE 语句或其它子查询中。任何允许使用表达式的地方都可以使用子查询。子查询可以使我们的编程灵活多样,可以用来实现一些特殊的功能。但是在性能上,往往一个不合适的子查询用法会形成一个性能瓶颈。
A、NOT IN、NOT EXISTS的相关子查询可以改用LEFT JOIN代替写法。比如:
select pub_name
from publishers
where pub_id not in
(select pub_id
from titles
where type = 'business')
--可以改写成:
select a.pub_name
from publishers a left join titles b
on b.type = 'business' and
a.pub_id=b. pub_id
where b.pub_id is null
select title
from titles
where not exists
(select title_id
from sales
where title_id = titles.title_id)
--可以改写成:
select title
from titles left join sales
on sales.title_id = titles.title_id
where sales.title_id is null
B、 如果保证子查询没有重复 ,IN、EXISTS的相关子查询可以用INNER JOIN 代替。比如:
select pub_name
from publishers
where pub_id in
(select pub_id
from titles
where type = 'business')
--可以改写成:
select distinct a.pub_name
from publishers a inner join titles b
on b.type = 'business' and
a.pub_id=b. pub_id
C、IN的相关子查询用EXISTS代替,比如
select pub_name
from publishers
where pub_id in
(select pub_id
from titles
where type = 'business')
--可以用下面语句代替:
select pub_name
from publishers
where exists
(select 1
from titles
where type = 'business' and
pub_id= publishers.pub_id)
D、不要用COUNT(*)的子查询判断是否存在记录,最好用LEFT JOIN或者EXISTS,比如有人写这样的语句:
select job_desc from jobs
where (select count(*) from employee where job_id=jobs.job_id)=0
--应该改成:
select jobs.job_desc from jobs left join employee
on employee.job_id=jobs.job_id
where employee.emp_id is null
select job_desc from jobs
where (select count(*) from employee where job_id=jobs.job_id)<>0
--应该改成:
select job_desc from jobs
where exists (select 1 from employee where job_id=jobs.job_id)
作为程序员还应该注意:
1、注意、关心各表的数据量。
2、编码过程和单元测试过程尽量用数据量较大的数据库测试,最好能用实际数据测试。
3、每个SQL语句尽量简单
4、不要频繁更新有触发器的表的数据
5、注意数据库函数的限制以及其性能1、子查询的用法
子查询是一个 select 查询,它嵌套在 SELECT、INSERT、UPDATE、DELETE 语句或其它子查询中。任何允许使用表达式的地方都可以使用子查询。子查询可以使我们的编程灵活多样,可以用来实现一些特殊的功能。但是在性能上,往往一个不合适的子查询用法会形成一个性能瓶颈。
A、NOT IN、NOT EXISTS的相关子查询可以改用LEFT JOIN代替写法。比如:
select pub_name
from publishers
where pub_id not in
(select pub_id
from titles
where type = 'business')
--可以改写成:
select a.pub_name
from publishers a left join titles b
on b.type = 'business' and
a.pub_id=b. pub_id
where b.pub_id is null
select title
from titles
where not exists
(select title_id
from sales
where title_id = titles.title_id)
--可以改写成:
select title
from titles left join sales
on sales.title_id = titles.title_id
where sales.title_id is null
B、 如果保证子查询没有重复 ,IN、EXISTS的相关子查询可以用INNER JOIN 代替。比如:
select pub_name
from publishers
where pub_id in
(select pub_id
from titles
where type = 'business')
--可以改写成:
select distinct a.pub_name
from publishers a inner join titles b
on b.type = 'business' and
a.pub_id=b. pub_id
C、IN的相关子查询用EXISTS代替,比如
select pub_name
from publishers
where pub_id in
(select pub_id
from titles
where type = 'business')
--可以用下面语句代替:
select pub_name
from publishers
where exists
(select 1
from titles
where type = 'business' and
pub_id= publishers.pub_id)
D、不要用COUNT(*)的子查询判断是否存在记录,最好用LEFT JOIN或者EXISTS,比如有人写这样的语句:
select job_desc from jobs
where (select count(*) from employee where job_id=jobs.job_id)=0
--应该改成:
select jobs.job_desc from jobs left join employee
on employee.job_id=jobs.job_id
where employee.emp_id is null
select job_desc from jobs
where (select count(*) from employee where job_id=jobs.job_id)<>0
--应该改成:
select job_desc from jobs
where exists (select 1 from employee where job_id=jobs.job_id)
作为程序员还应该注意:
1、注意、关心各表的数据量。
2、编码过程和单元测试过程尽量用数据量较大的数据库测试,最好能用实际数据测试。
3、每个SQL语句尽量简单
4、不要频繁更新有触发器的表的数据
5、注意数据库函数的限制以及其性能
相关文章推荐
- 非原创,看到以后发现自己在很多时候都忽略了这个问题一些基本的SQL优化
- 今天在使用iscroll4 做一个简单触屏滚动demo,发现上下拖动的时候总是会回弹,不能看到下面的内容.这个问题苦恼了很久,终于解决
- 新入的Android工程很多时候都会遇到这样的问题,R.mainactivity.xml报错,一看原来发现是R类访问不到我们的资源文件在这个时候怎么解决呢?
- 浏览者不需要在这个上面浪费时间,net一些问题解决方法--这是自己写的
- sql server查询时候注意的基本优化常识
- [原创]MySQL中SQL优化和架构设计的一些简单想法
- 因为涉及到泄密问题,个人博客上SQL优化部分的很多经典案例被删除
- 今天去面试,才发现自己忽略了很多东西
- 自己总结的一些SQL语句的基本知识
- 端口占用问题,今天学习的时候又出现了这个问题,找了度娘,特此记录以下。第一次写博客,希望自己能坚持下来。大家共同进步
- Curator是Netflix开源的一套ZooKeeper客户端框架. Netflix在使用ZooKeeper的过程中发现ZooKeeper自带的客户端太底层, 应用方在使用的时候需要自己处理很多事情
- 因为涉及到泄密问题,个人博客上SQL优化部分的很多经典案例被删除
- 自己移植程序的时候遇到的一些问题
- 基于maven的springMvc+Mybatis,搭建项目的时候发现一些问题这里例举下
- 在很多论坛上看到了有人抱怨Flash CS5中,代码提示(Code hint)有时不显示的问题,这个问题可能有很多原因,我整理了几个解决办法,有可能可以帮助到你~
- 今天是第一天所以写两个,这个是总结以前学到的一些知识看看自己以后的学习方向
- 调用一些自己写的类的时候遇到的问题
- 练习webpack遇到的一些问题以及解决办法,供自己以后参考
- 自己做项目的时候遇到的一些简单问题
- LinkedList基本用法,真得很详细,借鉴了,以后再遇到类似的问题啊,不仅要收集起来博文,更要自己争取写出来!