Skip to content

Latest commit

ย 

History

History

19. Query Optimization

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
ย 
ย 

Query Optimization

๊ฐ€์žฅ cost-effective ํ•œ ๋‚ด์šฉ์œผ๋กœ plan์„ ์งœ์„œ execution ํ•œ๋‹ค.

Incremental View Maintenance

  • Update view incrementally by accounting for changes that occurred since last update
    • Join
    • Selection
    • Projection
    • Intersection - Aggregation

Use of Selectives in Cost-Based Optimization

  • Cost components for query execution
    • Access cost to secondary storage
    • Disk storage cost
    • Computation cost
    • Memory usage cost
    • Communication cost

Additional Issues Related to Query Optimization

Displaying the systemโ€™s query execution plan

  • Oracle syntax
EXPLAIN PLAN FOR <SQL query>
  • IBM DB2 syntax
EXPLAIN PLAN SELECTION [additional options] FOR <SQL-query>
  • SQL server syntax
SET SHOWPLAN_TEXT ON or SET SHOWPLAN_XML ON or SET SHOW PLAN_ALL ON

Mysql EXPLAIN

์ฐธ๊ณ  : [MySQL] ์‹คํ–‰๊ณ„ํš (explain) ๋ณด๋Š”๋ฒ•

  • Mysql join type
    • The type column of EXPLAIN output describes how tables are joined.
  • ์•„๋ž˜๋กœ ๊ฐˆ์ˆ˜๋ก ์•ˆ์ข‹์€ type์ž„
    • system : The table has only one row (= system table). This is a special case of the const join type.
    • const : ํ…Œ์ด๋ธ”์— ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ๋ ˆ์ฝ”๋“œ๊ฐ€ ํ•˜๋‚˜์ผ ๋•Œ, ์ƒ์ˆ˜ ์ทจ๊ธ‰
    • eq_ref : primary key๋‚˜ unique not null column์œผ๋กœ ์ƒ์„ฑ๋œ ์ธ๋ฑ์Šค๋ฅผ ์‚ฌ์šฉํ•ด ์กฐ์ธ์„ ํ•˜๋Š” ๊ฒฝ์šฐ์ด๋‹ค. const ๋ฐฉ์‹ ๋‹ค์Œ์œผ๋กœ ๋น ๋ฅธ ๋ฐฉ๋ฒ•์ด๋‹ค.
      •   SELECT * FROM ref_table,other_table WHERE ref_table.key_column=other_table.column;
    • ref : ์ธ๋ฑ์Šค๋กœ ์ง€์ •๋œ ์ปฌ๋Ÿผ๋ผ๋ฆฌ์˜ '=' , '<=>' ์™€ ๊ฐ™์€ ์—ฐ์‚ฐ์ž๋ฅผ ํ†ตํ•œ ๋น„๊ต๋กœ ์ˆ˜ํ–‰๋˜๋Š” ์กฐ์ธ์ด๋‹ค
      •   SELECT * FROM ref_table WHERE key_column=expr;
    • fulltext : The join is performed using a FULLTEXT index.
    • ref_or_null : ref์™€ ๋™์ผํ•˜๋‚˜ NULL๊นŒ์ง€ ์ฐพ์„๋•Œ.
      •   SELECT * FROM ref_table WHERE key_column=expr OR key_column IS NULL;
    • index_merge : ์ธ๋ฑ์Šค ๋ณ‘ํ•ฉ ์ตœ์ ํ™”๊ฐ€ ์ ์šฉ๋˜๋Š” ์กฐ์ธํƒ€์ž…. ์ด ๊ฒฝ์šฐ, key์ปฌ๋Ÿผ์€ ์‚ฌ์šฉ๋œ ์ธ๋ฑ์Šค์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋‚˜ํƒ€๋‚ด๋ฉฐ key_len ์ปฌ๋Ÿผ์€ ์‚ฌ์šฉ๋œ ์ธ๋ฑ์Šค์ค‘ ๊ฐ€์žฅ ๊ธด key๋ช…์„ ๋‚˜ํƒ€๋‚ธ๋‹ค.
    • unique_subquery : ์˜ค์ง ํ•˜๋‚˜์˜ ๊ฒฐ๊ณผ๋งŒ์„ ๋ฐ˜ํ™˜ํ•˜๋Š” 'IN'์ด ํฌํ•จ๋œ ์„œ๋ธŒ์ฟผ๋ฆฌ์˜ ๊ฒฝ์šฐ์ด๋‹ค.
    • index_subquery : unique_subquery์™€ ๋น„์Šทํ•˜์ง€๋งŒ ์—ฌ๋Ÿฌ๊ฐœ์˜ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค
    • range : ํŠน์ •ํ•œ ๋ฒ”์œ„์˜ rows๋“ค์„ ๋งค์นญ์‹œํ‚ค๋Š”๋ฐ ์ธ๋ฑ์Šค๊ฐ€ ์‚ฌ์šฉ๋œ ๊ฒฝ์šฐ์ด๋‹ค. BETWEEN์ด๋‚˜ IN, '>', '>=' ๋“ฑ์ด ์‚ฌ์šฉ๋  ๋•Œ์ด๋‹ค.
    • all : ์กฐ์ธ์‹œ์— ๋ชจ๋“  ํ…Œ์ด๋ธ”์˜ ๋ชจ๋“  row๋ฅผ ์Šค์บ”ํ•˜๋Š” ๊ฒฝ์šฐ. ๋ฌผ๋ก  ์„ฑ๋Šฅ์ด ๊ฐ€์žฅ ๋Š๋ฆฌ๋‹ค.

References

  1. Fundamentals of Database Systems 7th Edition by Ramez Elmasri, Shamkant B. Navathe.
  2. [MySQL] ์‹คํ–‰๊ณ„ํš (explain) ๋ณด๋Š”๋ฒ•