Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
924 changes: 776 additions & 148 deletions README.md

Large diffs are not rendered by default.

24 changes: 24 additions & 0 deletions docs/performance-logs/case1_where_index.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
Case 1. WHERE condition index

-- Before
mysql> EXPLAIN ANALYZE SELECT * FROM reservations WHERE member_id = 1;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Explain Analyze를 통해 실제 쿼리 실행 계획까지 보여주셨네요!!

+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| EXPLAIN |
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| -> Filter: (reservations.member_id = 1) (cost=10104 rows=9967) (actual time=0.328..50.8 rows=150 loops=1)
-> Table scan on reservations (cost=10104 rows=99673) (actual time=0.327..45.8 rows=100000 loops=1)
|
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

-- Apply
mysql> CREATE INDEX idx_reservations_member_id ON reservations(member_id);


-- After
mysql> EXPLAIN ANALYZE SELECT * FROM reservations WHERE member_id = 1;
+---------------------------------------------------------------------------------------------------------------------------------------------------+
| EXPLAIN |
+---------------------------------------------------------------------------------------------------------------------------------------------------+
| -> Index lookup on reservations using idx_reservations_member_id (member_id=1) (cost=52.5 rows=150) (actual time=0.118..0.542 rows=150 loops=1)
|
+---------------------------------------------------------------------------------------------------------------------------------------------------+
47 changes: 47 additions & 0 deletions docs/performance-logs/case2_selective_index.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
Case 2. Selective index choice

-- Reset
mysql> DROP INDEX idx_reservations_member_id ON reservations;


-- Before
mysql> EXPLAIN ANALYZE SELECT * FROM reservations WHERE status = 'CONFIRMED' AND reserved_at >= DATE_SUB(NOW(), INTERVAL 3 DAY);
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| EXPLAIN |
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| -> Filter: ((reservations.`status` = 'CONFIRMED') and (reservations.reserved_at >= <cache>((now() - interval 3 day)))) (cost=9439 rows=3322) (actual time=0.353..75.3 rows=1427 loops=1)
-> Table scan on reservations (cost=9439 rows=99673) (actual time=0.342..51.8 rows=100000 loops=1)
|
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

-- Apply bad index
mysql> CREATE INDEX idx_reservations_status ON reservations(status);


-- Status only result
mysql> EXPLAIN ANALYZE SELECT * FROM reservations WHERE status = 'CONFIRMED' AND reserved_at >= DATE_SUB(NOW(), INTERVAL 3 DAY);
+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| EXPLAIN |
+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| -> Filter: (reservations.reserved_at >= <cache>((now() - interval 3 day))) (cost=2070 rows=16610) (actual time=1.5..187 rows=1427 loops=1)
-> Index lookup on reservations using idx_reservations_status (status='CONFIRMED') (cost=2070 rows=49836) (actual time=1.49..181 rows=80000 loops=1)
|
+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

-- Switch index
mysql> DROP INDEX idx_reservations_status ON reservations;


-- Apply good index
mysql> CREATE INDEX idx_reservations_reserved_at ON reservations(reserved_at);


-- Reserved_at result
mysql> EXPLAIN ANALYZE SELECT * FROM reservations WHERE status = 'CONFIRMED' AND reserved_at >= DATE_SUB(NOW(), INTERVAL 3 DAY);
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| EXPLAIN |
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| -> Filter: (reservations.`status` = 'CONFIRMED') (cost=788 rows=175) (actual time=0.443..3.54 rows=1427 loops=1)
-> Index range scan on reservations using idx_reservations_reserved_at over ('2026-05-20 22:13:40' <= reserved_at), with index condition: (reservations.reserved_at >= <cache>((now() - interval 3 day))) (cost=788 rows=1750) (actual time=0.434..3.26 rows=1750 loops=1)
|
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
26 changes: 26 additions & 0 deletions docs/performance-logs/case3_orderby_index.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
Case 3. ORDER BY optimization

-- Before
mysql> EXPLAIN ANALYZE SELECT * FROM store_orders ORDER BY total_price DESC LIMIT 100;
+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| EXPLAIN |
+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| -> Limit: 100 row(s) (cost=10087 rows=100) (actual time=60..60 rows=100 loops=1)
-> Sort: store_orders.total_price DESC, limit input to 100 row(s) per chunk (cost=10087 rows=99828) (actual time=60..60 rows=100 loops=1)
-> Table scan on store_orders (cost=10087 rows=99828) (actual time=0.303..44.1 rows=100000 loops=1)
|
+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

-- Apply
mysql> CREATE INDEX idx_store_orders_total_price ON store_orders(total_price);


-- After
mysql> EXPLAIN ANALYZE SELECT * FROM store_orders ORDER BY total_price DESC LIMIT 100;
+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| EXPLAIN |
+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| -> Limit: 100 row(s) (cost=0.114 rows=100) (actual time=1.16..1.51 rows=100 loops=1)
-> Index scan on store_orders using idx_store_orders_total_price (reverse) (cost=0.114 rows=100) (actual time=1.16..1.5 rows=100 loops=1)
|
+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
50 changes: 50 additions & 0 deletions docs/performance-logs/case4_covering_index.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
Case 4. WHERE + ORDER BY + covering index

-- Reset
mysql> DROP INDEX idx_reservations_reserved_at ON reservations;


