Tags

๐Ÿง‘โ€๐Ÿ’ป MySQL ๋ฌดํ•œ์Šคํฌ๋กค ํŽ˜์ด์ง• ์ฒ˜๋ฆฌํ•˜๊ธฐ

namjug-kim โ€ข 2019๋…„ 2์›” 15์ผ
mysql

์†Œ๊ฐœ

์ด๋ฒˆ ์‹œ๊ฐ„์— ํšŒ์‚ฌ์—์„œ ๋ฉ”ํƒ€ํด๋ผ์šฐ๋“œ(๊ตฌ๊ธ€, ๋“œ๋กญ๋ฐ•์Šค, ์›๋“œ๋ผ์ด๋ธŒ)๋ฅผ ํ•˜๋‚˜์˜ ํด๋ผ์šฐ๋“œ๋กœ ์—ฐ๊ฒฐ์‹œ์ผœ์ฃผ๋Š” ํ”„๋กœ์ ํŠธ๋ฅผ ์ง„ํ–‰ํ•˜๋ฉด์„œ ์‚ฌ์šฉํ–ˆ๋˜ ์ปค์„œ๋ฅผ ์ด์šฉํ•œ ๋”๋ณด๊ธฐ ๋ฐฉ์‹์˜ ํŽ˜์ด์ง• ๊ฐœ๋ฐœ ๊ฒฝํ—˜์„ ๊ณต์œ ํ•˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

๋ฌธ์ œ์ 

๋”๋ณด๊ธฐ ํŽ˜์ด์ง• limit, offset์œผ๋กœ๋Š” ์•ˆ๋˜๋Š”๊ฑฐ์•ผ?

๊ธฐ์กด์˜ ๋”๋ณด๊ธฐ๋ฅผ ์ง€์›ํ•˜๊ธฐ ์œ„ํ•œ API๋Š” MySQL์˜ limit, offset์„ ์ด์šฉํ•œ ํŽ˜์ด์ง•์œผ๋กœ ๊ตฌํ˜„์ด ๋˜์–ด์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ๋ฉ”ํƒ€ํด๋ผ์šฐ๋“œ์˜ ๋ณ€๊ฒฝ์‚ฌํ•ญ ๋™๊ธฐํ™”์™€ ๋‹ค์Œ ํŽ˜์ด์ง€ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ๊ฐ€ ๋™์‹œ์— ์‹คํ–‰๋ ๋•Œ ์ค‘๋ณต๋˜๋Š” ํ•ญ๋ชฉ๋“ค์ด ์กด์žฌํ•˜๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

1. ์ฒซ ํŽ˜์ด์ง€ ๋กœ๋”ฉ

SELECT id, file_name
FROM file
ORDER BY file_name asc
LIMIT 5
OFFSET 0
๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ
1, B
2, B
3, B
4, B
5, B

2. A๋ผ๋Š” ํŒŒ์ผ๋ช…์˜ ํŒŒ์ผ 5๊ฐœ ์ƒ์„ฑ

3. ๋‘๋ฒˆ์งธ ํŽ˜์ด์ง€ ๋กœ๋”ฉ

SELECT id, file_name
FROM file
ORDER BY file_name asc
LIMIT 5
OFFSET 5
๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ
1, B
2, B
3, B
4, B
5, B

๊ฒฐ๊ณผ์ ์œผ๋กœ ํด๋ผ์ด์–ธํŠธ์—์„œ๋Š” B๋ผ๋Š” ํŒŒ์ผ๋ช…์ด 10๊ฐœ ์กด์žฌํ•˜๋Š”๊ฒƒ์ฒ˜๋Ÿผ ๋ฆฌ์ŠคํŠธ์— ๋ณด์ด๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

ํ•ด๊ฒฐ์‹œ๋„

์ •๋ ฌ์กฐ๊ฑด์„ ๊ฒ€์ƒ‰์กฐ๊ฑด์— ๋„ฃ์–ด ๋งˆ์ง€๋ง‰ ํ•ญ๋ชฉ ์ดํ›„์˜ ๋ฆฌ์ŠคํŠธ ๊ฐ€์ ธ์˜ค๊ธฐ

1์ฐจ์ ์œผ๋กœ ์ •๋ ฌ์กฐ๊ฑด์„ ๊ฒ€์ƒ‰ ์กฐ๊ฑด์— ๋„ฃ์–ด์„œ ๋งˆ์ง€๋ง‰ ํ•ญ๋ชฉ ์ดํ›„์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ๊ฐ€์ ธ์˜ค๋„๋ก API์™€ ์ฟผ๋ฆฌ๋ฅผ ๋ณ€๊ฒฝํ•˜์˜€์Šต๋‹ˆ๋‹ค.

๋‹ค์ŒํŽ˜์ด์ง€๋ฅผ ๊ฐ€์ ธ์˜ฌ๋•Œ ์ด์ „ํŽ˜์ด์ง€์˜ ๋งˆ์ง€๋ง‰ ํ•ญ๋ชฉ์—์„œ ์ •๋ ฌ์กฐ๊ฑด์— ํ•ด๋‹นํ•˜๋Š” ์ •๋ณด ์— ํ•ด๋‹นํ•˜๋Š” ์ปค์„œ ๋ฐ์ดํ„ฐ๋ฅผ ํ†ตํ•ด ๋‹ค์ŒํŽ˜์ด์ง€๋ฅผ ๊ฒ€์ƒ‰ํ•˜์˜€์Šต๋‹ˆ๋‹ค.

