Skip to content

Latest commit

ย 

History

History
181 lines (150 loc) ยท 4.89 KB

sql-tuning.md

File metadata and controls

181 lines (150 loc) ยท 4.89 KB

SQL ํŠœ๋‹์— ๋Œ€ํ•œ ๊ธฐ์ˆ  ๋ฉด์ ‘ ๋‹ต๋ณ€

์ฃผ์š” ์งˆ๋ฌธ

"SQL ์„ฑ๋Šฅ ์ตœ์ ํ™”๋ฅผ ์œ„ํ•œ ํŠœ๋‹ ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์„ค๋ช…ํ•ด์ฃผ์„ธ์š”. ์‹ค์ œ ํ”„๋กœ์ ํŠธ์—์„œ ์„ฑ๋Šฅ ๊ฐœ์„ ์„ ์œ„ํ•ด ์–ด๋–ค ๋ฐฉ๋ฒ•๋“ค์„ ์ ์šฉํ•ด๋ณด์…จ๋‚˜์š”?"

๋‹ต๋ณ€ ๊ตฌ์กฐ

1. SQL ํŠœ๋‹์˜ ๊ธฐ๋ณธ ์›์น™

1.1 ์‹คํ–‰๊ณ„ํš ๋ถ„์„

-- MySQL์˜ ๊ฒฝ์šฐ
EXPLAIN SELECT * FROM users u 
JOIN orders o ON u.id = o.user_id 
WHERE u.status = 'active';

-- ์‹คํ–‰๊ณ„ํš ๋ถ„์„ ํฌ์ธํŠธ
- type ์ปฌ๋Ÿผ: const, eq_ref, ref, range, index, ALL ๋“ฑ์˜ ์ ‘๊ทผ ๋ฐฉ์‹ ํ™•์ธ
- key ์ปฌ๋Ÿผ: ์‚ฌ์šฉ๋˜๋Š” ์ธ๋ฑ์Šค ํ™•์ธ
- rows ์ปฌ๋Ÿผ: ๊ฒ€์ƒ‰๋˜๋Š” ํ–‰ ์ˆ˜ ์˜ˆ์ธก

1.2 WHERE ์ ˆ ์ตœ์ ํ™”

-- ์•ˆ์ข‹์€ ์˜ˆ
SELECT * FROM orders 
WHERE YEAR(created_at) = 2024;  -- ์ธ๋ฑ์Šค ์‚ฌ์šฉ ๋ถˆ๊ฐ€

-- ์ข‹์€ ์˜ˆ
SELECT * FROM orders 
WHERE created_at >= '2024-01-01' 
AND created_at < '2025-01-01';  -- ์ธ๋ฑ์Šค ์‚ฌ์šฉ ๊ฐ€๋Šฅ

2. ์ฃผ์š” ํŠœ๋‹ ๊ธฐ๋ฒ•

2.1 ์ธ๋ฑ์Šค ์ตœ์ ํ™”

-- ๋ณตํ•ฉ ์ธ๋ฑ์Šค ์ƒ์„ฑ ์‹œ ์„ ํƒ๋„(Selectivity)๊ฐ€ ๋†’์€ ์ปฌ๋Ÿผ์„ ์•ž์— ๋ฐฐ์น˜
CREATE INDEX idx_user_status_created 
ON users(status, created_at);  -- status๋ณด๋‹ค created_at์˜ ์„ ํƒ๋„๊ฐ€ ๋†’์€ ๊ฒฝ์šฐ ์ˆœ์„œ ๋ณ€๊ฒฝ ๊ณ ๋ ค

-- ์ปค๋ฒ„๋ง ์ธ๋ฑ์Šค ํ™œ์šฉ
SELECT user_id, status, created_at  -- ์ธ๋ฑ์Šค์— ํฌํ•จ๋œ ์ปฌ๋Ÿผ๋งŒ ์กฐํšŒ
FROM users 
WHERE status = 'active' 
AND created_at > '2024-01-01';

2.2 ์กฐ์ธ ์ตœ์ ํ™”

-- ์ž‘์€ ๊ฒฐ๊ณผ์…‹์„ ๋จผ์ € ์กฐ์ธ
SELECT * FROM small_table s  -- 1000 rows
JOIN large_table l          -- 1000000 rows
ON s.id = l.small_id;

-- ์กฐ์ธ ์กฐ๊ฑด ์ตœ์ ํ™”
SELECT * FROM orders o
JOIN users u ON u.id = o.user_id  -- PK-FK ์กฐ์ธ
WHERE u.status = 'active';

2.3 ์„œ๋ธŒ์ฟผ๋ฆฌ ์ตœ์ ํ™”

-- ์•ˆ์ข‹์€ ์˜ˆ (์ƒ๊ด€ ์„œ๋ธŒ์ฟผ๋ฆฌ)
SELECT *, 
  (SELECT COUNT(*) FROM orders o 
   WHERE o.user_id = u.id) as order_count
FROM users u;

