Skip to main content

Nested Loop join,  Hash Join and Merge Join

SQL server has three types of internal joins. Although there are different kinds of logical joins at the query level, SQL Server implement these all with three different physical join operators based on the scenarios. They are:
  1. Nested Loop join
  2. Hash Join
  3. Merge Join

 1. Nested Loop Join
The optimizer may choose the Nested Loops join when one of the joining tables is small, considered as the outer table and the other one is large, considered as the inner table which is indexed on the column that is in the join.One of the joining table is taken as the outer table and the other one as the inner table. For each row of the outer table, all the rows from the inner table are matched one by one. It cycles through the outer table, then search for matches in the inner table. 

The optimizer considers three variants for a Nested Loops join:
  1. Naive nested loops join when the search scans the whole table or index
  2. Index nested loops join when the search can utilize an existing index to perform lookups
  3. Temporary index nested loops join if the optimizer creates a temporary index as part of the query plan and drop it after query execution completes. 

2.Hash Join:
SQL server would select this join strategy in following scenarios:
  1. when tables to be joined are large and dissimilar in size
  2. when tables to be joined don’t have a useful index. 

It build hashing tables to sort through the result.

3.Merge Join:
SQL server would select this join strategy when tables to be joined are large and similar in size.
 It takes a row from each table and compares them. 

Ref:
http://www.mssqltips.com/sqlservertip/2115/understanding-sql-server-physical-joins/

Comments

Popular posts from this blog

SQL Server script Error: Insufficient result space to convert uniqueidentifier value to char.

Error:    G etting below error while trying to copy data from one table to another: Msg 8170, Level 16, State 2, Line 1004  Insufficient result space to convert uniqueidentifier value to char.   Fix: UserID column which would be having Unique identifier data require more space (36 character). Size is specified as 15 here. To fix it just specify 36 as varchar length or specify Uniqueidentifier as data type for UserID  column.

While executing Select query to pull data from a different server, SQL Server is showing following error:

Msg 7202, Level 11, State 2, Line 1 Could not find server 'LAPTOP-09O6NE3K\SQLEXPRESS03' in sys.servers. Verify that the correct server name was specified. If necessary, execute the stored procedure sp_addlinkedserver to add the server to sys.servers. Completion time: 2024-02-05T17:18:24.6869756+05:30 Solution: Step1:  Check if the server is available in the sys.servers table SELECT name from sys.servers Step2:  If no, add the new server to the sys.servers table EXEC sp_addlinkedserver @server = 'server name' Eg" EXEC sp_addlinkedserver @server = 'LAPTOP-09O6NE3K\SQLEXP RESS03' Step3:  Recheck if the new server name is added to the sys.servers table. If yes, try then rerunning the select query which throwed the error. Now it should pull data from the table in the new server successfully.    

Overcoming Performance Issues with Memory Optimized tables - SQL Server 2014 CPT2

Overcoming Performance Issues with Memory Optimized tables - SQL Server 2014 CPT2 1.        Problem Tuning    databases so that they provide the best possible performance is one of the most important responsibilities of database administrators (DBAs).   SQL Server was originally designed at a time when it could be assumed that main memory was very expensive, so data needed to reside on disk except when it was actually needed for processing.   As the amount data    within an organization increases, accumulation of data affects the productivity of DBAs, who must spend considerable time and effort on achieving the high levels of database performance. 2.        Solution Nowadays memory is much cheaper and a working data set can be stored entirely in memory. The assumption that main memory was very expensive is no longer valid as memory prices have dropped enormously. Memory-opti...