SQL forces the specified index to speed up queries

Source: Internet
Author: User
Tags dba


Today encountered a query problem, add a query parameter caused by query super-times yellow, after the company DBA Improvement, up posture. Now send to share with you! ~


SELECT m.* from Tb_usersitegroup u with (NOLOCK), Message.dbo.View_Message_8 m with (NOLOCK) WHERE  m.fromuserid = U. UserID and   U.adminid =880982 and m.state=1 and  m.touserid=0 and U.siteid = 8ORDER by  m.time DESC

This is the original SQL, with the extra U. Adminid = 880982 parameter causes the query to time out.

The query followed by with (NOLOCK) can theoretically speed up the query by 33%, but may cause dirty reads (Baidu, you know)


This is the improved SQL:

SELECT  m.*from    tb_usersitegroup u with (nolock,index=ix_tb_usersitegroup_siteid)  JOIN       Message.dbo.View_Message_8 m with (NOLOCK) on    M.fromuserid = U.userid and        m.state = 1 and        m.touserid = 0        and U.siteid = 8 and        U.adminid = 880982ORDER by M.time DESC

From the previous query time 2 minutes to become now seconds out ... Creates a new Ix_tb_usersitegroup_siteid index and forces the query index to be specified.

The join connection form is changed from the previous comma connection (no known what is called).


Asked DBA,DBA that the join connection automatically searches for the optimal, best-fit index for lookups, whereas a comma connection is a matching lookup based on the Where condition. There is a functional loss between

These are my vague memories of things, the DBA said the specific are not clear, may not be accurate, welcome you to add the discussion.


SELECT m.* from Tb_usersitegroup u w (nolock,index=ix_tb_usersitegroup_siteid), Message.dbo.View_Message_8 m with ( NOLOCK) WHERE  M.fromuserid = U.userid and U.adminid =880982 and m.state=1 and M.touserid=0 and  U.siteid = 8ORD ER by  M.time DESC

This SQL is the result of the original SQL plus the specified index.
Query time is also seconds out ...
In terms of data volume and time, the essential difference between join and comma connections is not visible for the moment. Follow-up study is followed.


For the time being so many, write down for later reference, also for everyone reference study.

Welcome to the Danale to enlighten them. Add the wrong place ...



SQL forces the specified index to speed up queries

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.