一段sql的优化
优化前代码
select *
,ROW_NUMBER() OVER(order by WrongCount desc) as rowId
from(select Quba_IDint,Quba_Number
,
(select top 1 Sqre_AddDateTime
from tbStudentStudyQuestionRecords where Sqre_QubaId=Quba_IDint and Sqre_StudentId=200
and sqre_AnswerJudge='wrong' order by Sqre_AddDateTime desc) as Sqre_AddDateTime,
(select top 1 Sqre_StudyFromType
from tbStudentStudyQuestionRecords where Sqre_QubaId=Quba_IDint and Sqre_StudentId=200
and sqre_AnswerJudge='wrong' order by Sqre_AddDateTime desc) as Sqre_StudyFromType,
COUNT(Quba_IDint) as WrongCount
,COUNT(distinct Expo_KnowPointIDint) as KnpoCount
,
(select top 1 Sqre_MainId
from tbStudentStudyQuestionRecords where Sqre_QubaId=Quba_IDint and Sqre_StudentId=200 and sqre_AnswerJudge='wrong'
order by Sqre_AddDateTime desc) as Sqre_MainId
from tbStudentStudyQuestionRecords
left join tbQuestionBank on Sqre_QubaId=Quba_IDint
left join tbQuestionType on QuTy_Id=Quba_Type
left join tbExamKnowPoint on Expo_ExamIDint=Quba_IDint
where Sqre_StudentId=200 and Quba_SubjectId=15 and Sqre_AnswerJudge='wrong' and QuTy_Name<>'综合题'
group by Quba_IDint,Quba_Number)as t order by quba_idint
优化后代码
select t.*,Sqre_AddDateTime,Sqre_StudyFromType,Sqre_MainId,Sqre_QubaId,
ROW_NUMBER() OVER(order by WrongCount desc) as rowId
from (select Quba_IDint,Quba_Number,QuTy_Name,
COUNT(Quba_IDint) as WrongCount
,COUNT(distinct Expo_KnowPointIDint) as KnpoCount,max(Sqre_Id) as lastId
from tbStudentStudyQuestionRecords
left join tbQuestionBank on Sqre_QubaId=Quba_IDint
left join tbQuestionType on QuTy_Id=Quba_Type
left join tbExamKnowPoint on Expo_ExamIDint=Quba_IDint
where Sqre_StudentId=200 and sqre_AnswerJudge='wrong' and Quba_SubjectId=15 and QuTy_Name<>'综合题'
group by Quba_IDint,Quba_Number,QuTy_Name) as t
left join tbStudentStudyQuestionRecords on t.lastId=Sqre_Id
而已看到优化后执行时间不用1秒
优化思路,第一个sql因为有三个其实查的都是同一条语句,但是因为子查询不能查三列,之前就是这样写的。
所以想着用左连接来优化,先取出一部分,再取出一部分然后连接。
一段sql的优化的更多相关文章
- 撸一段 SQL ? 还是撸一段代码?
记得刚入公司带我的研发哥们能写一手漂亮的 SQL,搜索准确.执行快.效率高. 配合Web项目中的查询展示数据的需求,基本是分分钟完成任务. 那段时间基本是仰视的态度,每天都去讨教一点手写 SQL 的要 ...
- SQL性能优化案例分析
这段时间做一个SQL性能优化的案例分析, 整理了一下过往的案例,发现一个比较有意思的,拿出来给大家分享. 这个项目是我在项目开展2期的时候才加入的, 之前一期是个金融内部信息门户, 里面有个功能是收集 ...
- FP 某段SQL语句执行时间超过1个小时,并报错:ORA-01652: 无法通过 128 (在表空间 TEMPSTG 中) 扩展
一.出现如下两个错误:1.某一段SQL语句执行时间超过1个小时:2.一个小时后,提示如下错误:ORA-01652: 无法通过 128 (在表空间 TEMPSTG 中) 扩展 temp 段ORA-065 ...
- SQL索引优化
序言数据库的优化方法有很多种,在应用层来说,主要是基于索引的优化.本次秘笈根据实际的工作经验,在研发原来已有的方法的基础上,进行了一些扩充,总结了基于索引的SQL语句优化的降龙十八掌,希望有一天你能用 ...
- sql性能优化总结(转)
网上看到一篇sql优化的文章,整理了一下,发现很不错,虽然知道其中的部分,但是没有这么全面的总结分析过…… 一. 目的 数据库参数进行优化所获得的性能提升全部加起来只占数据库应用系统性能提升的40 ...
- SQL性能优化没有那么神秘
经常听说SQL Server最难的部分是性能优化,不禁让人感到优化这个工作很神秘,这种事情只有高手才能做.很早的时候我在网上看到一位高手写的博客,介绍了SQL优化的问题,从这些内容来看,优化并不都是一 ...
- MySQL数据库SQL层级优化
本篇主涉及MySQL SQL Statements层面的优化. 首先,推荐一个链接为万物之始:http://dev.mysql.com/doc/refman/5.0/en/optimization.h ...
- 大约sql声明优化
最近做的mysql数据库优化,并sql声明优化指南.我写了一个小文件.这种互相鼓励有关! 数据库参数获得的性能优化升级都在一起只占数据库应用系统的性能改进40%左右.其余60%的系统性能提升所有来自相 ...
- 数据库 基于索引的SQL语句优化之降龙十八掌(转)
一篇挺不错的关于SQL语句优化的文章,因不知原始出处,故未作引用说明! 1 前言 客服业务受到SQL语句的影响非常大,在规模比较大的局点,往往因为一个小的SQL语句不够优化,导致数据库性能急 ...
随机推荐
- linux文件权限查看及修改-chmod
查看linux文件的权限:ls -l 文件名称 查看linux文件夹的权限:ls -ld 文件夹名称(所在目录) 修改文件及文件夹权限: sudo chmod -(代表类型)×××(所有者)×××(组 ...
- log4j输出信息到mongodb
官网 http://log4mongo.org/display/PUB/Log4mongo+for+Java 保存主机信息,方法等 http://my.oschina.net/chi ...
- Oracle数据库字符串连接方法
转至:http://database.51cto.com/art/201011/232267.htm 和其他数据库系统类似,Oracle字符串连接使用“||”进行字符串拼接,其使用方式和MSSQLSe ...
- grep -P的一个小问题
用grep时,发现一个怪异的问题. 背景:grep -E表示用扩展的正则表达式.grep -P 表示用perl正则表达式,区别:http://www.cnblogs.com/wangkangluo1/ ...
- PAT 1034. Head of a Gang (30)
题目地址:http://pat.zju.edu.cn/contests/pat-a-practise/1034 此题考查并查集的应用,要熟悉在合并的时候存储信息: #include <iostr ...
- Yum出错Error: Cannot find a valid baseurl for repo: base(转)
centos yum 错误 Error: Cannot find a valid baseurl for repo: base 装了个CentOS 6.x,使用yum时出现了下面的错误提示.Loade ...
- [GIF] GIF Loop Coder - Animation Functions
Previous, we animate the item by passing an array to tell the start position and end position. To ma ...
- count(1) count(*)
mysql from t; +---+ | +---+ | | | | +---+ rows in set (0.00 sec) mysql) from t; +----------+ ) | +-- ...
- cglib源码分析(三):Class生成策略
cglib中生成类的工作是由AbstractClassGenerator的create方法使用相应的生成策略完成,具体代码如下: private GeneratorStrategy strategy ...
- Oracle错误代码案例总结及解决方案
引自:http://blog.sina.com.cn/s/blog_9daa54ec0100yr7v.html 常见错误: ORA-00001:违反唯一约束条件(主键错误) ORA-00028:无法连 ...