Hash Join ½ÇÇà
(1) °³¿ä
JOIN ÀÇ Á¾·ù´Â 3°¡Áö·Î ³ª´µ´Âµ¥, Sort merge join, Nested loop join, Hash join ÀÌ´Ù.ÀÌÁß Hash Join (HJ) Àº 7.3 ºÎÅÍ »ç¿ë°¡´ÉÇÏ¸ç ±× ÁÖ¿ä ±â´ÉÀ» »ìÆ캸¸é
- index °¡ ¿©·¯ level ÀÇ depth ¸¦ °¡Áú ¶§ Sort Merge Join (SMJ) À̳ª Nested Loops (NL)º¸´Ù ÁÁÀº È¿°ú¸¦ ³½´Ù.
- sort ¸¦ ÇÏÁö ¾ÊÀ¸¹Ç·Î SMJ º¸´Ù ÁÁÀº ¼º´ÉÀ» ³»¸ç, ÀÛÀº table °ú Å« table ÀÇ join ½Ã¿¡ À¯¸®ÇÏ´Ù.
- ÁÖÀÇÇØ¾ß ÇÒ °ÍÀº hash join Àº equi join ¿¡¼¸¸ °¡´ÉÇÏ´Ù´Â °ÍÀÌ´Ù.
- HJ Àº driving table ¿¡ index ¸¦ ÇÊ¿ä·Î ÇÏÁö ¾Ê´Â´Ù.
- SMJ ³ª NL º¸´Ù È¿À²ÀûÀε¥, ÀÌ´Â SMJ °¡ merge ´Ü°è¿¡ µé¾î°¡±â À§ÇØ ¾çÂÊ table ÀÌ ¸ðµÎ sort µÇ¾î¾ß Çϱ⠶§¹®ÀÌ´Ù.
¶Ç NLÀº driving table ÀÌ ¸¹Àº row ÀÇ data ¸¦ °®´Â °æ¿ì ºñÈ¿À²ÀûÀ̼ inner table À» ¿©·¯¹ø probe(Ž»ö)ÇÏ°Ô ÇÑ´Ù.
ÀÌ¿¡ ¹ÝÇØ HJ´Â °¢ table ¿¡ ´ëÇØ 1 ¹ø¸¸ pass ÇÑ´Ù.
(2) CostÀÇ ºñ±³
ÆíÀÇ»ó join µÇ´Â sql ¹®ÀÌ ´ÙÀ½°ú °°´Ù°í °¡Á¤ÇÏÀÚ.
SELECT S.a, B.a FROM S,B WHERE S.a = B.a
S´Â small table ÀÌ°í, B´Â big table ÀÌ´Ù.
(* analyze ¸¦ ¼öÇàÇϸé CBO ´Â ÀÌ¹Ì S°¡ out table ÀÌ°í B °¡ inner table À̸ç , S °¡ driving table ÀÓÀ» ÀνÄÇÑ´Ù. )
NLJ ´Â S table ÀÇ ¸ðµç row ¿¡ ´ëÇØ a column À» B table ÀÇ ¸ðµç column À»match Çϱ⠶§¹®¿¡ rS* rB key ºñ±³°¡ ¿ä±¸µÈ´Ù.: Cost(NLJ) ´Â Read(S) + [ rS *Read(B) ] ¿¡ ºñ·Ê
¶Ç SMJ ´Â S ¿Í B ¸¦ memory ¿¡ Àоî¿Í join key ¸¦ °¢°¢ sort ÇÏ°í, join À»¼öÇàÇϹǷΠcost ´Â
Cost(SMJ) ´Â Read(S) + Write(SortRuns(S))
+ Read(B) + Write(SortRuns(B))
+ Merge(S,B)
+ CPUSortCost(S + B) ¿¡ ºñ·ÊÇÑ´Ù.
memory ¿¡¼ ¼öÇàµÇ´Â HJ ÀÇ algorithm Àº ¾Æ·¡¿¡¼ ¼³¸íµÈ¾î Áö´Âµ¥ ÀÌÀÇ cost ¸¦ ¹Ì¸® check ÇØ º¸ÀÚ¸é Cost(HJ) = Read(S) + Build Hash Table in Memory (cpu)
+ Read(B) + Perform In memory Join(cpu)
ÀÌ °æ¿ì CPU costs¸¦ ¹«½ÃÇϸé ,
Cost(HJ)´Â Read(S) + Read(B) ¿¡ ºñ·ÊÇÑ´Ù°í ÇÒ¼ö ÀÖ´Ù.
(3) Hash join À» ¼öÇàÇϱâ À§ÇØ Oracle Àº ´ÙÀ½ÀÇ °úÁ¤À» °ÅÄ£´Ù.:
À̸¦ ¼öÇàÇϱâ À§ÇØ partition ´Ü°è¿Í join ´Ü°è¸¦ °ÅÄ¡¸ç ÀÌÀÇ algorithm À» grace join À̶ó ÇÑ´Ù. ÀÌÀÇ ÇÑ°è´Â join value ÀÇ ºÐ¹è°¡ ÇÑÂÊÀ¸·Î Ä¡¿ìħÀÌ ¾øÀÌ partition ¿¡ °í¸£°Ô ºÐÆ÷µÇ¾î¾ß ÇÑ´Ù´Â °ÍÀÌ´Ù. ÀÌ algorithm Àº ´ÙÀ½°ú °°´Ù.
1. partiton °¹¼ö¸¦ °áÁ¤ÇÑ´Ù.À̸¦ fan out À̶óÇÑ´Ù. high fan out Àº ¿©·¯°³ÀÇ ÀÛÀº partition À» ¸¸µé¾î i/o ÀÇ È¿À²À» ¶³¾î ¶ß¸®¸ç,low fan out Àº Ä¿´Ù¶õ partition À» ¸¸µé¾î hash memory ÀÇ hit À²À» ¶³¾î¶ß¸°´Ù . ±×·¯¹Ç·Î À̸¦ ÀûÀýÈ÷ °¡Á®°¡´Â °ÍÀÌ performance ÀÇ ÁÖ ¿äÁ¡À̸ç(ÀÌ´Â bit map °¹¼ö¸¦ °áÁ¤) ÀÌÀÇ È¿À²À» ³ôÀ̱â À§ÇØ hash area size¸¦ ´Ã¸®°í, hash multi block io ¸¦ ÁÙÀδÙ. 2. driving table À» °áÁ¤ÇÑ´Ù.(ÀÛÀº table ·Î °áÁ¤)
3. small table ÀÇ ÇÊ¿ä column À» Àоîµé¿© hash area ÀÇ partition ¿¡ ºÐ¹è Çϴµ¥ ÀÌ´Â join column À¸·Î hash function1À» Åë°ú ½ÃÅ°¸é¼ partition ¿¡ hash function2 ÀÇ hash value ¿Í ÇÔ²² ºÐ¹èÇÑ´Ù. À̶§ bitmap vector ¸¦ ¸¸µç´Ù. ÀÌ bitmap Àº 2Â÷¿ø bucket Àε¥ hash function 1 °ú 2 ¸¦ Åë°ú½ÃÄÑ ¸¸µç ´Ù.Áï partition ÀÌ 100 °³¶ó¸é 100* 100 ÀÇ 10000 °³ÀÇ cell ·Î ÀÌ·ç¾î Áø´Ù.
4. °¢ row ¿¡ ´ëÇØ bitmap ÀÇ (a,b) ¿¡ marking À» ÇÑ´Ù.
5 À§ÀÇ step ÀÌ ¸ðµÎ ³¡³ª¸é driving table ÀÌ ¾Æ´Ñ Å« table À» Àоîµé¿© function1,2 ¸¦ Åë°úÇÑ´Ù. À̶§ ³ª¿Â hash value ¸¦ driving table ÀÌ ¸¸µé¾î ³õÀº bitmap °ú ´ëÁ¶ÇÏ¿© 1 À̸é join À» ÇØ¾ß ÇÏ´Â column À¸·Î ÀνÄÇÏ°í ¾Æ´Ï¸é join ÇÒ ÇÊ¿ä°¡ ¾ø´Â row À̹ǷΠ¹ö¸°´Ù. À̸¦ bit vector filtering À̶óÇÑ´Ù.
À̶§ hash table À» ±¸¼ºÇϱâ À§ÇØ Ç×»ó full table À» scan ÇÏ´Â °ÍÀº ¾Æ´Ï´Ù. ¸ÕÀú where Á¶°ÇÀÇ index ¸¦ Ÿ¼ Á¶°Ç¿¡ ¸Â°Ô row ¸¦ °É·¯³½ ´ÙÀ½ ±×°á°ú¿¡ ´ëÇØ hash table À» ±¸¼ºÇÑ´Ù. ¶Ç hash array size °¡ Å©¸é ¹®Á¦°¡ ¾ÈµÇ´Âµ¥, ÀÛÀ¸¸é disk ÀÇ temp segment ¿¡ ³»·Á º¸³»¾ß ÇϹǷΠproblem ÀÌ ¹ß»ýÇÑ´Ù.
6. B ÀÇ joined value ¸¦ hash function 1 À» Åë°ú½ÃÄÑ ÀÌ row °¡ bit vector¿¡ ÀÖ°í, memory À§ÀÇ partition ¿¡ ÀÖÀ¸¸é join ÀÌ ¼öÇàµÇ°í °á°ú°¡ return µÈ´Ù. memory ¿¡ ÀÖÁö ¾ÊÀ¸¸é disk ¿¡ ÀÖ´Â ÀûÀýÇÑ S partition ¿¡ ¾º¿©Áø´Ù. 7. 1¹ø° B °¡ pass µÈÈÄ S ÀÇ ¼öÇàµÇÁö ¾Ê´Â partition µéÀÌ ÃÖ´ëÇÑ temp segment ¿¡¼ memory ·Î ¿Ã·ÁÁö°í hash table ÀÌ »ý¼ºµÈ´Ù. ±×¸®°í B ÀÇ partition ÀÌ ´Ù½Ã ÀÐÇôÁ® memory join ÀÌ ½ÇÇàµÈ´Ù.
Áï ¼öÇàµÇÁö ¾Ê´Â disk ÀÇ partition (S,B) ÀÌ ÀÐÇôÁø´Ù.
(4) parameter -HASH_JOIN_ENABLED : true ·Î ÁöÁ¤½Ã »ç¿ë°¡´É
-HASH_AREA_SIZE : sort_area_size ÀÇ 2¹è°¡ ±âº»
-HASH_MULTIBLOCK_IO_COUNT : DB_BLOCK_READ_COUNT °¡ ±âº»
-USE_HASH : hint
(5) partition °¹¼ö °áÁ¤
ù¹ø°·Î ¿ì¸®´Â partition (bucket) ÀÇ °¹¼ö¸¦ °áÁ¤ÇØ¾ß ÇÑ´Ù. ¿©±â¿¡ ¿ì¸®´Â hashed row ¸¦ ³ÖÀ» °ÍÀÌ´Ù. ÀÌ´Â hash_area_size, db_block_size and hash_multiblock_io_count parameters¿¡ ÀÇÇØ °áÁ¤µÈ´Ù.
¶Ç ÀÌ °ªÀº 20% Á¤µµÀÇ overhead ¸¦ °í·ÁÇØ¾ß ÇÑ´Ù.- storing partitions,the bitmap of unique left input and the hash table
ÇÔ¼ö :
Partitions °¹¼ö = 0.8 x hash_area_size) ---------------------------- (db_block_size x hash_multiblock_io_count)
row °¡ °¡Àå ÀÛÀº table ÀÌ ÀÐÇôÁö°í (R À̶ó°í ºÎ¸£ÀÚ) , °¢ row ´Âhash algorithm À» µû¸¥´Ù.
°¢ row ¸¦ bucket ¿¡ °ñ°í·ç ÆîÃÄÁö°Ô Çϱâ À§ÇØ 2°¡ÁöÀÇ algorithm À» µû¸¥´Ù.
hash µÇ´Â row °¡ partition ¿¡ °ñ°í·ç ºÐ»êµÇ±â À§ÇØ 1 ¹ø° hash function À» µû¸£¸ç, 2 ¹ø° hash value ´Â ´ÙÀ½ hash µÇ´Â °æ¿ì¸¦ À§ÇØ row ¿Í ÇÔ²²ÀúÀåµÈ´Ù.ÀÌ¿Í µ¿½Ã¿¡ µÎ°¡ÁöÀÇ hash value ¸¦ ÀÌ¿ëÇÑ bitmap ÀÌ ¸¸µé¾îÁø´Ù.
(6) Bitmap building ¿¹Á¦ :
Hash Algorithm 1 ->
1 2 3 4
1 0 0 0 0SecondHash 2 0 0 0 0 ------> Algorithm | 3 0 0 0 0 V 4 0 0 0 0
driving table Àº hash function 1, 2 ¸¦ Åë°úÇÏ¿© bitmap À» ¸¸µç´Ù . ¸¸ÀÏ hash area °¡ ¸ðµÎ Â÷¸é °¡Àå Å« partition ÀÌ disk ·Î ³»·Á°£´Ù.
disk ÀÇ partition Àº partition ¿¡ ÇÒ´çµÇ´Â row ¿¡ ÀÇÇØ disk ¿¡¼ updateµÇ¾îÁø´Ù. ¸¸ÀÏ hash area ÀÇ ºÎÁ·À¸·Î 1 partition ¸¸ÀÌ memeory ¿¡ ¿Ã¶ó°£´Ù¸é ³ª¸ÓÁö partition Àº ¸ðµÎ disk ¿¡ ³õ¿©Áö°Ô µÈ´Ù. ÀÌ·± °æ¿ì´Â »ý±âÁö ¾Êµµ·Ï Á¶½ÉÇÏ¿©¾ß ÇÑ´Ù.ÀÌ ÀÛ¾÷ÀÌ R table ÀÇ ¸ðµç row ¿¡ ´ëÇØ ÇàÇØÁø´Ù.
ÀÌ ÀÛ¾÷½Ã °¡´ÉÇÑ ¸ðµç partition ÀÌ memeory ¿¡ À§Ä¡Çϵµ·Ï ÇØ¾ß ÇÑ´Ù.ÀÌ ÀÛ¾÷ÀÌÈÄ B table À» ÀоîµéÀδÙ.À̵µ ¿ª½Ã hash function À» Åë°ú½ÃÄÑ hash value °¡ memory ¿¡ ÀÖ´Â partition À» hit ÇÏ´ÂÁö check ÇÑ´Ù. ¸¸ÀÏ ±×·¯¸é ÀÌ row ´Â joined row ·Î ¹ÝȯÇÑ´Ù.¸¸ÀÏ ¾Æ´Ï¸é ÇØ´ç row ¸¦ »õ·Î¿î partiion ¿¡ write ÇÑ´Ù. À̶§ S ¿Í °°Àº hash function À» »ç¿ëÇϸç ÀÌÀÇ Àǹ̴ S¿Í B ÀÇ °°Àº value´Â °°Àº partition number ¸¦ °®°Ô Çϱâ À§ÇÔÀÌ´Ù.
(7) unique join keys ÀÇ bitmap
bitmap Àº partition ¿¡ µé¾îÀÖ´Â value ÀÇ flag À̶ó ÇÒ¼ö ÀÖ´Ù.ÀÌ´Â S ÀÇ row °¡ disk ÀÇ partititon ¿¡ ¾ºÀ̱â Àü¿¡ »ý¼ºµÇ¾î Áø´Ù.
from otn
ÁÁÀº»ý°¢ ^^