It's important to be careful---guess what this SQL execution means

Source: Internet
Author: User

Today, when we help the customer to do the sentence optimization, suddenly encountered such a statement, similar to the following example (the original statement is an update)

The example uses two tables from the AdventureWorks data.

ProductID is [Production]. The primary key of the [Product] table, in [Purchasing]. Nonclustered indexes are also established in [PurchaseOrderDetail]

SelectName, (Select Count(*) from [Purchasing].[PurchaseOrderDetail] whereProductID=ProductID) from [Production].[Product]whereProductID= 341

  

-----------------------------------here, what kind of index do you choose to recite the meaning of the statement in mind? -----------------------------------------

    

First, take a look at the execution plan

    

[Purchasing]. [PurchaseOrderDetail] Why go index scan? This obviously should be indexed to find Ah!

Take a closer look at the predicate of the index scan

    

Did I match myself to myself? Is it the same as you think?

Modify the code to see where the problem lies

    

 select  name, (select  count  (* ) from  [ purchasing  ] . [ purchaseorderdetail   " 

where ProductID = [Production]. [Product]. ProductID)
 from [Production]. [Product] where = 341

    

    

This is a mistake due to carelessness, the example is a query, in fact, it is an update effect can be imagined, and this effect in the stored process has not been discovered for a long while, but if the optimization to view the execution plan relationship overhead, I take a look at the code also assume that absolutely no problem!

Solve the problem, that is, the problem of slow-running statements, and find out the program bug.

Summary: careful is important!

It's important to be careful---guess what this SQL execution means

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.