-
Notifications
You must be signed in to change notification settings - Fork 11
[23기_오지송] 성능최적화 미션 제출합니다. #75
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Oh-Jisong
wants to merge
2
commits into
CEOS-Developers:Oh-Jisong
Choose a base branch
from
Oh-Jisong:Oh-Jisong
base: Oh-Jisong
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| +----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | ||
| | 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) | ||
| | | ||
| +---------------------------------------------------------------------------------------------------------------------------------------------------+ | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | ||
| | | ||
| +----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | ||
| | | ||
| +---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | ||
| | | ||
| +----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Explain Analyze를 통해 실제 쿼리 실행 계획까지 보여주셨네요!!