问题
I have two tables.
Table A contains UserID, UserName
Table B contains ID, FK_UserID, ClientName
I need to return a list of distinct A.UserName where A.Username exists in table B and has at least one ClientName attached to them, but in my query, only look at distinct B.ClientName.
My thoughts were:
Select Distinct A.UserName from A as A
Inner Join B as B
on A.UserID = B.FK_UserID
But that only distincts on table A
My next thought was:
Select Distinct Username from A
where UserID In
(
Select FK_UserID, distinct ClientName from B)
I was told that there is a way to do a distinct on both tables in the join, because if table A is 300 rows, and table B is 3 BILLION rows, my original query is going to take awhile.
The person specifically wants me to use an inner join...
回答1:
Your original query is:
Select Distinct A.UserName
from A as A Inner Join
B as B
on A.UserID = B.FK_UserID;
This can be a problem, if there are many matches in b
. Using in
isn't quite the right solution. Instead, use exists
:
select a.UserName
from a
where exists (select 1
from b
where b.fk_UserID = a.UserId
)
Then, be sure that you have an index on b(fk_UserId)
.
This should do about 300 lookups in the index. That should be quite fast.
My advice for the person who told you to use the inner join: Write a special version for that person. For the many minutes or hours that it takes to run, let other people use the faster version using exists
.
来源:https://stackoverflow.com/questions/23278387/options-for-returning-distinct-values-across-an-inner-join