![]() ![]() But increase it to 50 elements, or 5000 elements, and the list version slows to a crawl, because it's O(N) vs. ![]() Because of the size, the hash table version is actually slower. You can see the same effect by writing a program in your favourite programming language to perform a large number of lookups on a list with 5 elements, vs. It's because SQL Server wants to do a hash match for the INNER JOIN, but does nested loops for the LEFT JOIN the former is normally much faster, but since the number of rows is so tiny and there's no index to use, the hashing operation turns out to be the most expensive part of the query. If you run this and view the execution plan, you'll see that the INNER JOIN query does indeed cost more than the LEFT JOIN, because it satisfies the two criteria above. INSERT #Test2 (ID, Name) VALUES (5, 'Five') INSERT #Test2 (ID, Name) VALUES (4, 'Four') INSERT #Test2 (ID, Name) VALUES (3, 'Three') ![]() INSERT #Test2 (ID, Name) VALUES (2, 'Two') INSERT #Test2 (ID, Name) VALUES (1, 'One') INSERT #Test1 (ID, Name) VALUES (5, 'Five') INSERT #Test1 (ID, Name) VALUES (4, 'Four') INSERT #Test1 (ID, Name) VALUES (3, 'Three') INSERT #Test1 (ID, Name) VALUES (2, 'Two') INSERT #Test1 (ID, Name) VALUES (1, 'One') The tables do not have sufficient indexes to cover the query.Ĭonsider this example: CREATE TABLE #Test1.Some of the tables are very small (say, under 10 rows). ![]() Reflecting further on this, I could think of one circumstance under which a LEFT JOIN might be faster than an INNER JOIN, and that is when: If you post your schema, we might be able to provide more details. 9 tables is quite a lot to be joining so the slowdown could literally be almost anywhere. Most likely your performance problems lie elsewhere, such as not having a candidate key or foreign key indexed properly. (And even if a LEFT JOIN were faster in specific situations due to some difficult-to-imagine confluence of factors, it is not functionally equivalent to an INNER JOIN, so you cannot simply go replacing all instances of one with the other!) It would also be expected to return more rows, further increasing the total execution time simply due to the larger size of the result set. In fact, it's slower by definition, an outer join ( LEFT JOIN or RIGHT JOIN) has to do all the work of an INNER JOIN plus the extra work of null-extending the results. ON e.ProductGroup1Cd = g2.ProductGroup1CdĪ LEFT JOIN is absolutely not faster than an INNER JOIN. INNER JOIN coProductGroup2 g2 - PK = Compan圜d, ProductCategor圜d, UsedDepartment, ProductGroup2Cd ON e.ProductGroup1Cd = g1.ProductGroup1Cd INNER JOIN coProductGroup1 g1 - PK = Compan圜d, ProductCategor圜d, UsedDepartment, ProductGroup1Cd INNER JOIN coProductOldInformation j - PK = Compan圜d, BFStatus, SpecCd INNER JOIN coProduct e - PK = Compan圜d, ProductSalesCd INNER JOIN coMappingExpParty d - NO PK AND FK INNER JOIN exFSlipHdr c - PK = Compan圜d, FSlipNo, FSlipSuffix INNER JOIN exFSlipDet h - PK = Compan圜d, FSlipNo, FSlipSuffix, FSlipLine INNER JOIN sidisalesdetmly b - THIS TABLE ALSO HAVE NO PK AND FK FROM sidisaleshdrmly a - NOT HAVE PK AND FK I would like to know why LEFT JOIN is faster than INNER JOIN? After I changed it, the speed of query got significantly improved. So my folk suggested me to change INNER JOIN to LEFT JOIN because the performance of LEFT JOIN is better, despite what I know. I've created SQL command that uses INNER JOIN on 9 tables, anyway this command takes a very long time (more than five minutes). ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |