MySQL Query Cache 설정 방법은 무엇인가요?
💡 요약 정리
- MySQL Query Cache는 반복되는 select 쿼리를 캐시에서 가져와 속도를 높여주는 기술입니다
- /etc/my.cnf 파일에 Query Cache 설정을 추가하고 MySQL을 재시작하면 활성화됩니다
- show status 명령어로 Query Cache 상태를 모니터링할 수 있습니다
MySQL Query Cache 설정하기
설치환경
- CentOS 5.x (64bit)
- mysql 5.1.59 설치위치: /home/APM/mysql
1. 쿼리 캐시란?
테이블의 내용이 자주 변하지 않고 자주 반복되는 select 쿼리를 수행할때, 쿼리를 mysql에서 가져오지 않고 캐시에서 가져와 속도를 크게 높여주는 기술이다.mysql 4.0.1 이상에서 지원된다.
2. 쿼리 캐시 활성화
/etc/my.cnf 파일에 다음과 같이 추가합니다.
[root@cafe24 ~]# vi /etc/my.cnf
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
# The MySQL server
[mysqld]
port = 3306
socket = /tmp/mysql.sock
skip-locking
key_buffer_size = 384M
max_allowed_packet = 1M
table_open_cache = 512
sort_buffer_size = 2M
read_buffer_size = 2M
read_rnd_buffer_size = 8M
myisam_sort_buffer_size = 64M
thread_cache_size = 8
query_cache_size = 32M
# Try number of CPU's*2 for thread_concurrency
thread_concurrency = 8
max_connections = 2048
# Query Cache
query_cache_limit=16M
query_cache_size=32M
query_cache_type=1
#default-character-set=euckr
#character-set-client-handshake = FALSE
# Don't listen on a TCP/IP port at all. This can be a security enhancement,
# if all processes that need to connect to mysqld run on the same host.
# All interaction with mysqld must be made via Unix sockets or named pipes.
# Note that using this option without enabling named pipes on Windows
# (via the "enable-named-pipe" option) will render mysqld useless!
#
#skip-networking
# slow qeury log
log-slow-queries = /home/APM/mysql/var/mysql-slow.log
long_query_time = 3
# Replication Master Server (default)
# binary logging is required for replication
log-bin=mysql-bin
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
설정 옵션 설명
- query_cache_limit : 쿼리 캐싱 제한 메모리
- query_cache_size : 쿼리 캐싱 메모리 사이즈
- query_cache_type : 쿼리 캐싱 타입.
- 0 : 사용하지 않음
- 1 : 모든 쿼리에 대하여 캐싱을 하고 select 문에서 select sql_no_cache 설정이 있을경우에만 캐싱을 하지 않는다
- 2 : 선택한 쿼리만 query cache를 사용한다. (SQL_CACHE 힌트를 사용하는 쿼리는 query cache를 사용한다.)
설정이 완료되었으면 mysql을 재시작하여 적용합니다.
[root@cafe24 ~]# /etc/rc.d/init.d/mysql restart
Shutting down MySQL. [ OK ]
Starting MySQL. [ OK ]
[root@cafe24 ~]#
3. 쿼리 캐시 확인
show status 명령어를 이용하여 모니터링 할 수 있다.
[root@cafe24 ~]# mysql
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 1
Server version: 5.1.59-log Source distribution
Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.
mysql> show status;
+-----------------------------------+----------+
| Variable_name | Value |
+-----------------------------------+----------+
| Aborted_clients | 0 |
| Aborted_connects | 0 |
| Binlog_cache_disk_use | 0 |
| Binlog_cache_use | 0 |
| Bytes_received | 95 |
| Bytes_sent | 161 |
| Com_admin_commands | 0 |
.
.
.
.
.
.
| Qcache_free_blocks | 1 |
| Qcache_free_memory | 33536880 |
| Qcache_hits | 0 |
| Qcache_inserts | 0 |
| Qcache_lowmem_prunes | 0 |
| Qcache_not_cached | 1 |
| Qcache_queries_in_cache | 0 |
| Qcache_total_blocks | 1 |
.
.
.
.
| Threads_cached | 0 |
| Threads_connected | 1 |
| Threads_created | 1 |
| Threads_running | 1 |
| Uptime | 5 |
| Uptime_since_flush_status | 5 |
+-----------------------------------+----------+
291 rows in set (0.00 sec)
mysql>
[root@cafe24 ~]#
Query Cache 상태 변수 설명
- QCache_queries_in_Cache : 캐시에 등록된 쿼리 갯수.
- QCache_inserts : 캐시에 추가된 쿼리 갯수.
- QCache_hits : 캐시 Hit(적용된) 갯수.
- QCache_lowmem_prunes : 메모리 부족으로 캐시에서 삭제된 쿼리 갯수.
- QCache_not_Cached : 캐시 불가능한 쿼리 갯수(캐시 불가능하거나 Query_Cache_TYPE에 의해).
- QCache_free_memory : Query Cache의 비할당 메모리양.
- QCache_free_blocks : Query Cache의 비할당 메모리 블럭의 갯수.
- QCache_total_blocks : Query Cache의 총 메모리 블럭 갯수.
총 쿼리수 = Qcache_inserts + Qcache_hits + Qcache_not_cached
Qcache_lowmem_prunes 변수값이 크다면 query cache 메모리가 부족하다는 의미이므로 query_cache_size를 늘여줄 필요가 있습니다.
4. Query Cache 관련 명령어
flush query cache : query cache를 defragment
reset query cache : query cache를 clear
flush tables : query cache를 flush
show status : query cache 성능 모니터링
참고 사이트
문제가 해결되지 않았나요?
궁금하신 사항은 언제든지 1:1 문의게시판으로 문의해 주세요.
문의 시 포함 정보:
- 카페24 아이디
- 서비스 ID: 서버호스팅 서비스 ID
- MySQL 버전: mysql --version 명령 실행 결과
- 설정 내용: /etc/my.cnf의 Query Cache 설정 내용
- 문의 내용: Query Cache 설정 관련 문의 사항