RadarURL

데이터베이스
2015.03.16 20:25

Mysql Slow query log 등록

조회 수 1575 추천 수 0 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄

대량의 데이터를 조회하거나

잘못된 인덱스 사용으로 인해 장시간 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

 
 
Setting my.cnf for log slow queries
 

MySQL을 운용하다보면 궁극적으로 봉착하는 문제가 바로 Slow queriy 다. 이 Slow query 야말로 모둔 query의 적이라 해도 과언이 아니라고 생각한다. MySQL에서는이 Slow query를 log로 기록할수 있게 되어있는데 이 설정이 꽤나 delicate 하다. 다음은 MySQL 5.0에서 동작하는 설정이다.

1. Check the gloval variables

in the mysql command prompt,

show global variables WHERE Variable_name LIKE ‘l%’;

+———————————+—————————+
| Variable_name | Value |
+———————————+—————————+
| language | /usr/share/mysql/english/ |
| large_files_support | ON |
| large_page_size | 0 |
| large_pages | OFF |
| license | GPL |
| local_infile | ON |
| locked_in_memory | OFF |
| log | OFF |
| log_bin | OFF |
| log_bin_trust_function_creators | OFF |
| log_error | |
| log_slave_updates | OFF |
| log_slow_queries | ON |
| log_warnings | 1 |
| long_query_time | 3 |
| low_priority_updates | OFF |
| lower_case_file_system | OFF |
| lower_case_table_names | 0 |
+———————————+—————————+

2. Configuration

[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Default to using old password format for compatibility with mysql 3.x
# clients (those using the mysqlclient10 compatibility package).
old_passwords=1

# added 1007/10/05
max_connections = 10000
table_cache = 256
wait_timeout = 900
max_connect_errors = 10000
key_buffer = 16M
sort_buffer = 1M
set-variable = long_query_time=3
log-slow-queries=/var/log/mysqld-slow.log

[mysql.server]
user=mysql
basedir=/var/lib

[mysqld_safe]
log-error=/var/log/mysqld.log
#log-slow-queries=/var/log/mysqld-slow.log
pid-file=/var/run/mysqld/mysqld.pid

# 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

  -v           verbose
  -d           debug
  -s ORDER     what to sort by (t, at, l, al, r, ar etc), 'at' is default
  -r           reverse the sort order (largest last instead of first)
  -t NUM       just show the top n queries
  -a           don't abstract all numbers to N and strings to 'S'
  -n NUM       abstract numbers with at least n digits within names
  -g PATTERN   grep: only consider stmts that include this string
  -h HOSTNAME  hostname of db server for *-slow.log filename (can be wildcard),
               default is '*', i.e. match all
  -i NAME      name of server instance (if using mysql.server startup script)
  -l           don't subtract lock time from total time

 

 

Slow query filter

http://www.mysqlperformanceblog.com/files/utils/mysql_slow_log_filter


용도  : slow query log 에서 실행 시간이 일정 시간 이상이 되는 쿼리 와 일정 row 이상 access하는 쿼리만 filter 해 낼 수 있음

 

사용법

 

cat slow query log msql_slow_log_filter -T timesec -R numrows  ( T, R option은 단독으로 사용할 수 있음 )

Ex)

cat  kidsmbbs1-slow.log | msql_slow_log_filter  -R 400000  ( 400000 row 이상 access 하는 쿼리만 추출 )

 

SELECT /*!40001 SQL_NO_CACHE */ * FROM `tb_comment_121`;

# Time: 070307  5:01:11

# Query_time: 4  Lock_time: 0  Rows_sent: 31864  Rows_examined: 455198

select distinct code from tb_comment_121;

# Time: 070307  5:06:17

# Query_time: 5  Lock_time: 0  Rows_sent: 455596  Rows_examined: 455596

SELECT /*!40001 SQL_NO_CACHE */ * FROM `tb_comment_121`;

# Time: 070308  5:01:09

# 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`;

# Time: 070306  5:00:40

# 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;

# Time: 070306 14:13:22

# Query_time: 15  Lock_time: 0  Rows_sent: 37014  Rows_examined: 37014

SELECT /*!40001 SQL_NO_CACHE */ * FROM `tb_bbs_154`;

# Time: 070307  5:00:40

# Query_time: 14  Lock_time: 0  Rows_sent: 37014  Rows_examined: 37014

SELECT /*!40001 SQL_NO_CACHE */ * FROM `tb_bbs_154`;

 

Aggregating slow query log

http://www.mysqlperformanceblog.com/files/utils/mysql_slow_log_parser

 

용도 : slow query log 에 있는 slow query에 대해서 aggregate 한 정보를 보여줌, 즉 실행 횟수, 평균 수행 시간, 총 수행 시간 , access rows  , 개인적으로 mysqldumpslow 보다 보기  좋은 것 같습니다.

 

### 4 Queries

### 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`;

 

 

### 2 Queries

### Total time: 10, Average time: 5

### Taking 5 , 5  seconds to complete

### Rows analyzed 78261 and 81439

use ykids_bbs;

SELECT /*!XXX SQL_NO_CACHE */ * FROM `tb_bbs_XXX`;


출처 : http://leepfe.tistory.com/163

TAG •
?

공부 게시판

공부에 도움되는 글을 올려주세요.

  1. No Image notice by 처누 2003/08/18 by 처누
    Views 952963 

    [공지] 공부 게시판 입니다.

  2. [Rocky Linux 9] /home 용량 줄이고 /root 용량 늘리기

  3. [Rocky Linux] LVM 환경에서 /root 파티션 공간 부족 해결

  4. 일반적인 Htaccess 301 리디렉션 규칙

  5. [Linux/Rocky] SSH Root 로그인하는 방법 (root 접속 허용)

  6. MariaDB my.cnf 설정 파일

  7. MariaDB 설정 파일 개요와 구조

  8. 윈도우11 비밀번호 분실시 설정 변경방법 (Windows10 포함)

  9. 리눅스 서버 설치 중 에러 "Failed to find a suitable stage1 device"

  10. Linux 11 . Linux 설치 시 lvm 수동설정

  11. [Rocky Linux] 누구나 쉽게 따라하는 Rocky Linux 9.0 OS 다운로드 및 설치 방법~!!

  12. Linux/Rocky Linux Rocky Linux : Composer 설치

  13. [Rocky Linux] 록키 리눅스 최신 업데이트 적용 방법

  14. rocky linux 커널 업데이트

  15. [Windows] OWASP ZAP 사용법

  16. [웹 취약점] 웹서버 디렉토리 리스팅 방지

  17. Clonezilla(클론질라) 백업파일 하드디스크로 복구 방법

  18. 클론질라 사용 방법 - OS 이미지 백업 CloneZilla

  19. [Linux / Rocky] FTP 접속 관련 설정 실습 예제

  20. Could not reliably determine the server's fully qualified domain name

  21. [아파치] 아파치 웹서버에 .htaccess 파일 설정하는 방법

Board Pagination Prev 1 2 3 4 5 6 7 8 9 10 ... 35 Next
/ 35


즐겨찾기 (가족)

JAESOO's HOMEPAGE


장여은 홈페이지


장여희 홈페이지


장여원 홈페이지


즐겨찾기 (업무)

알리카페 홀릭

숭실대 컴퓨터 통신연구실 (서창진)

말레이시아 KL Sentral 한국인 GuestHouse


즐겨찾기 (취미)

어드민아이디

유에코 사랑회

아스가르드 좋은사람/나쁜사람

JServer.kr

제이서버 메타블로그

재수 티스토리


즐겨찾기 (강의, 커뮤니티)

재수 강의 홈페이지


한소리


VTMODE.COM


숭실대 인공지능학과


숭실대 통신연구실


베너