SQL查询效率

本文通过多个SQL查询实例,展示了如何使用不同的SQL语句来获取所需数据,并对比了不同查询方式的性能开销,帮助读者理解和掌握SQL查询的优化技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 


  1. select * from student inner join sc on student.sno = sc.sno  
  2.  
  3. set statistics io on 
  4.  
  5. select student.sno,student.sname,sc.cno,sc.grade from student join sc on student.sno=sc.sno where student.sdept='计算机' 
  6. --以下这两个sql实现的功能是一样的,只是写的方法不一样  
  7. select sname,cno,grade from student,sc where sc.sno = student.sno and student.sdept='计算机' 
  8. select student.sname,sc.cno,sc.grade from student join sc on student.sno = sc.sno where student.sdept='计算机'   
  9.  
  10. select cno,grade from sc where sc.sno<>990001  
  11. --以下三条sql语句第一句开销最小,说明效率最高,后面两条开销一样  
  12. select grade from sc where sno=(select sno from student where sname='张忠和'and cno = (select cno from course where cname='软件工程')  
  13. --T-SQL写法  
  14. select student.sname,student.sno,sc.grade,sc.cno,course.cname from student,sc,course where student.sno=sc.sno and sc.cno=course.cno and student.sname='张忠和' and course.cname='软件工程' 
  15. --ANSI SQL92写法  
  16. select student.sname,student.sno,sc.grade,sc.cno,course.cname from student   
  17. join sc on student.sno=sc.sno --这里要注意了join与join之间连接的时候没有任何的连接符也没有and之类的  
  18. join course on sc.cno=course.cno   
  19. where student.sname='张忠和' and course.cname='软件工程' 
  20.  
  21. select * into student1 from student  
  22.  
  23. select * into student2 from student union select * from student1  
  24.  
  25. select * from student where sno in (select sno from sc where cno='001')  
  26. select * from student where exists (select * from sc where sno=student.sno and cno='001')  
  27.  
  28. select * from student a where exists (select * from student b where a.sdept=b.sdept and b.sname='张三')  
  29.  
  30. select *from student where sno not in(select sno from sc where cno=001)  
  31.  
  32. select * from student where not exists (select sno from sc where student.sno=sc.sno and cno='001')  

 本文转自sucre03 51CTO博客,原文链接:http://blog.51cto.com/sucre/420273,如需转载请自行联系原作者

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值