디비 부하 해당쿼리 튜닝방법 있을까요?
본문
select * from g5_write_free where wr_parent = '265004' and wr_is_comment = 1 order by wr_comment , wr_comment_reply
select count(*) as cnt from g5_write_free where mb_id = 'qwerer' and wr_is_comment = '1' and date_format(wr_datetime, '%Y-%m-%d') = '2023-01-19'
select * from g5_write_gallery where wr_parent = '17574' and wr_is_comment = 1 order by wr_comment , wr_comment_reply
이것들때문에 부하가 상당한데.
이거 튜닝이나 개선방법 있을까요?ㅠㅠ
답변 4
기본적으로 게시물 자체가 엄청나게 많은가 보네요..
index 를 잘 활용하면 도움이 될 것 같습니다.
기본적으로 아래의 내용을 참고해서 테이블을 최적화 하는 방법 밖에 없을 것입니다.
https://dev.mysql.com/doc/refman/8.0/en/optimizing-database-structure.html
현재 인덱스가 어떻게 구성되어 있나요
optimize g5_write_free ;
optimize g5_write_gallery ;
를 한번 실행해보세요
그리고 날짜비교는
date(wr_datetime) = '2023-01-19' 로 하는것도 방법입니다.
index를 하나 추가하자면
wr_id , wr_is_comment , wr_datetime
이 묶인걸 하나 추가해보세요
그래도 좀 아쉬우면
wr_comment , wr_comment_reply 묶인index 하나더 추가후 이상없으면 그대로 적용
별차이 없으면 제거
index는 너무 많아도 안되지만 너무 난해해도 안되니 최대한 필요한것만 간결하게 적용하세요
* 인덱스 추가
1. wr_parent, wr_is_comment
2. mb_id, wr_is_comment
* 2번째 쿼리 변경
select count(*) as cnt from g5_write_free where mb_id = 'qwerer' and wr_is_comment = '1' and date_format(wr_datetime, '%Y-%m-%d') = '2023-01-19'
->
select count(*) as cnt from g5_write_free where mb_id = 'qwerer' and wr_is_comment = '1' and (wr_datetime between '2023-01-19 00:00:00' and '2023-01-19 23:59:59')