优化前代码

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的优化的更多相关文章

  1. 撸一段 SQL ? 还是撸一段代码?

    记得刚入公司带我的研发哥们能写一手漂亮的 SQL,搜索准确.执行快.效率高. 配合Web项目中的查询展示数据的需求,基本是分分钟完成任务. 那段时间基本是仰视的态度,每天都去讨教一点手写 SQL 的要 ...

  2. SQL性能优化案例分析

    这段时间做一个SQL性能优化的案例分析, 整理了一下过往的案例,发现一个比较有意思的,拿出来给大家分享. 这个项目是我在项目开展2期的时候才加入的, 之前一期是个金融内部信息门户, 里面有个功能是收集 ...

  3. FP 某段SQL语句执行时间超过1个小时,并报错:ORA-01652: 无法通过 128 (在表空间 TEMPSTG 中) 扩展

    一.出现如下两个错误:1.某一段SQL语句执行时间超过1个小时:2.一个小时后,提示如下错误:ORA-01652: 无法通过 128 (在表空间 TEMPSTG 中) 扩展 temp 段ORA-065 ...

  4. SQL索引优化

    序言数据库的优化方法有很多种,在应用层来说,主要是基于索引的优化.本次秘笈根据实际的工作经验,在研发原来已有的方法的基础上,进行了一些扩充,总结了基于索引的SQL语句优化的降龙十八掌,希望有一天你能用 ...

  5. sql性能优化总结(转)

    网上看到一篇sql优化的文章,整理了一下,发现很不错,虽然知道其中的部分,但是没有这么全面的总结分析过…… 一.   目的 数据库参数进行优化所获得的性能提升全部加起来只占数据库应用系统性能提升的40 ...

  6. SQL性能优化没有那么神秘

    经常听说SQL Server最难的部分是性能优化,不禁让人感到优化这个工作很神秘,这种事情只有高手才能做.很早的时候我在网上看到一位高手写的博客,介绍了SQL优化的问题,从这些内容来看,优化并不都是一 ...

  7. MySQL数据库SQL层级优化

    本篇主涉及MySQL SQL Statements层面的优化. 首先,推荐一个链接为万物之始:http://dev.mysql.com/doc/refman/5.0/en/optimization.h ...

  8. 大约sql声明优化

    最近做的mysql数据库优化,并sql声明优化指南.我写了一个小文件.这种互相鼓励有关! 数据库参数获得的性能优化升级都在一起只占数据库应用系统的性能改进40%左右.其余60%的系统性能提升所有来自相 ...

  9. 数据库 基于索引的SQL语句优化之降龙十八掌(转)

    一篇挺不错的关于SQL语句优化的文章,因不知原始出处,故未作引用说明! 1 前言      客服业务受到SQL语句的影响非常大,在规模比较大的局点,往往因为一个小的SQL语句不够优化,导致数据库性能急 ...

随机推荐

  1. 最简单的基于FFMPEG的音频编码器(PCM编码为AAC)

    http://blog.csdn.net/leixiaohua1020/article/details/25430449 本文介绍一个最简单的基于FFMPEG的音频编码器.该编码器实现了PCM音频采样 ...

  2. SqlServer定时跑一段SQL语句

    1.请把这段SQL语句写成一个存储过程,然后需要在B上面开启 SQL Server Agent服务,如下图: 2.开启完之后,打开数据库管理工具,然后依下图所示,展开“SQL Server Agent ...

  3. iOS Xcode的快捷键

    将一些搜集和经常使用的快捷键记录下来,方便你我. Command +1~ 8: 跳转到导航区的不同位置 Command +0 :显示/隐藏导航区 Command Alt 1~ 6:在不同检测器之间跳转 ...

  4. (翻译)什么是Java的永久代(PermGen)内存泄漏

    http://www.codelast.com/?p=7248 转载请注明出处:http://www.codelast.com/ 本文是我对这篇文章的翻译:What is a PermGen leak ...

  5. [GIF] Colors in GIF Loop Coder

    In this lesson we cover the different methods for defining and animating colors in GIF Loop Coder. f ...

  6. C、Shell、Perl基于Tomcat开发CGI程序环境配置

    基于Tomcat7.0版本号配置CGI开发环境,步聚例如以下: 以我的Tomcat7安装文件夹为例:TOMCA_HOME = /Users/yangxin/Documents/devToos/java ...

  7. linux shell read command-Getting User Input Via Keyboard--ref

    ref:http://bash.cyberciti.biz/guide/Getting_User_Input_Via_Keyboard You can accept input from the ke ...

  8. selenium中定位iframe框

    这是使用谷歌看到的源码.想要往里面输入内容,需要使用js. 这个iframe没有id,不能通过id直接定位到.但可以通用TagName来定位到iframe. WebDriver中定位的代码如下: St ...

  9. 删除主目录下的.ssh目录下文件对boot2docker启动影响

    现象: 1) boot2docker ssh需要输入密码 2) boot2docker start 或 up 需要输入密码 解决方法: 1)删除 /Users/sunzhaoyu/.boot2dock ...

  10. 网页设定固定背景图片(抄袭自百度FM)

    这个新技能,我是从百度FM中学习到的. 在网页中,有一个id为"body-bg"的层, html代码: <div id="body-bg" style=& ...