SELECT DISTINCT Inside WHERE IN function

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

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

GCL_Loanshas a list of loans with basic information CCL_Loan_Itemscontains information about a specific item in the loan. INGCL_Loan_Items

there may be a duplicate Loan_ID

Can someone explain why this query will be faster or slower than the previous one?

SELECT*FROM GCL_Loans WHERE Loan_ID IN
(
    SELECT DISTINCT Loan_ID FROM GCL_Loan_Items
)
+4
source share
2 answers

The version of "DISTINCT" is probably faster because the sentence INwill specify a smaller set of data to search to determine if there is any given GCL_Loans.Loan_IDin the set. Without DISTINCTa data set there will be more.

, IN, , , DISTINCT ... , .

, , (mysql) .

+1

, DISTINCT, SUBQuery (IN) . , SQL , , IN.

-1

All Articles