SELECT id, file_name
FROM file
WHERE file_name > ${cursor}
ORDER BY file_name asc
LIMIT 2

์ด๋ ‡๊ฒŒ ํ•˜๋‹ค๋ณด๋‹ˆ ์•„๋ž˜์™€ ๊ฐ™์€ ํ…Œ์ŠคํŠธ๋ฐ์ดํ„ฐ์—์„œ ์ •๋ ฌ์กฐ๊ฑด์— ๊ฑธ๋ฆฌ๋Š” ๋ฐ์ดํ„ฐ๊ฐ€ ์œ ์ผํ•˜์ง€ ์•Š์€ ๊ฒฝ์šฐ ๋ˆ„๋ฝ์ด ๋ฐœ์ƒํ•˜๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ

id, file_name
1, A
2, A
3, A
4, B
5, B
6, B

์ฒซ๋ฒˆ์งธ ํŽ˜์ด์ง€

SELECT id, file_name
FROM file
LIMIT 2
1, A
2, A
์ปค์„œ : A

๋‘๋ฒˆ์งธ ํŽ˜์ด์ง€

SELECT id, file_name
FROM file
WHERE file_name > 'A'
LIMIT 2
4, B
5, B
์ปค์„œ : B

์œ„์™€ ๊ฐ™์ด ๋ฐ์ดํ„ฐ๋ฅผ 2๊ฐœ์”ฉ ํŽ˜์ด์ง• ํ•˜๊ณ  ์‹ถ์„ ๋•Œ 2๋ฒˆ์งธ ํŽ˜์ด์ง€์—์„œ file_name > A ์˜ ์กฐ๊ฑด์ด ๋“ค์–ด๊ฐ€๊ฒŒ ๋˜๋ฏ€๋กœ 3, A ํŒŒ์ผ์ด ๋ˆ„๋ฝ๋˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

ํ•ด๊ฒฐ

์œ ์ผํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ์ •๋ ฌ์กฐ๊ฑด์œผ๋กœ ๋งŒ๋“ค์ž

๋ˆ„๋ฝ๋˜๋Š” ๋ฐ์ดํ„ฐ๊ฐ€ ์—†๋„๋ก ํ•˜๊ธฐ ์œ„ํ•ด์„œ ์ •๋ ฌ ์กฐ๊ฑด์— ์œ ์ผํ•œ ๊ฐ’(์ผ๋ฐ˜์ ์œผ๋กœ PK)์„ 2์ฐจ ์ •๋ ฌ ์กฐ๊ฑด์œผ๋กœ ์ถ”๊ฐ€ํ•˜์˜€์Šต๋‹ˆ๋‹ค.

SELECT id, file_name
FROM file
WHERE file_name > ${lastCursorFileName}
    OR (file_name = ${lastCursorFileName} AND id > ${lastCursorId})
ORDER BY file_name ASC, id ASC
LIMIT 2

์ฒซ๋ฒˆ์งธ ํŽ˜์ด์ง€

SELECT id, file_name
FROM file
LIMIT 2
1, A
2, A
์ปค์„œ : 2, A

๋‘๋ฒˆ์งธ ํŽ˜์ด์ง€

SELECT id, file_name
FROM file
WHERE file_name > 'A'
    OR (file_name = 'A' AND id > 2)
ORDER BY file_name ASC, id ASC
LIMIT 2
3, A
4, B
์ปค์„œ : 4, B

์„ธ๋ฒˆ์งธ ํŽ˜์ด์ง€

SELECT id, file_name
FROM file
WHERE file_name > 'B'
    OR (file_name = 'B' AND id > 4)
ORDER BY file_name ASC, id ASC
LIMIT 2
5, B
6, B
์ปค์„œ : 6, B

์œ„์™€๊ฐ™์ด ์œ ์ผํ•œ ์ •๋ ฌ์กฐ๊ฑด์ธ id๋ฅผ ์ถ”๊ฐ€ํ•จ์œผ๋กœ์จ ๋ˆ„๋ฝ๊ณผ ์ค‘๋ณต์ด ๋ฐœ์ƒํ•˜์ง€ ์•Š๋Š” ์ปค์„œ๋ฐฉ์‹์˜ ๋”๋ณด๊ธฐ๋ฅผ ๊ตฌํ˜„ ํ•  ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.

์ข…ํ•ฉ

์œ ์ผํ•œ ์ •๋ ฌ ์กฐ๊ฑด์„ ์ด์šฉํ•˜์—ฌ where ์กฐ๊ฑด์— ๋„ฃ์Œ์œผ๋กœ์จ ์ค‘๋ณต์ด ๋ฐœ์ƒํ•˜์ง€ ์•Š๋Š” offset์„ ์ฐพ์„ ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.