인덱스 튜닝

품절 soldout 뒤쪽으로 문제

gw1 2025. 10. 28. 15:57

CREATE TABLE `product` (
  `id` int unsigned NOT NULL AUTO_INCREMENT,
  `category_id` int unsigned NOT NULL,
  `title` varchar(200) NOT NULL,
  `content` text,
  `created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `soldout` varchar(10) DEFAULT 'N',
  `hit` int unsigned DEFAULT '0' COMMENT '조회수',
  PRIMARY KEY (`id`),
  KEY `idx_category_hit` (`category_id`,`hit`),
  KEY `idx_category_soldout_hit` (`category_id`,`soldout`,`hit`)
) ENGINE=InnoDB AUTO_INCREMENT=1000001 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci

 

 
총 상품은 100만개로 데이터 세팅
카테고리 분포는 균등합니다.
 
기존 쿼리

SELECT *
FROM product
WHERE category_id = 5
ORDER BY hit DESC
LIMIT 10, 10;

  
의심할 여지가 없이
KEY `idx_category_hit` (`category_id`,`hit`)로
 
0.01초 내로 쿼리가 실행됨


어느 날 기획자에게 아래와 같은 요청 사항이 들어옵니다.
 
페이지에서 품절된 상품 (soldout = Y) 은 페이징 맨 뒤로 좀 보내주세요.
 
기존

soldout= N , hit = 999soldout= Y , hit = 988soldout= N , hit = 970soldout= Y , hit = 800soldout= N , hit = 600

 
변경 후

soldout= N , hit = 999soldout= N , hit = 970soldout= N , hit = 600soldout= Y , hit = 988soldout= Y , hit = 800

 
알파벳 정렬 순서 N -> Y 오름차순에 따라
 

SELECT *
FROM product
WHERE category_id = 5
ORDER BY
    soldout ASC,
    hit DESC
LIMIT 10, 10;

 
soldout을 추가하였음
 

 
실행 계획

 
13초나 걸려서 저 상태로는 서비스 배포가 불가능한 상태

 
1. 해당 쿼리가 느린 이유를 설명하세요.(반드시 이유를 설명하는게 핵심)
 
2. 해결 방안을 제시하세요.