简体   繁体   中英

SELECT DISTINCT Inside WHERE IN clause performance

I have a performance question about the following code...

SELECT*FROM GCL_Loans WHERE Loan_ID IN
(
    SELECT Loan_ID FROM GCL_Loan_Items
)

GCL_Loans has a list of loans with basic infomation CCL_Loan_Items has information about a specific item in a loan. There can be duplicate Loan_ID's in GCL_Loan_Items

Can anyone explain why this query would be faster or slower than the one above?

SELECT*FROM GCL_Loans WHERE Loan_ID IN
(
    SELECT DISTINCT Loan_ID FROM GCL_Loan_Items
)

The "DISTINCT" version is probably faster, because the IN clause will have a smaller data set to search to determine if any given GCL_Loans.Loan_ID is in the set. Without the DISTINCT , the data set will be larger.

There's a reasonably good argument to be made that the query optimizer will automatically recognize the IN test is a set-wise, not a list-wise test and do the DISTINCT during auto-indexing ... but I've seen that fail before.

Note that subselects can be a fail here too, because some databases (mysql) will execute the subselect for each element in the primary select.

两者的计划和绩效是平等的

Because by selecting DISTINCT there is less criteria in the SUBQuery (IN). My understanding is SQL will run the subquery first to generate the list of items that are to be included in the IN.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM