Skip to content
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

[SWM-427] Apply Predefined Indexes and Verify Performance #154

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

D-w-nJ
Copy link
Member

@D-w-nJ D-w-nJ commented Apr 4, 2024

Motivation ๐Ÿค”

  • ๏ฟฝDB ์ธ๋ฑ์Šค๋ฅผ ์ ์šฉ์‹œ์ผœ ์„ฑ๋Šฅ์„ ๋†’์ด๊ณ ์ž ํ•ฉ๋‹ˆ๋‹ค.

Key changes โœ…

  • ์˜์ƒ์ •๋ณด (VIDEO ํ…Œ์ด๋ธ”) ์•ฝ 6๋งŒ๊ฑด์˜ ๋”๋ฏธ๋ฐ์ดํ„ฐ๋ฅผ Youtube Data API๋ฅผ ํ†ตํ•ด ์ถ”๊ฐ€ํ•˜์˜€์Šต๋‹ˆ๋‹ค
    image
  • ๋ฏธ๋ฆฌ ์ •ํ•ด๋‘์—ˆ๋˜ ์ธ๋ฑ์Šค๋“ค์„ ์ ์šฉ์‹œํ‚ต๋‹ˆ๋‹ค.
  • VIDEO ๋”๋ฏธ๋ฐ์ดํ„ฐ๋ฅผ ํ†ตํ•ด ์„ฑ๋Šฅ์„ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค (MariaDB Profiling)

To reviewers ๐Ÿ™

  • ์ธ๋ฑ์Šค๋ฅผ ์ ์ ˆํžˆ ์„ค์ •ํ–ˆ๋Š”์ง€ ๋ด์ฃผ์„ธ์š”