-- ์ข‹์€ ์˜ˆ (์กฐ์ธ์œผ๋กœ ๋ณ€ํ™˜)
SELECT u.*, COALESCE(o.order_count, 0) as order_count
FROM users u
LEFT JOIN (
  SELECT user_id, COUNT(*) as order_count
  FROM orders
  GROUP BY user_id
) o ON u.id = o.user_id;

3. ๊ณ ๊ธ‰ ํŠœ๋‹ ๊ธฐ๋ฒ•

3.1 ํŒŒํ‹ฐ์…”๋‹

-- ๋ฒ”์œ„ ํŒŒํ‹ฐ์…”๋‹ ์˜ˆ์‹œ
CREATE TABLE orders (
    id INT,
    created_at DATE,
    amount DECIMAL(10,2)
) PARTITION BY RANGE (YEAR(created_at)) (
    PARTITION p2023 VALUES LESS THAN (2024),
    PARTITION p2024 VALUES LESS THAN (2025)
);

3.2 materialized view ํ™œ์šฉ

-- PostgreSQL์˜ ๊ฒฝ์šฐ
CREATE MATERIALIZED VIEW monthly_sales AS
SELECT 
    DATE_TRUNC('month', created_at) as month,
    SUM(amount) as total_sales
FROM orders
GROUP BY DATE_TRUNC('month', created_at)
WITH DATA;

๊ทผ๊ฑฐ ์ž๋ฃŒ

1. ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๋ฒค๋” ๊ณต์‹ ๋ฌธ์„œ

2. ์„ฑ๋Šฅ ์ธก์ • ๋„๊ตฌ ๋ฌธ์„œ

3. ํ•™์ˆ  ์ž๋ฃŒ ๋ฐ ์„œ์ 

  • "High Performance MySQL" by Baron Schwartz, Peter Zaitsev, and Vadim Tkachenko
  • "SQL Performance Explained" by Markus Winand

4. ์ปค๋ฎค๋‹ˆํ‹ฐ ๋ฐ ๋ธ”๋กœ๊ทธ

์‹ค์ œ ํŠœ๋‹ ์‚ฌ๋ก€

1. ๋Œ€๋Ÿ‰ ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ ์ตœ์ ํ™”

-- ์ฒ˜๋ฆฌ ์ „
DELETE FROM logs WHERE created_at < '2023-01-01';

-- ์ฒ˜๋ฆฌ ํ›„ (๋ฐฐ์น˜ ์ฒ˜๋ฆฌ)
REPEAT
    DELETE FROM logs 
    WHERE created_at < '2023-01-01' 
    LIMIT 10000;
    SLEEP(0.1);
UNTIL ROW_COUNT() = 0 END REPEAT;

2. IN์ ˆ ์ตœ์ ํ™”

-- ์ฒ˜๋ฆฌ ์ „
SELECT * FROM orders 
WHERE user_id IN (
    SELECT id FROM users WHERE status = 'active'
);

-- ์ฒ˜๋ฆฌ ํ›„
SELECT o.* 
FROM orders o
JOIN users u ON o.user_id = u.id
WHERE u.status = 'active';

3. GROUP BY ์ตœ์ ํ™”

-- ์ฒ˜๋ฆฌ ์ „
SELECT user_id, COUNT(*) 
FROM orders 
GROUP BY user_id;

-- ์ฒ˜๋ฆฌ ํ›„ (์ธ๋ฑ์Šค ํ™œ์šฉ)
CREATE INDEX idx_user_id ON orders(user_id);
SELECT user_id, COUNT(*) 
FROM orders 
GROUP BY user_id;

์‹ค์ œ ๋ฉด์ ‘์—์„œ๋Š” ์ด๋Ÿฌํ•œ ์ด๋ก ์ ์ธ ๋‚ด์šฉ๊ณผ ํ•จ๊ป˜, ๋ณธ์ธ์ด ์‹ค์ œ ํ”„๋กœ์ ํŠธ์—์„œ ๊ฒฝํ—˜ํ•œ ์„ฑ๋Šฅ ๊ฐœ์„  ์‚ฌ๋ก€๋ฅผ ๊ตฌ์ฒด์ ์œผ๋กœ ์„ค๋ช…ํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด:

  1. ๋ฌธ์ œ ์ƒํ™ฉ ์„ค๋ช…
  2. ์›์ธ ๋ถ„์„ ๊ณผ์ •
  3. ํ•ด๊ฒฐ ๋ฐฉ๋ฒ• ์ ์šฉ
  4. ์„ฑ๋Šฅ ๊ฐœ์„  ๊ฒฐ๊ณผ

์ด๋Ÿฌํ•œ ๊ตฌ์ฒด์ ์ธ ๊ฒฝํ—˜์„ ๊ณต์œ ํ•˜๋ฉด ์‹ค๋ฌด ๋Šฅ๋ ฅ์„ ํšจ๊ณผ์ ์œผ๋กœ ์–ดํ•„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.