Linux
如何辨識哪些 MySQL 查詢需要花費大量時間?
我的 MySQL 伺服器正在消耗大量記憶體。
記憶體佔用1.記憶體消耗降序排列(前4位)
特別是 MySQL 的記憶體消耗 -
為什麼會有這麼多
mysqld
程序?他們是程序/執行緒嗎?它們正在消耗大量記憶體。我在某個地方設置了那個數字嗎?此外,我的查詢需要很長時間才能執行,因此我的網站變慢了。
我的 my.cnf 的一個片段 -
[mysqld] key_buffer = 16M max_allowed_packet = 16M thread_stack = 192K thread_cache_size = 8 myisam-recover = BACKUP query_cache_limit = 1M query_cache_size = 16M innodb_lock_wait_timeout = 120 innodb_buffer_pool_size = 500M [mysqldump] max_allowed_packet = 16M [mysql] #no-auto-rehash # faster start of mysql but no tab completition [isamchk] key_buffer = 16M wait_timeout = 60
我跑去
tuning-primer.sh
檢查我做錯了什麼。結果的相關部分tuning-primer.sh
-MySQL Version 5.1.63-0ubuntu0.10.04.1 i486 Uptime = 176 days 0 hrs 24 min 31 sec Avg. qps = 8 Total Questions = 128972512 Threads Connected = 6 SLOW QUERIES The slow query log is NOT enabled. Current long_query_time = 10.000000 sec. You have 55628 out of 128972533 that take longer than 10.000000 sec. to complete Your long_query_time seems to be fine BINARY UPDATE LOG The binary update log is NOT enabled. You will not be able to do point in time recovery See http://dev.mysql.com/doc/refman/5.1/en/point-in-time-recovery.html WORKER THREADS Current thread_cache_size = 8 Current threads_cached = 7 Current threads_per_sec = 0 Historic threads_per_sec = 0 Your thread_cache_size is fine MAX CONNECTIONS Current max_connections = 151 Current threads_connected = 5 Historic max_used_connections = 147 The number of used connections is 97% of the configured maximum. You should raise max_connections INNODB STATUS Current InnoDB index space = 109 M Current InnoDB data space = 10.32 G Current InnoDB buffer pool free = 0 % Current innodb_buffer_pool_size = 500 M Depending on how much space your innodb indexes take up it may be safe to increase this value to up to 2 / 3 of total system memory MEMORY USAGE Max Memory Ever Allocated : 929 M Configured Max Per-thread Buffers : 405 M Configured Max Global Buffers : 534 M Configured Max Memory Limit : 939 M Physical Memory : 3.94 G Max memory limit seem to be within acceptable norms KEY BUFFER Current MyISAM index space = 1 M Current key_buffer_size = 16 M Key cache miss rate is 1 : 204 Key buffer free ratio = 88 % Your key_buffer_size seems to be fine QUERY CACHE Query cache is enabled Current query_cache_size = 16 M Current query_cache_used = 8 M Current query_cache_limit = 1 M Current Query cache Memory fill ratio = 50.29 % Current query_cache_min_res_unit = 4 K MySQL won't cache query results that are larger than query_cache_limit in size SORT OPERATIONS Current sort_buffer_size = 2 M Current read_rnd_buffer_size = 256 K Sort buffer seems to be fine JOINS Current join_buffer_size = 132.00 K You have had 10728 queries where a join could not use an index properly You should enable "log-queries-not-using-indexes" Then look for non indexed joins in the slow query log. If you are unable to optimize your queries you may want to increase your join_buffer_size to accommodate larger joins in one pass. Note! This script will still suggest raising the join_buffer_size when ANY joins not using indexes are found. OPEN FILES LIMIT Current open_files_limit = 1024 files The open_files_limit should typically be set to at least 2x-3x that of table_cache if you have heavy MyISAM usage. Your open_files_limit value seems to be fine TABLE CACHE Current table_open_cache = 64 tables Current table_definition_cache = 256 tables You have a total of 130 tables You have 64 open tables. Current table_cache hit rate is 0% , while 100% of your table cache is in use You should probably increase your table_cache TEMP TABLES Current max_heap_table_size = 16 M Current tmp_table_size = 16 M Of 6404640 temp tables, 48% were created on disk Perhaps you should increase your tmp_table_size and/or max_heap_table_size to reduce the number of disk-based temporary tables Note! BLOB and TEXT columns are not allow in memory tables. If you are using these columns raising these values might not impact your ratio of on disk temp tables. TABLE SCANS Current read_buffer_size = 128 K Current table scan ratio = 206 : 1 read_buffer_size seems to be fine TABLE LOCKING Current Lock Wait ratio = 1 : 84225 Your table locking seems to be fine
我的伺服器規格 -
Disk Size 80GB ( Used = 50GB ) RAM 4GB Swap 4GB Number of CPUs 8 OS type 32 bit OS Ubuntu 10.04
為什麼有這麼多執行緒
我以前見過很多次。這不是問題。它只是表示如何安裝 mysqld 二進製文件。我之前討論過這個:見我的 2012 年 3 月 29 日的文章:相同的 mysql 程序
舊的原始碼編譯版本的 mysqld 將產生多個執行緒。我不知道什麼設置可以避免這種情況。我通常喜歡安裝 RPM 版本而不是 yum 安裝或進行原始碼編譯。
或者,您可以升級到最新版本。您還可以查找 MySQL 5.1.63 的 RPM 版本並安裝它。否則,您無需擔心任何事情。
由於數據庫連接是執行緒,因此您可以根據需要將max_connections設置為高或低來更改連接數。例如,您可以使用以下方法即時設置它:
SET GLOBAL max_connections = 500;
當然,您必須將該設置添加到每次重新啟動 mysqld 時保持 500
/etc/my.cnf
。max_connections
您的原始問題
要查找哪些查詢需要很長時間,您可以使用pt-query-digest動態執行此操作。以下是我關於如何設置的 DBA StackExchange 文章:
Jun 26, 2012
:現在正在執行什麼?Apr 24, 2012
:調查 MySQL 吞吐量的峰值Nov 24, 2011
: MySQL 一般查詢日誌性能影響試試看 !!!