์ธ๋ฑ์Šค ์ ์šฉ ์ด์œ 

  1. ๊ฒ€์ƒ‰ ์„ฑ๋Šฅ ํ–ฅ์ƒ: WHERE ์ ˆ์—์„œ ํŠน์ • ์นผ๋Ÿผ์„ ์ด์šฉํ•œ ์กฐ๊ฑด ๊ฒ€์ƒ‰์ด ๋นจ๋ผ์ง‘๋‹ˆ๋‹ค. ์ธ๋ฑ์Šค๊ฐ€ ์—†์œผ๋ฉด ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์—”์ง„์€ ํ…Œ์ด๋ธ” ์ „์ฒด๋ฅผ ์Šค์บ”ํ•˜์—ฌ ์กฐ๊ฑด์— ๋งž๋Š” ํ–‰์„ ์ฐพ์•„์•ผ ํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ธ๋ฑ์Šค๊ฐ€ ์žˆ๋‹ค๋ฉด ์ธ๋ฑ์Šค๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋” ๋น ๋ฅด๊ฒŒ ๊ฒ€์ƒ‰์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.
  2. ์กฐ์ธ ์„ฑ๋Šฅ ํ–ฅ์ƒ: JOIN ์ ˆ์—์„œ ์กฐ์ธํ•  ๋•Œ ์ธ๋ฑ์Šค๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ, ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์—”์ง„์€ ์ธ๋ฑ์Šค๋ฅผ ์ด์šฉํ•˜์—ฌ ์กฐ์ธ์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Š” ๋‘ ํ…Œ์ด๋ธ” ๊ฐ„์˜ ๋งค์นญ๋˜๋Š” ํ–‰์„ ํšจ์œจ์ ์œผ๋กœ ์ฐพ์„ ์ˆ˜ ์žˆ๋„๋ก ๋„์™€์ค๋‹ˆ๋‹ค.
    • ๋ฐ์ดํ„ฐ๊ฐ€ ์ด๋ฏธ ์ •๋ ฌ๋˜์–ด์žˆ๋Š”์ง€ ํ™•์ธํ•˜์—ฌ ์ •๋ ฌ๋ณ‘ํ•ฉ์กฐ์ธ์˜ ์„ฑ๋Šฅ์ด ์˜ฌ๋ผ๊ฐˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  3. ์ •๋ ฌ ๋ฐ ๊ทธ๋ฃนํ™” ์„ฑ๋Šฅ ํ–ฅ์ƒ: ์ธ๋ฑ์Šค๋Š” ์ •๋ ฌ ๋ฐ ๊ทธ๋ฃนํ™” ์ž‘์—…์—๋„ ๋„์›€์„ ์ค๋‹ˆ๋‹ค. ํŠนํžˆ, **ORDER BY**๋‚˜ GROUP BY ์ ˆ์—์„œ ์ธ๋ฑ์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ •๋ ฌ์ด๋‚˜ ๊ทธ๋ฃนํ™” ์ž‘์—…์ด ๋นจ๋ผ์ง‘๋‹ˆ๋‹ค.
  4. ๋ฒ”์œ„ ๊ฒ€์ƒ‰ ์ตœ์ ํ™”: ์ผ๋ถ€ ์ธ๋ฑ์Šค๋Š” ๋ฒ”์œ„ ๊ฒ€์ƒ‰์— ํŠนํžˆ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ๋‚ ์งœ ๋ฒ”์œ„๋‚˜ ์ˆซ์ž ๋ฒ”์œ„์— ๋Œ€ํ•œ ๊ฒ€์ƒ‰์—์„œ ์ธ๋ฑ์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ํšจ์œจ์ ์œผ๋กœ ์›ํ•˜๋Š” ๊ฒฐ๊ณผ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ธ๋ฑ์Šค ์ ์šฉํ•˜๊ธฐ

  • VIDEO ๋ฐ์ดํ„ฐ๋ฅผ ์กฐํšŒํ•˜๋Š” ์ฟผ๋ฆฌ๋Š” GET_BY_CODE, GET_BY_ID, GET_TOP_RATED_ORDER, GET_LIST_BY_PLAYLIST_ID, GET_CODE_SET_BY_MEMBER_ID, GET_RANDOM_BY_CHANNEL, GET_VIEW_COUNT_ORDER_BY_CHANNEL, GET_LATEST_ORDER_BY_CHANNEL ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ์ด ์ค‘ CHANNEL์„ ๊ธฐ์ค€์œผ๋กœ SELECT ํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์œผ๋‹ˆ, ๋‹ค์Œ SQL์„ ํ†ตํ•ด CHANNEL ์นผ๋Ÿผ ์ธ๋ฑ์Šค๋ฅผ ์ƒ์„ฑํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.
    --์ธ๋ฑ์Šค ์ƒ์„ฑ
    CREATE INDEX channel ON VIDEO ( channel );
  • ๋‹ค์Œ SQL์„ ํ†ตํ•ด ์ธ๋ฑ์Šค๋ฅผ ํ™•์ธ ํ•ด ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
    --์ธ๋ฑ์Šค ํ™•์ธ
    SHOW INDEX FROM VIDEO;
  • ๊ฒฐ๊ณผ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. PK๋กœ ๊ธฐ๋ณธ์ ์œผ๋กœ ์ƒ์„ฑ๋˜๋Š” ์ธ๋ฑ์Šค๋„ ํ™•์ธํ•ด ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
    image
    image
  • EXPLAIN ์„ ํ†ตํ•ด ์ธ๋ฑ์Šค๋ฅผ ์ž˜ ํƒ€๋Š”์ง€ ํ™•์ธํ•ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.
    EXPLAIN
    SELECT * FROM VIDEO
    WHERE channel = '์–ด๋ฐ”์›ƒ์‡ผํ•‘'
    ORDER BY view_count DESC
    LIMIT 10;
  • ๋‹ค์Œ๊ณผ ๊ฐ™์ด channel ์ด๋ผ๋Š” ์ธ๋ฑ์Šค๋ฅผ ์‚ฌ์šฉํ•œ ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
    image
    image
    • id: ๊ฐ ์ฟผ๋ฆฌ ๋ธ”๋ก ๋˜๋Š” ์„œ๋ธŒ์ฟผ๋ฆฌ์— ๋Œ€ํ•ด ๋ถ€์—ฌ๋œ ๊ณ ์œ ํ•œ ์‹๋ณ„์ž. ์—ฌ๋Ÿฌ ์„œ๋ธŒ์ฟผ๋ฆฌ๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ ๊ณ„์ธต์ ์œผ๋กœ ํ‘œํ˜„๋ฉ๋‹ˆ๋‹ค.
    • select_type: ์ฟผ๋ฆฌ์˜ ์œ ํ˜•์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. "SIMPLE"์€ ๋‹จ์ˆœํ•œ SELECT ์ฟผ๋ฆฌ๋ฅผ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค.
    • table: ์ฟผ๋ฆฌ๊ฐ€ ์ฐธ์กฐํ•˜๋Š” ํ…Œ์ด๋ธ”์˜ ์ด๋ฆ„.
    • type: ํ…Œ์ด๋ธ”์—์„œ ๋ ˆ์ฝ”๋“œ๋ฅผ ์ฝ๋Š” ๋ฐฉ๋ฒ•์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ "ref"๋Š” ์ธ๋ฑ์Šค๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์—ฌ๋Ÿฌ๊ฐœ์˜ ํ–‰์„ ์Šค์บ”ํ•˜์˜€์Œ์„ ๋งํ•ฉ๋‹ˆ๋‹ค. ํ•œ๊ฐœ๋ผ๋ฉด eq_ref์ผ ๊ฒƒ์ž…๋‹ˆ๋‹ค. "const" ๋Š” ์ธ๋ฑ์Šค๋ฅผ ์‚ฌ์šฉํ•ด 1๊ฑด์„ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ์Šค์บ”, "range"๋Š” ์ธ๋ฑ์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฒ”์œ„ ์Šค์บ”์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.
    • possible_keys: ์ฟผ๋ฆฌ์—์„œ ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ๋Š” ์ธ๋ฑ์Šค ๋ชฉ๋ก. ์—ฌ๊ธฐ์„œ๋Š” channel ์ธ๋ฑ์Šค๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Œ์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค.
    • key: ์‹ค์ œ๋กœ ์„ ํƒ๋œ ์ธ๋ฑ์Šค. ์—ฌ๊ธฐ์„œ๋Š” channel ์ธ๋ฑ์Šค๊ฐ€ ์„ ํƒ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.
    • Extra: ๊ธฐํƒ€ ์ •๋ณด. ์—ฌ๊ธฐ์„œ **"Using index condition"**์€ ์ธ๋ฑ์Šค ์กฐ๊ฑด์„ ์‚ฌ์šฉํ•˜์—ฌ ํ•„ํ„ฐ๋ง ๋˜์—ˆ์Œ์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค.

