A section of SQL optimization

Source: Internet
Author: User

Pre-optimization Code

Select *,row_number () over (order by wrongcount Desc) as Rowidfrom (select Quba_idint,quba_number, sel ECT top 1 sqre_adddatetime from Tbstudentstudyquestionrecords where Sqre_qubaid=quba_idint and sqre_studentid=  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 Wrongcou Nt,count (distinct expo_knowpointidint) as Knpocount, (select top 1 sqre_mainid from tbSt Udentstudyquestionrecords where Sqre_qubaid=quba_idint and sqre_studentid=200 and sqre_answerjudge= ' wrong ' or Der by sqre_adddatetime Desc) as Sqre_mainid from Tbstudentstudyquestionrecords left joins Tbquestionbank on Sqre_q Ubaid=quba_idint LefT join Tbquestiontype on Quty_id=quba_type left join Tbexamknowpoint on Expo_examidint=quba_idint where sqre_stude Ntid=200 and quba_subjectid=15 and sqre_answerjudge= ' wrong ' and quty_name<> ' comprehensive questions ' GROUP by Quba_idint,quba_number ) as T ORDER by Quba_idint

  

Post-optimization code

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_E Xamidint=quba_idint             where sqre_studentid=200 and sqre_answerjudge= ' wrong ' and quba_subjectid=15 and QuTy_Name< > ' Comprehensive question ' GROUP by Quba_idint,quba_number,quty_name ' as-             T left              join Tbstudentstudyquestionrecords on t.lastid= sqre_id    

 

It's not 1 seconds to see the optimized execution time.

Optimization ideas, the first SQL because there are three actually check is the same statement, but because the subquery can not check three columns, it was written before.

So think about using left join to optimize, take out part first, then remove part and then connect.

A section of SQL optimization

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.