首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何确定哪些MySQL查询花费大量时间?

如何确定哪些MySQL查询花费大量时间?
EN

Server Fault用户
提问于 2013-11-18 11:42:01
回答 1查看 1.7K关注 0票数 3

我的MySQL服务器占用了大量内存。

内存占用1.按降序排列的内存消耗(前4位)

特别是MySQL的内存消耗-

为什么有这么多的mysqld进程?它们是进程/线程吗?他们消耗了很多记忆。我把那个号码设在什么地方了吗?

此外,我的查询需要很多时间来执行,因此我的网站变得越来越慢。

我的my.cnf片段-

代码语言:javascript
复制
[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结果的相关部分

代码语言:javascript
复制
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

我的服务器规格-

代码语言:javascript
复制
Disk Size   80GB ( Used = 50GB )
RAM         4GB
Swap    4GB
Number of CPUs  8
OS type 32 bit
OS  Ubuntu 10.04
EN

回答 1

Server Fault用户

回答已采纳

发布于 2013-11-18 17:21:01

为什么有这么多线程

我以前见过很多次了。这不是问题。它只是表示mysqld二进制文件是如何安装的。我之前已经讨论过这个问题:见我2012年3月29日的帖子:相同mysql进程

以前的mysqld源代码编译版本将产生多个线程。我不知道什么环境能阻止这一切。我通常喜欢安装RPM版本,而不是yum安装或执行源代码编译。

或者,您只需升级到最新版本即可。您还可以查找RPM版本的MySQL 5.1.63并安装它。否则,你什么都不用担心。

因为DB连接是线程,所以您可以根据需要将最大值设置为高或低,从而更改连接的数量。例如,您可以使用以下方法将其设置为“动态”:

代码语言:javascript
复制
SET GLOBAL max_connections = 500;

当然,您必须将该设置添加到/etc/my.cnf中,这样每次重新启动mysqld时,max_connections都会保持500。

你原来的问题

要找到哪些查询需要很长时间,您可以使用查询摘要动态地完成这些查询。下面是我关于如何设置DBA StackExchange的文章:

试试看!

票数 1
EN
页面原文内容由Server Fault提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://serverfault.com/questions/555693

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档