- Table and Index Structure
- Page, Extent, Heap
- Index
- Index Access Method
- Index Access Method
- Included Non-key Columns
- Index Intersection
- Indexed Views
- Index Optimization Scale
- Index Access Method Performace/Selectivity
- À妽º »ç¿ë·®/IO ¹× Àá±Ý üũ
- Index Fragmentation
- Á¶°¢ÈÀÇ Á¾·ù
- Á¶°¢È üũ ¹× ÇØ°á
- Á¶°¢È ¹æÁö
- Partitioning
- ¸µÅ©
1 Table and Index Structure
1.1 Page, Extent, Heap
- Page : 8KB. IO ÃÖ¼Ò ´ÜÀ§. ¾Æ¹«¸® ÀÛÀº ÇàÀ» ÀоîµéÀδٰí ÇÏ´õ¶óµµ ÆäÀÌÁö ´ÜÀ§·Î ÀоîµéÀÌ°Ô µÈ´Ù.
- Extent : 8 Page, Áï 64 KB. ÆäÀÌÁö °ø°£ÀÌ ¸ðÀÚ¶ó´Â °æ¿ì, ÀͽºÅÙÆ® ´ÜÀ§·Î ÇÒ´çÀ» ÇÏ°Ô µÈ´Ù. °´Ã¼°¡ 64KBº¸´Ù ÀÛÀ» °æ¿ì¿¡´Â ¿¹¿Ü. ¸î¸î Äõ¸®´Â ÀͽºÅÙÆ® ´ÜÀ§·Î µ¿ÀÛÀ» ÇÏ°Ô µÇ´Âµ¥, ÀÌ·± ³ÑµéÀº ·Î±×°¡ ÃÖ¼ÒÇÑÀ¸·Î ³²±â ¶§¹®¿¡ ºü¸£´Ù. Å×ÀÌºí µå¶ø vs °¢°¢ÀÇ ÇàÀ» µû·Î Áö¿ì±â¸¦ »ý°¢ÇÏ¸é µÈ´Ù.
- Heap : Ŭ·¯½ºÅ͵å À妽º¸¦ °¡ÁöÁö ¾ÊÀº Å×À̺í. Èü ¾È¿¡ ¹¹°¡ µé¾ú´ÂÁö´Â IAM(Index Allocation Map)À» ÅëÇØ¼ °ü¸®. IAM ÀÚü´Â ¸µÅ©µå ¸®½ºÆ®. Èü°ú ¹Ý´ëµÇ´Â Å×À̺í, Áï Ŭ·¯½ºÅ͵å À妽º¸¦ °¡Áö´Â Å×À̺íÀ» Ŭ·¯½ºÅ͵å Å×À̺íÀ̶ó°í ºÎ¸¥´Ù.
1.2 Index
- Clustered Index : ¸®ÇÁ ³ëµå¿¡ ½ÇÁ¦ µ¥ÀÌÅ͸¦ °¡Áö´Â Æ®¸®.
- Nonclustered Index : ¸®ÇÁ ³ëµå¿¡ ½ÇÁ¦ µ¥ÀÌÅ͸¦ °¡ÁöÁö ¾Ê°í, row locator °ªÀ» °¡Áö´Â Æ®¸®. Row locator ¾È¿¡ ÀÖ´Â ³»¿ëÀº Å×À̺íÀÌ ÈüÀ̳Ä, Ŭ·¯½ºÅ͵å Å×À̺íÀ̳Ŀ¡ µû¶ó Ʋ¸®´Ù.
- ÈüÀÇ °æ¿ì, row locator´Â RID, Áï µ¥ÀÌÅͰ¡ ÀÖ´Â °÷ÀÇ ¹°¸®ÀûÀÎ ÁÖ¼Ò¸¦ °¡¸£Å°´Â Æ÷ÀÎÅ͸¦ °¡Áø´Ù. Lookup °úÁ¤À» ÅëÇØ ½ÇÁ¦ µ¥ÀÌÅ͸¦ Àоî¿À±â À§Çؼ´Â ÆäÀÌÁö Çϳª¸¦ Àоîµé¿©¾ß ÇÑ´Ù.
- Ŭ·¯½ºÅ͵å Å×À̺íÀÇ °æ¿ì, row locator´Â ½ÇÁ¦ µ¥ÀÌÅͰ¡ ÀÖ´Â °÷À» °¡¸£Å°´Â °ÍÀÌ ¾Æ´Ï¶ó, clustering key¶ó´Â ³í¸®ÀûÀÎ °ªÀ» °¡Áø´Ù. Clustering key´Â Å×À̺íÀÌ »ðÀÔÀÌ ÀϾ¼, ÆäÀÌÁö ºÐÇÒÀÌ ÀϾµµ ¹Ù²îÁö ¾Ê´Â °ªÀÌ´Ù. ½±°Ô »ý°¢ÇÏÀÚ¸é, Ŭ·¯½ºÅ͵å À妽ºÀÇ ³ëµå ID¸¦ °¡¸£Å°°í ÀÖ´Ù°í »ý°¢ÇÏ¸é µÈ´Ù. Lookup °úÁ¤À» ÅëÇØ µ¥ÀÌÅ͸¦ Àоî¿À±â À§Çؼ´Â clustering key¸¦ ÀÌ¿ëÇØ Æ®¸®¸¦ °Ë»öÇØ¾ß ÇÑ´Ù. Áï Æ®¸®ÀÇ ³ôÀ̰¡ 3À̶ó¸é 3°³ÀÇ ÆäÀÌÁö¸¦ Àоîµé¿©¾ß ÇÑ´Ù. ±×·¸´Ù¸é Ŭ·¯½ºÅ͵å Å×À̺íÀº ¹«Á¶°Ç ÇÇÇØ¾ß ÇÏ´Â °Ô ¾Æ´Ñ°¡¶ó°í »ý°¢ÇÒ ¼ö Àִµ¥, Ŭ·¯½ºÆ¼µå À妽º¿¡¼ ¸®ÇÁ°¡ ¾Æ´Ñ ³ëµåµéÀº ´ëºÎºÐ ij½¬¿¡ ÀúÀåµÈ´Ù. ±×·¯¹Ç·Î ¹°¸®ÀûÀÎ Àбâ ÀÛ¾÷Àº ±×·¸°Ô ¸¹ÀÌ ÀϾÁö ¾Ê´Â´Ù. À½ ¹Ì¹¦Çϱº.
- Covered Index : SELECT ±¸¹®¿¡¼ Á¶È¸ÇÏ´Â ¸ðµç Ä÷³ÀÌ À妽º¿¡ Æ÷Ç﵃ ¶§¸¦ ÀǹÌÇÑ´Ù. Áï Ä¿¹öµå À妽º´Â Ưº°ÇÑ Á¾·ùÀÇ À妽º°¡ ¾Æ´Ï¶ó, Äõ¸®°¡ ¾î¶² °ÍÀ̳Ŀ¡ µû¶ó Ä¿¹öµå/¾ðÄ¿¹öµå°¡ ³ª´µ°Ô µÈ´Ù´Â ¸»ÀÌ´Ù.
2 Index Access Method
2.1 Index Access Method
±âº»ÀûÀ¸·Î À妽º´Â B-TREE ±¸Á¶¸¦ °¡Áø´Ù. ¿©±â¿¡ Ãß°¡·Î ¸®ÇÁ ·¹º§ ³ëµåµé°£ÀÇ ´õºí ¸µÅ©µå ¸®½ºÆ®°¡ Á¸ÀçÇÑ´Ù.
- Table Scan/Unordered Clustered Index Scan
À妽º°¡ ³ª¹ßÀÌ°í ¾øÀÌ °Á IAMÀ» ÅëÇØ ÁÖ¿í ÀоîµéÀÌ´Â ÀÛ¾÷ÀÌ´Ù. Read-ahead ±â´ÉÀ» ÅëÇØ ÀͽºÅÙÆ® ´ÜÀ§·Î IO¸¦ Çϱ⵵ ÇÏ´Â ¸ð¾çÀÌ´Ù.
- Unordered Covering Nonclustered Index Scan
ÇÊ¿ä·Î ÇÏ´Â Ä÷³ÀÌ À妽º ³»ºÎ¿¡ ÀÖ´Ù´Â °ÍÀ» Á¦¿ÜÇϰí´Â Å×ÀÌºí ½ºÄµ°ú °°Àº ÀÛ¾÷ÀÌ´Ù.
- Ordered Clustered Index Scan
Ŭ·¯½ºÅ͵å À妽ºÀÇ ¸®ÇÁ ³ëµå¸¦ ¸µÅ©µå ¸®½ºÆ® µû¶ó ÁÖ¿í µµ´Â ÀÛ¾÷ÀÌ´Ù. Unordered Clustered Index Scan°ú ´Þ¸® À妽ºÀÇ Á¶°¢È°¡ ½ÉÇÑ °æ¿ì ¼º´ÉÀÌ ¶³¾îÁö°Ô µÈ´Ù.
- Ordered Covering Nonclustered Index Scan
ÇÊ¿ä·Î ÇÏ´Â Ä÷³ÀÌ ¸ðµÎ À妽º ¾È¿¡ Á¸ÀçÇϱ⠶§¹®¿¡, ÀоîµéÀÏ ÆäÀÌÁö°¡ Àû´Ù´Â °Í À̿ܿ¡´Â Ordered Clustered Index Scan°ú ºñ½ÁÇÑ ÀÛ¾÷ÀÌ´Ù.
- Nonclustered Index Seek + Ordered Partial Scan + Lookups
Äõ¸®ÀÇ °á°ú·Î ¹ÝȯµÇ´Â Çà ¼ö°¡ ÀûÀº Äõ¸®¿¡¼ ÀϾ´Â ÀÛ¾÷ÀÌ´Ù. ¸ÕÀú °Ë»ö ¹üÀ§(WHERE aaa BETWEEN min TO MAX)¸¦ ÅëÇØ À妽º¿¡¼ ½ÃÀÛ ³ëµå¿Í ³¡ ³ëµå¸¦ ãÀº ´ÙÀ½, ÀÌ »çÀ̸¦ ¸µÅ©µå ¸®½ºÆ®¸¦ µû¶ó ÁÖ¿í µµ´Â ÀÛ¾÷ÀÌ´Ù. Ä¿¹öµå À妽º¸¦ ÀÌ¿ëÇÏ´Â °ÍÀÌ ¾Æ´Ï¹Ç·Î, °¢°¢ÀÇ °á°ú Çึ´Ù lookup ÀÛ¾÷ÀÌ ÀϾÙ. Áï Å×À̺íÀÌ ÈüÀÏ °æ¿ì¿¡´Â ÆäÀÌÁö Çϳª, Å×À̺íÀÌ Å¬·¯½ºÅ͵åÀÏ °æ¿ì¿¡´Â À妽º ·¹º§¸¸ÅÀÇ ÆäÀÌÁö¸¦ ÀоîµéÀÌ°Ô µÈ´Ù. Áï °Ë»ö ¹üÀ§°¡ ³ÐÀ» °æ¿ì, ¼º´ÉÀÌ ÇöÀúÇÏ°Ô ¶³¾îÁö°Ô µÇ´Â ÀÛ¾÷ÀÌ´Ù.
- Unordered Nonclustered Index Scan + Lookups
Äõ¸®ÀÇ °á°ú¸¦ ÇÊÅ͸µÇÏ´Â Á¶°ÇÀýÀÌ À妽º¿Í´Â ¹«°üÇÑ °æ¿ì ¹ß»ýÇÏ´Â ÀÛ¾÷ÀÌ´Ù. À妽º ¸®ÇÁ ·¹º§À» ±×³É ÁÖ¿í µ¹¸é¼ °¢°¢ÀÇ Çà¿¡ ´ëÇØ lookup °úÁ¤À» ¼öÇàÇÏ°Ô µÈ´Ù.
- Clustered Index Seek + Ordered Partial Scan
°Ë»ö Á¶°ÇÀÌ À妽º¿¡ ù¹øÂ° ŰÄ÷³ÀÏ °æ¿ì¿¡ ÀϾ´Â ÀÛ¾÷ÀÌ´Ù. °Ë»ö Á¶°ÇÀÇ min °ªÀ» ÀÌ¿ëÇØ ¸®ÇÁ ·¹º§ÀÇ ³ëµå¸¦ ãÀº ´ÙÀ½, max °ªÀ» °¡Áö´Â ³ëµå¸¦ ¸¸³¯ ¶§±îÁö, ¸®ÇÁ ·¹º§ ¸µÅ©µå ¸®½ºÆ®¸¦ µû¶ó°¡´Â ÀÛ¾÷ÀÌ´Ù. Ŭ·¯½ºÅ͵å Å×À̺íÀ̹ǷΠ·è¾÷ °úÁ¤ÀÌ ÇÊ¿ä¾ø´Ù.
- Covering Nonclustered Index Seek + Ordered Partial Scan
³ÍŬ·¯½ºÅ͵å À妽ºÀÇ ¸®ÇÁ ·¹º§¿¡ Á¸ÀçÇÏ´Â Ä÷³ °ªÀ» ÀÌ¿ë(row locator ¾È¿¡ ÀÌ·± °Íµµ ÀÖ±¸³ª!)ÇÑ´Ù´Â °ÍÀ» Á¦¿ÜÇϰí´Â Clustered Index Seek¿Í °°Àº ÀÛ¾÷ÀÌ´Ù.
°á±¹ Ŭ·¯½ºÅ͵å Å×À̺íÀ̶ó°í ¹«Á¶°Ç ÁÁÀº °ÍÀÌ ¾Æ´Ï´Ù¶ó´Â °É ¾Ë ¼ö ÀÖ´Ù. ÇÁ¶óÀ̸Ӹ® ۸¦ »ý¼ºÇÏ´Â °æ¿ì, µû·Î ÁöÁ¤ÇØÁÖÁö ¾ÊÀ¸¸é ¹«Á¶°Ç Ŭ·¯½ºÅ͵å À妽º°¡ »ý±â´Â ¸ð¾çÀε¥, Çѹø »ý°¢Çغ¼ ¹®Á¦ÀÎ °Í °°´Ù.
2.2 Included Non-key Columns
Ä¿¹ö¸µ À妽º¸¦ ¸¸µé ¶§, À妽º¿¡ µé¾î°¡´Â ¸ðµç Ä÷³À» Ű Ä÷³À¸·Î ¸¸µé ÇÊ¿ä´Â ¾ø´Ù. °¢°¢ÀÇ Å° Ä÷³µéÀº À妽ºÀÇ Á¤·Ä ¼ø¼¸¦ Á¤ÀÇÇÑ´Ù. ÀÌ ¸»Àº À妽ºÀÇ Å° Ä÷³ °ªÀÌ ¹Ù²î¸é Æ®¸®ÀÇ Á¤·Ä ¼ø¼ ¶ÇÇÑ º¯°æµÈ´Ù´Â ¸»ÀÌ´Ù. ´Ü¼øÈ÷ Ä¿¹ö¸µÀÌ ¸ñÀûÀÎ °æ¿ì¿¡´Â included non-key columnsÀ» »ç¿ëÇÏ´Â °ÍÀÌ ÁÁ´Ù. ÀÌ Ä÷³µéÀº ¸®ÇÁ ·¹º§¿¡¸¸ µé¾î°¡°Ô µÈ´Ù.
CREATE NONCLUSTERED INDEX idx_nc_sid_od_i_cid
ON dbo.Orders(shipperid, orderdate)
INCLUDE(custid);
2.3 Index Intersection
Äõ¸® ÃÖÀûȱⰡ »ç¿ëÇÏ´Â Æ®¸¯. 2°¡Áö ÀÌ»óÀÇ À妽ºÀÇ °á°ú¸¦ row locator °ªÀ» ±âÁØÀ¸·Î ºñ±³Çؼ Çϳª·Î ¸¸µå´Â ±â´É. ±×³É ÀÌ·± °Ô ÀÖ´Ù°í¸¸ ¾Ë¸é µÇ´Â °Í °°Àºµ¥?
2.4 Indexed Views
3 Index Optimization Scale
3.1 Index Access Method Performace/Selectivity
Fast <------------------> Slow
-----------------------------------+---------------------------------
Table Scan/Unordered |
Clustered Index Scan | O
-----------------------------------+---------------------------------
Unordered Covering |
Nonclustered Index Scan | O
-----------------------------------+---------------------------------
Unordered Covering |
Nonclustered Index Scan + Lookups | <------------------------->
-----------------------------------+---------------------------------
Nonclustered Index Seek + |
Ordered Partial Scan + Lookups | <------------------------------>
-----------------------------------+---------------------------------
Clustered Index Seek |
Ordered Partial Scan | <----------------------->
-----------------------------------+---------------------------------
Covering Nonclustered Index Seek + |
Ordered Partial Scan | <---->
-----------------------------------+---------------------------------
High Low
Selectivity <---> Selectivity
0% 100%
3.2 À妽º »ç¿ë·®/IO ¹× Àá±Ý üũ
sys.dm_db_index_operational_stats (
{ database_id | NULL | 0 | DEFAULT }
, { object_id | NULL | 0 | DEFAULT }
, { index_id | 0 | NULL | -1 | DEFAULT }
, { partition_number | NULL | 0 | DEFAULT }
)
SELECT * FROM sys.dm_db_index_usage_stats u, sys.objects o
WHERE u.object_id=o.object_id AND o.type in ('U', 'V');
Ä«¿îÅÍ´Â SqlServer¸¦ ½ÃÀÛÇÒ ¶§¸¶´Ù ÃʱâȵȴÙ.
4 Index Fragmentation
4.1 Á¶°¢ÈÀÇ Á¾·ù
- Logical scan fragmentation or Average fragmentation in percent or External fragmentation
Out of order ÆäÀÌÁöÀÇ ºñÀ². Áï ³í¸®ÀûÀÎ ¼ø¼¿Í ¾î±ß³ª°Ô ¹°¸®ÀûÀ¸·Î ¹èÄ¡µÈ À妽º ÆäÀÌÁöÀÇ ºñÀ². Ordered scan ÀÛ¾÷ÀÇ ¼º´É¿¡ Å« ¿µÇâÀ» ¹ÌÄ£´Ù.
- Average page density or Internal fragmentation
Logical scan framentationÀº ¹«Á¶°Ç ³ª»Û °ÍÀÌÁö¸¸, ÆäÀÌÁö ¹Ðµµ´Â µÎ °¡Áö Ãø¸éÀ» °¡Áø´Ù. ³·Àº ¹Ðµµ¸¦ °¡Áö´Â À妽º¿¡¼ Àбâ ÀÛ¾÷À» ÇÒ ¶§¿¡´Â ³ôÀº ¹Ðµµ¸¦ °¡Áö´Â À妽º¿¡¼ ÀÛ¾÷ÇÏ´Â °Íº¸´Ù ¸¹Àº ÆäÀÌÁö¸¦ Àоîµé¿©¾ß ÇϹǷΠºÒ¸®ÇÏ´Ù. ÇÏÁö¸¸ ¾²±â ÀÛ¾÷À» ÇÒ ¶§¿¡´Â ³·Àº ¹Ðµµ¸¦ °¡Áö´Â ÂÊÀÌ À¯¸®ÇÏ´Ù. ³ôÀº ¹ÐµµÀÇ À妽º¿¡ ´ëÇØ ¾²±â ÀÛ¾÷À» ÇÏ´Â °æ¿ì, »õ·Î¿î ÇàÀÌ Ãß°¡µÇ¸é ÆäÀÌÁö ½ºÇø´, Áï Æ®¸®ÀÇ ºÐÇÒÀÌ ÀÌ·ç¾îÁú °¡´É¼ºÀÌ ³ô±â ¶§¹®ÀÌ´Ù. Áï SELECT ÀÛ¾÷À» ÁÖ·Î ÇÏ´Â Å×ÀÌºí¿¡¼´Â ¹Ðµµ°¡ ³ô¾Æ¾ß Çϰí, INSERT ÀÛ¾÷À» ÀÚÁÖ ÇÏ´Â OLTP ȯ°æ¿¡¼´Â ¹Ðµµ°¡ ³·¾Æ¾ß ÇÑ´Ù.
4.2 Á¶°¢È üũ ¹× ÇØ°á
sys.dm_db_index_physical_stats (
{ database_id | NULL | 0 | DEFAULT }
, { object_id | NULL | 0 | DEFAULT }
, { index_id | NULL | 0 | -1 | DEFAULT }
, { partition_number | NULL | 0 | DEFAULT }
, { mode | NULL | DEFAULT }
)
SELECT
OBJECT_NAME(p.object_id) as table_name,
i.name,
p.index_type_desc,
p.avg_fragmentation_in_percent
FROM sys.dm_db_index_physical_stats(DB_ID('µ¥ÀÌÅͺ£À̽ºÀ̸§'), NULL, NULL, NULL, 'LIMITED') p, sys.indexes i
WHERE p.object_id = i.object_id AND p.index_id = i.index_id;
Á¶°¢ÈÀ²ÀÌ 30% ÀÌÇ϶ó¸é ALTER INDEX REORGANIZE, 30% Ãʰú¶ó¸é ALTER INDEX REBUILD ±¸¹®À» »ç¿ëÇÏ´Â °ÍÀÌ ÁÁ´Ù°í ÇÑ´Ù.
ALTER INDEX À妽ºÀ̸§(¶Ç´Â ALL) ON Å×À̺íÀ̸§ REORGANIZE;
ALTER INDEX À妽ºÀ̸§(¶Ç´Â ALL) ON Å×À̺íÀ̸§ REBUILD WITH (ONLINE=ON);
4.3 Á¶°¢È ¹æÁö
Á¶°¢È¸¦ ¾Æ¿¹ ÇÇÇϱâ À§Çؼ´Â ¾î¶² ÀÏÀ» ÇØ¾ßÇϴ°¡?
from
How to Detect Table Fragmentation in SQL Server 2000 and 2005
- When a database is created, make sure the data files are created with or assigned the largest values possible. You can do this by planning to use a value that can fit the maximum amount of data during a certain period (say, three years at least).
- Sometimes it is feasible to permit the data files to grow automatically while keeping a limit on the growth by specifying a maximum data file growth size that leaves some available space on the hard disk.
- After a period of time, ascertain and re-evaluate the expected maximum database size by adding more files or filegroups to the database, if required.
- Do not let the data files grow automatically if there many data files share the same disk partition. If the data files are heavily used then locate them in a different filegroup or on a different partition.
- Perform regular database maintenance tasks, such as DBCC DBREINDEX, and recompiling stored procedures and triggers.
- If the table row(s) are modified or deleted frequently then it is better to run intermittent UPDATE STATISTICS on the table, which will help it avoid any slow performance from the execution plan.
5 Partitioning
...¹º°¡ °í±Þ ÁÖÁ¦Àΰ¡ º»µ¥?
6 ¸µÅ©
SeriousMoin v1 (koMoinMoin 1.0a4 Modified)