잘못된 인덱스 사용으로 인해 장시간 Mysql 프로세스가 물리는 현상이 발생 할 경우
어떤 쿼리문이 이런 현상을 야기 시켰느냐에 대한 분석 시
Slow Query 를 사용하시면 손 쉽게 부하가 증가되는 쿼리를 찾으실 수 있으실 것입니다.
<사용 방법>
1. mysql 환경 설정 파일인 my.cnf 파일에 아래 내용을 추가 하시면 됩니다.
[mysqld]
long_query_time=초제한(1~10)
log-slow-queries=로그파일명
ex)
[mysqld]
log-slow-queries = /temp/mysql-slow.log
long_query_time = 3
위의 의미는 쿼리타임이 3초를 초과하는 쿼리에 대해 /temp/mysql-slow.log 파일에 로그를 남기라는 의미 입니다.
2. mysql 구동시 적용하는 방법이 있습니다.
$ mysqld_safe --datadir=데이터디렉토리 --log-slow-queries[=file_name
] &
< 로그 파일의 내용 >
# Time: 080218 13:25:06
# User@Host: XXXXX[xxxxx] @ localhost [127.0.0.1]
# Query_time: 8 Lock_time: 0 Rows_sent: 1 Rows_examined: 98767
use CrediMail;
SELECT count(*) FROM test
위의 양식으로 slow 쿼리가 남게 됩니다.
slow log 로 남은 쿼리들은 explain 을 사용하여 정상적인 index를 타고 있는지 체크 해 보는 방법으로
문제점을 찾아 가시면 될 것입니다.
ex) mysql>explain 문제시 되는 쿼리;
<참고>
Query_time : 쿼리 처리 시간
Lock_time : lock 이 걸린 횟수
Row_sent : 조회 결과 Row 수
Rows_examined : 조회 대상 Row 수
Reference URL
http://dev.mysql.com/doc/refman/5.0/en/slow-query-log.html
http://www.sitepoint.com/forums/showthread.php?t=397521
1. Check the gloval variables
show global variables WHERE Variable_name LIKE ‘l%’;
2. Configuration
[mysql.server]
user=mysql
basedir=/var/lib# added 2007/10/05
#set-variable = long_query_time=3
#log-slow-queries=/var/log/mysqld-slow.log
Usage: mysqldumpslow [ OPTS... ] [ LOGS... ]
Parse and summarize the MySQL slow query log. Options are
--verbose verbose
--debug debug
--help write this text to standard output
용도 : slow query log 에서 실행 시간이 일정 시간 이상이 되는 쿼리 와 일정 row 이상 access하는 쿼리만 filter 해 낼 수 있음
cat ‘slow query log’ msql_slow_log_filter -T timesec -R numrows ( T, R option은 단독으로 사용할 수 있음 )
cat kidsmbbs1-slow.log | msql_slow_log_filter -R 400000 ( 400000 row 이상 access 하는 쿼리만 추출 )
SELECT /*!40001 SQL_NO_CACHE */ * FROM `tb_comment_121`;
# Query_time: 4 Lock_time: 0 Rows_sent: 31864 Rows_examined: 455198
select distinct code from tb_comment_121;
# Query_time: 5 Lock_time: 0 Rows_sent: 455596 Rows_examined: 455596
SELECT /*!40001 SQL_NO_CACHE */ * FROM `tb_comment_121`;
# Query_time: 4 Lock_time: 0 Rows_sent: 31905 Rows_examined: 455596
select distinct code from tb_comment_121;
cat kidsmbbs1-slow.log | msql_slow_log_filter -T 8 ( 8초 이상 걸리는 쿼리만 추출 )
SELECT /*!40001 SQL_NO_CACHE */ * FROM `tb_bbs_154`;
# Query_time: 8 Lock_time: 0 Rows_sent: 1 Rows_examined: 329849
select count(indexno) as su from tb_comment_160 where wdate>=1173020400 and wdate<=1173106800;
# Query_time: 15 Lock_time: 0 Rows_sent: 37014 Rows_examined: 37014
SELECT /*!40001 SQL_NO_CACHE */ * FROM `tb_bbs_154`;
# Query_time: 14 Lock_time: 0 Rows_sent: 37014 Rows_examined: 37014
SELECT /*!40001 SQL_NO_CACHE */ * FROM `tb_bbs_154`;
### Total time: 12, Average time: 3
### Taking 3 , 3 , 3 , 3 seconds to complete
### Rows analyzed 144673, 150153, 185656 and 189447
SELECT /*!XXX SQL_NO_CACHE */ * FROM `tb_bbs_limit`;
SELECT /*!40001 SQL_NO_CACHE */ * FROM `tb_bbs_limit`;
### Total time: 10, Average time: 5
### Taking 5 , 5 seconds to complete
### Rows analyzed 78261 and 81439
SELECT /*!XXX SQL_NO_CACHE */ * FROM `tb_bbs_XXX`;