-- Before
mysql> EXPLAIN ANALYZE SELECT id, status, reserved_at, total_price FROM reservations WHERE member_id = 1 ORDER BY reserved_at DESC LIMIT 10;
+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| EXPLAIN |
+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| -> Limit: 10 row(s) (cost=10104 rows=10) (actual time=44..44 rows=10 loops=1)
-> Sort: reservations.reserved_at DESC, limit input to 10 row(s) per chunk (cost=10104 rows=99673) (actual time=44..44 rows=10 loops=1)
-> Filter: (reservations.member_id = 1) (cost=10104 rows=99673) (actual time=0.255..43.9 rows=150 loops=1)
-> Table scan on reservations (cost=10104 rows=99673) (actual time=0.254..39.4 rows=100000 loops=1)
|
+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

-- Apply single index
mysql> CREATE INDEX idx_reservations_member_id ON reservations(member_id);


-- Single index result
mysql> EXPLAIN ANALYZE SELECT id, status, reserved_at, total_price FROM reservations WHERE member_id = 1 ORDER BY reserved_at DESC LIMIT 10;
+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| EXPLAIN |
+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| -> Limit: 10 row(s) (cost=52.5 rows=10) (actual time=0.314..0.315 rows=10 loops=1)
-> Sort: reservations.reserved_at DESC, limit input to 10 row(s) per chunk (cost=52.5 rows=150) (actual time=0.313..0.313 rows=10 loops=1)
-> Index lookup on reservations using idx_reservations_member_id (member_id=1) (cost=52.5 rows=150) (actual time=0.0815..0.272 rows=150 loops=1)
|
+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

-- Switch to covering index
mysql> DROP INDEX idx_reservations_member_id ON reservations;


-- Apply covering index
mysql> CREATE INDEX idx_reservations_member_reserved_at_cover ON reservations(member_id, reserved_at DESC, status, total_price);


-- Covering result
mysql> EXPLAIN ANALYZE SELECT id, status, reserved_at, total_price FROM reservations WHERE member_id = 1 ORDER BY reserved_at DESC LIMIT 10;
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| EXPLAIN |
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| -> Limit: 10 row(s) (cost=17.9 rows=10) (actual time=0.0457..0.0502 rows=10 loops=1)
-> Covering index lookup on reservations using idx_reservations_member_reserved_at_cover (member_id=1) (cost=17.9 rows=150) (actual time=0.0447..0.0485 rows=10 loops=1)
|
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
14 changes: 14 additions & 0 deletions docs/performance-sql/case1_where_index.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
SELECT 'Before' AS stage;
EXPLAIN ANALYZE
SELECT *
FROM reservations
WHERE member_id = 1;

CREATE INDEX idx_reservations_member_id
ON reservations(member_id);

SELECT 'After' AS stage;
EXPLAIN ANALYZE
SELECT *
FROM reservations
WHERE member_id = 1;
30 changes: 30 additions & 0 deletions docs/performance-sql/case2_selective_index.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
DROP INDEX idx_reservations_member_id ON reservations;

SELECT 'Before' AS stage;
EXPLAIN ANALYZE
SELECT *
FROM reservations
WHERE status = 'CONFIRMED'
AND reserved_at >= DATE_SUB(NOW(), INTERVAL 3 DAY);

CREATE INDEX idx_reservations_status
ON reservations(status);

SELECT 'Status only result' AS stage;
EXPLAIN ANALYZE
SELECT *
FROM reservations
WHERE status = 'CONFIRMED'
AND reserved_at >= DATE_SUB(NOW(), INTERVAL 3 DAY);

DROP INDEX idx_reservations_status ON reservations;

CREATE INDEX idx_reservations_reserved_at
ON reservations(reserved_at);

SELECT 'Reserved_at result' AS stage;
EXPLAIN ANALYZE
SELECT *
FROM reservations
WHERE status = 'CONFIRMED'
AND reserved_at >= DATE_SUB(NOW(), INTERVAL 3 DAY);
16 changes: 16 additions & 0 deletions docs/performance-sql/case3_orderby_index.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
SELECT 'Before' AS stage;
EXPLAIN ANALYZE
SELECT *
FROM store_orders
ORDER BY total_price DESC
LIMIT 100;

CREATE INDEX idx_store_orders_total_price
ON store_orders(total_price);

SELECT 'After' AS stage;
EXPLAIN ANALYZE
SELECT *
FROM store_orders
ORDER BY total_price DESC
LIMIT 100;
33 changes: 33 additions & 0 deletions docs/performance-sql/case4_covering_index.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
DROP INDEX idx_reservations_reserved_at ON reservations;

SELECT 'Before' AS stage;
EXPLAIN ANALYZE
SELECT id, status, reserved_at, total_price
FROM reservations
WHERE member_id = 1
ORDER BY reserved_at DESC
LIMIT 10;

CREATE INDEX idx_reservations_member_id
ON reservations(member_id);

SELECT 'Single index result' AS stage;
EXPLAIN ANALYZE
SELECT id, status, reserved_at, total_price
FROM reservations
WHERE member_id = 1
ORDER BY reserved_at DESC
LIMIT 10;

DROP INDEX idx_reservations_member_id ON reservations;

CREATE INDEX idx_reservations_member_reserved_at_cover
ON reservations(member_id, reserved_at DESC, status, total_price);

SELECT 'Covering result' AS stage;
EXPLAIN ANALYZE
SELECT id, status, reserved_at, total_price
FROM reservations
WHERE member_id = 1
ORDER BY reserved_at DESC
LIMIT 10;
Loading