์นด๋””๋„๋ฆฌํ‹ฐ ์ˆ˜์น˜

  • ์นด๋””๋„๋ฆฌํ‹ฐ ์ˆ˜์น˜๋Š” ํŠน์ • ์นผ๋Ÿผ์ด๋‚˜ ๊ด€๊ณ„์—์„œ ๊ณ ์œ ํ•œ ๊ฐ’์„ ๊ฐ€์ง€๋Š” ์ •๋„์ž…๋‹ˆ๋‹ค. ์‚ฌ๋žŒ์˜ ์ด๋ฆ„์ด ์‚ฌ๋žŒ์˜ ์„ฑ๋ณ„๋ณด๋‹ค ์นด๋””๋„๋ฆฌํ‹ฐ ์ˆ˜์น˜๊ฐ€ ๋†’์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค.
  • JOIN, WHERE์ ˆ์— ์—ฌ๋Ÿฌ ์นผ๋Ÿผ์ด ์žˆ๋‹ค๋ฉด, ์นด๋””๋„๋ฆฌํ‹ฐ ์ˆ˜์น˜๊ฐ€ ๋†’์€ ์นผ๋Ÿผ์„ ์ธ๋ฑ์Šค๋กœ ์„ค์ •ํ•ด์•ผ ํšจ์œจ์ ์ผ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋‹ค์Œ ์ฟผ๋ฆฌ๋ฅผ ํ†ตํ•ด ์นด๋””๋„๋ฆฌํ‹ฐ ์ˆ˜์น˜๋ฅผ ๊ณ„์‚ฐํ•ด ๋ดค์Šต๋‹ˆ๋‹ค.
    SELECT
    CONCAT(ROUND(COUNT(DISTINCT video_id) / COUNT(*) * 100, 2), '%') AS id_cardinality,
    CONCAT(ROUND(COUNT(DISTINCT video_code) / COUNT(*) * 100, 2), '%') AS video_code_cardinality,
    CONCAT(ROUND(COUNT(DISTINCT channel) / COUNT(*) * 100, 2), '%') AS channel_cardinality,
    CONCAT(ROUND(COUNT(DISTINCT title) / COUNT(*) * 100, 2), '%') AS title_cardinality,
    CONCAT(ROUND(COUNT(DISTINCT duration) / COUNT(*) * 100, 2), '%') AS duration_cardinality,
    CONCAT(ROUND(COUNT(DISTINCT published_at) / COUNT(*) * 100, 2), '%') AS publishedAt_cardinality,
    CONCAT(ROUND(COUNT(DISTINCT language) / COUNT(*) * 100, 2), '%') AS language_cardinality,
    CONCAT(ROUND(COUNT(DISTINCT view_count) / COUNT(*) * 100, 2), '%') AS viewCount_cardinality
    FROM VIDEO;
  • ๊ฒฐ๊ณผ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๊ณ ์œ ํ•œ id์™€ code๋Š” 100%์ธ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๊ณ , ํ•˜๋‚˜์˜ ์ฑ„๋„์ด ์—ฌ๋Ÿฌ ์˜์ƒ์„ ๊ฐ€์งˆ ์ˆ˜ ์žˆ์œผ๋‹ˆ channel์€ 68% ์ •๋„์ž…๋‹ˆ๋‹ค.
    image
    image

@D-w-nJ D-w-nJ added the enhancement New feature or request label Apr 4, 2024
@D-w-nJ D-w-nJ self-assigned this Apr 4, 2024
@D-w-nJ D-w-nJ linked an issue Apr 4, 2024 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[SWM-427] Apply Predefined Indexes and Verify Performance
1 participant