what is Hash join?how it is different from inner join?what
is the sign used for inner join?(eg: like the (+) sign used
for outer join)?

Answers were Sorted based on User's Feedback



what is Hash join?how it is different from inner join?what is the sign used for inner join?(eg: li..

Answer / guest

Microsoft SQL Server 7.0/2000 supports three types of join
operations:

# Nested-Loop joins
# Merge joins
# Hash joins

The Hash join will be used, if there are no adequate indexes
on the joined columns. This is a worst situation. In this
case, hash table will be created. Hash join is most
efficient when one of the tables is significantly differ in
size than another one.

The query optimizer makes a Hash join in two phases: build
and probe. So, Hash join has two inputs: the build input and
the probe input.

On the build phase, hash table will be created by scanning
each value in the build input and applying the hashing
algorithm to the key.

Let me to describe Hash join on the example with two tables.
Look at this example:

if object_id('dbo.Table1') is not null drop table Table1
GO
CREATE TABLE Table1 (id int, Name char(10))
GO
if object_id('dbo.Table2') is not null drop table Table2
GO
CREATE TABLE Table1 (id int, Name char(20))
GO

DECLARE @i int
SELECT @i = 1
WHILE @i < 1000
BEGIN
INSERT INTO Table1 VALUES (@i, LTRIM(str(@i)))
SELECT @i = @i + 1
END
GO

DECLARE @i int
SELECT @i = 1
WHILE @i < 1000
BEGIN
INSERT INTO Table2 VALUES (@i, LTRIM(str(@i)))
SELECT @i = @i + 1
END
GO

SET SHOWPLAN_TEXT ON
GO
SELECT a.Name FROM Table1 a INNER JOIN Table2 b
ON a.Name = b.Name
GO
SET SHOWPLAN_TEXT OFF
GO

This is the result:


StmtText
-------------------------------------------------------------------------------------------
|--Hash Match(Inner Join, HASH:([a].[Name])=([b].[Name]),
RESIDUAL:([a].[Name]=[b].[Name]))
|--Table Scan(OBJECT:([pubs].[dbo].[Table1] AS [a]))
|--Table Scan(OBJECT:([pubs].[dbo].[Table2] AS [b]))

The smaller table will be build input, the other - probe
input. Field Name (column that joins the tables) is called
hash key. The hash table consists of linked lists called
hash buckets. The result of using a hash function on a hash
key is called hash value. Hash value and RID (row
identifier) will be placed into hash table.

Hash value must be smaller than hash key. So, query
processor economies on the size of the hash table. The real
example of hashing is notebook. You can open notebook on the
appropriate letter and scan all surnames on this letter to
find necessary ones. So, notebook is the example of hash
table, and pages on the appropriate letter are the example
of hash bucket.

During the probe phase, the entire probe input is scanned,
and for each probe row computes the same hash value on the
hash key to find any matches in the corresponding hash bucket.

There are two main kinds of Hash join:

# In-memory Hash join
# Grace Hash join


In-memory Hash Join will be used if entire build input can
be placed into memory.

Grace Hash join will be used if your server has not enough
memory to hold the entire build input. In this case, query
processor proceeds Hash Join in several steps (hash table
will be divided into multiple partitions and relevant
partition will be loaded as need).

Because the query optimizer usually selects the best
execution plan for a given select statement, it is not
necessary to enforce the desirable join type, but sometimes
it can be useful. You can enforce the desirable join type by
using the OPTION clause.

This is the example to enforce Hash join:

USE pubs
GO
SET SHOWPLAN_TEXT ON
GO
SELECT a.au_id FROM authors a JOIN titleauthor b
ON a.au_id = b.au_id OPTION (HASH JOIN)
GO
SET SHOWPLAN_TEXT OFF
GO

This is the result:


StmtText
-----------------------------------------------------------------------------------------------
SELECT a.au_id FROM authors a JOIN titleauthor b
ON a.au_id = b.au_id OPTION (HASH JOIN)

(1 row(s) affected)

StmtText
-----------------------------------------------------------------------------------------------
|--Hash Match(Inner Join, HASH:([a].[au_id])=([b].[au_id]),
RESIDUAL:([a].[au_id]=[b].[au_id]))
|--Index Scan(OBJECT:([pubs].[dbo].[authors].[aunmind]
AS [a]))
|--Index
Scan(OBJECT:([pubs].[dbo].[titleauthor].[titleidind] AS [b]))

(3 row(s) affected)


INNER JOIN: inner join is used to retrieve matched data from
2 or more tables. inner join used "=" sign.

Is This Answer Correct ?    5 Yes 0 No

what is Hash join?how it is different from inner join?what is the sign used for inner join?(eg: li..

Answer / ram

when the joining tables having huge data or selective columns don't have index at that
hash will occured automatically by using optimization

Is This Answer Correct ?    0 Yes 0 No

what is Hash join?how it is different from inner join?what is the sign used for inner join?(eg: li..

Answer / anil kumar jaiswal

Hash Join : When there is no index available on join columns and both table has huge amount of data then oracle optimizer will pick hash join automatically.

we also have use_hash hints in oracle to instruct oracle optimizer to perform hash join on the given Query.

check explain plan for any query to see the join performed on that particular query.

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More SQL PLSQL Interview Questions

wa procedure to return the month and the no'f developers joined in each month.

4 Answers   Fidelity,


How do you create a unique index?

0 Answers  


How do I run a sql query?

0 Answers  


How do I quit sql?

0 Answers  


What are different sql data types?

0 Answers  






Which one of the following pairs of aggregate functions do you use with character fields? 1. COUNT(field) and MAX(field) 2. AVG(field) and COUNT(field) 3. MIN(field) and SUM(field) 4. AVG(field) and MAX(field) 5. COUNT(field) and SUM(field)

6 Answers   HCL,


what is oracle database ? : Sql dba

0 Answers  


I have one Excel file with 1,50,000 Records. Now I need to load that whole file into Oracle Database with same columns in Excel sheet . I need PLSQL Procedure or used by SQL PLUS

7 Answers   Polaris,


How to write a procedure for displying the data in a TREE or (PARENT and CHILD ) relationship , for ex: A is the main project id, for this project B,C,D are sub tasks(sub project id's) for B the sub tasks are e,f,g and for c is h ,i ,j and for d is k,l,m now i need to display the o/p in a TREE fashion pls help me , thanks in advance surendra

2 Answers   Zensar,


How you will create Toad Function?

1 Answers  


Can sql developer connect to db2?

0 Answers  


What are the different types of tables in sql?

0 Answers  


Categories