July 24, 2018 at 2:32 am
Hi
I hope this is an easy question to answer. ๐
I'm joining a large table to a small table and results in an index scan on the large table, returning 1 million rows, which is then hash joined to the 2 rows returned by the small table
select ST.col1, ST.col2, .ST.col3. LT.col4
from SmallTable as ST
inner join LargeTable as LT ST.col1 = LT.col1
where ST.col2 = 6
Is there a better way to join these tables, so there isn't a table scan on the large table?
Cheers
Alex
July 24, 2018 at 3:29 am
alex.palmer - Tuesday, July 24, 2018 2:32 AMHiI hope this is an easy question to answer. ๐
I'm joining a large table to a small table and results in an index scan on the large table, returning 1 million rows, which is then hash joined to the 2 rows returned by the small table
select ST.col1, ST.col2, .ST.col3. LT.col4
from SmallTable as ST
inner join LargeTable as LT ST.col1 = LT.col1
where ST.col2 = 6Is there a better way to join these tables, so there isn't a table scan on the large table?
Cheers
Alex
Have you considered putting a supporting index on the large table? Say, the join column as key column, with any other columns referenced by the query in the INCLUDE section?
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
July 24, 2018 at 3:35 am
Yep! I've done exactly that and I still get a table scan
July 24, 2018 at 3:39 am
alex.palmer - Tuesday, July 24, 2018 3:35 AMYep! I've done exactly that and I still get a table scan
Can you attach the ACTUAL execution plan as a .sqlplan file attachment please?
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
July 24, 2018 at 4:31 am
Hi
sorry I've wasted your time. I looked at the column definitions and the data types where different.
Once I casted the nchar to a char and the data types matched I got the expected scan
Cheers
Alex
July 26, 2018 at 11:26 am
alex.palmer - Tuesday, July 24, 2018 4:31 AMHisorry I've wasted your time. I looked at the column definitions and the data types where different.
Once I casted the nchar to a char and the data types matched I got the expected scan
Cheers
Alex
Wouldn't you expect a SEEK as opposed to a scan?
Steve (aka sgmunson) ๐ ๐ ๐
Rent Servers for Income (picks and shovels strategy)
July 26, 2018 at 12:18 pm
sgmunson - Thursday, July 26, 2018 11:26 AMalex.palmer - Tuesday, July 24, 2018 4:31 AMHisorry I've wasted your time. I looked at the column definitions and the data types where different.
Once I casted the nchar to a char and the data types matched I got the expected scan
Cheers
Alex
Wouldn't you expect a SEEK as opposed to a scan?
Depends. Might be a range scan using the index, which can be more efficient than a seek.
July 26, 2018 at 12:46 pm
sorry I've wasted your time. I looked at the column definitions and the data types where different.
Is there a reason you didn't use a foreign key reference? This would have come up when you tried to establish it. Also doing this, if it's appropriate, gives the analyzer additional information about the relationship of the data.
July 26, 2018 at 3:21 pm
andycadley - Thursday, July 26, 2018 12:18 PMsgmunson - Thursday, July 26, 2018 11:26 AMalex.palmer - Tuesday, July 24, 2018 4:31 AMHisorry I've wasted your time. I looked at the column definitions and the data types where different.
Once I casted the nchar to a char and the data types matched I got the expected scan
Cheers
Alex
Wouldn't you expect a SEEK as opposed to a scan?
Depends. Might be a range scan using the index, which can be more efficient than a seek.
IIRC, I'm pretty sure that range scans start with a seek.
--Jeff Moden
Change is inevitable... Change for the better is not.
July 26, 2018 at 4:14 pm
andycadley - Thursday, July 26, 2018 12:18 PMsgmunson - Thursday, July 26, 2018 11:26 AMalex.palmer - Tuesday, July 24, 2018 4:31 AMHisorry I've wasted your time. I looked at the column definitions and the data types where different.
Once I casted the nchar to a char and the data types matched I got the expected scan
Cheers
Alex
Wouldn't you expect a SEEK as opposed to a scan?
Depends. Might be a range scan using the index, which can be more efficient than a seek.
That would show up as an index seek. Anything that searches down the index tree for a particular value is considered a seek.
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
June 6, 2022 at 11:30 am
Whenever you do a left join a small table and a large table make sure that large table is defined first in the join query. In your case the query can be optimised by simly doing:
select ST.col1, ST.col2, .ST.col3. LT.col4
from LargeTable as LT
inner join SmallTable as ST
where ST.col1 = LT.col1
and ST.col2 =ย 6
June 7, 2022 at 2:50 pm
Whenever you do a left join a small table and a large table make sure that large table is defined first in the join query. In your case the query can be optimised by simly doing:
select ST.col1, ST.col2, .ST.col3. LT.col4 from LargeTable as LT inner join SmallTable as ST
where ST.col1 = LT.col1 and ST.col2 =ย 6
First, the question was about an INNER join, not a LEFT join.ย The two types of joins are VERY different.
Second, changing the order of tables in a LEFT join can completely change the data that is returned.ย This is a much more important factor than the efficiency of the join.
Third, I don't believe that the order of the tables in an INNER JOIN has any effect on the efficiency of that join.
Drew
J. Drew Allen
Business Intelligence Analyst
Philadelphia, PA
June 7, 2022 at 2:56 pm
satyanarayana09 wrote:Whenever you do a left join a small table and a large table make sure that large table is defined first in the join query. In your case the query can be optimised by simly doing:
select ST.col1, ST.col2, .ST.col3. LT.col4 from LargeTable as LT inner join SmallTable as ST
where ST.col1 = LT.col1 and ST.col2 =ย 6
First, the question was about an INNER join, not a LEFT join.ย The two types of joins are VERY different.
Second, changing the order of tables in a LEFT join can completely change the data that is returned.ย This is a much more important factor than the efficiency of the join.
Third, I don't believe that the order of the tables in an INNER JOIN has any effect on the efficiency of that join.
Drew
Yes, the optimiser decides on the order to join tables it has nothing to do with the order they are specified in the query unless you use a FORCE ORDER hint.
Viewing 13 posts - 1 through 12 (of 12 total)
You must be logged in to reply to this topic. Login to reply