MariaDB CPU 时间高达 500%

MariaDB CPU 时间高达 500%

全球地位

MariaDB [(none)]> SHOW GLOBAL STATUS;
+--------------------------------------------------------------+--------------------------------------------------+
| Variable_name                                                | Value                                            |
+--------------------------------------------------------------+--------------------------------------------------+
| Aborted_clients                                              | 10                                               |
| Aborted_connects                                             | 17                                               |
| Access_denied_errors                                         | 2808                                             |
| Acl_column_grants                                            | 0                                                |
| Acl_database_grants                                          | 255                                              |
| Acl_function_grants                                          | 0                                                |
| Acl_procedure_grants                                         | 0                                                |
| Acl_proxy_users                                              | 1                                                |
| Acl_role_grants                                              | 0                                                |
| Acl_roles                                                    | 0                                                |
| Acl_table_grants                                             | 2                                                |
| Acl_users                                                    | 253                                              |
| Aria_pagecache_blocks_not_flushed                            | 0                                                |
| Aria_pagecache_blocks_unused                                 | 15706                                            |
| Aria_pagecache_blocks_used                                   | 40                                               |
| Aria_pagecache_read_requests                                 | 287044                                           |
| Aria_pagecache_reads                                         | 20253                                            |
| Aria_pagecache_write_requests                                | 36614                                            |
| Aria_pagecache_writes                                        | 36593                                            |
| Aria_transaction_log_syncs                                   | 1                                                |
| Binlog_commits                                               | 0                                                |
| Binlog_group_commits                                         | 0                                                |
| Binlog_group_commit_trigger_count                            | 0                                                |
| Binlog_group_commit_trigger_lock_wait                        | 0                                                |
| Binlog_group_commit_trigger_timeout                          | 0                                                |
| Binlog_snapshot_file                                         |                                                  |
| Binlog_snapshot_position                                     | 0                                                |
| Binlog_bytes_written                                         | 0                                                |
| Binlog_cache_disk_use                                        | 0                                                |
| Binlog_cache_use                                             | 0                                                |
| Binlog_stmt_cache_disk_use                                   | 0                                                |
| Binlog_stmt_cache_use                                        | 0                                                |
| Busy_time                                                    | 0.000000                                         |
| Bytes_received                                               | 243371501                                        |
| Bytes_sent                                                   | 2355355672                                       |
| Com_admin_commands                                           | 2293                                             |
| Com_alter_db                                                 | 0                                                |
| Com_alter_db_upgrade                                         | 0                                                |
| Com_alter_event                                              | 0                                                |
| Com_alter_function                                           | 0                                                |
| Com_alter_procedure                                          | 0                                                |
| Com_alter_server                                             | 0                                                |
| Com_alter_table                                              | 0                                                |
| Com_alter_tablespace                                         | 0                                                |
| Com_alter_user                                               | 0                                                |
| Com_analyze                                                  | 0                                                |
| Com_assign_to_keycache                                       | 0                                                |
| Com_begin                                                    | 0                                                |
| Com_binlog                                                   | 0                                                |
| Com_call_procedure                                           | 0                                                |
| Com_change_db                                                | 92                                               |
| Com_change_master                                            | 0                                                |
| Com_check                                                    | 0                                                |
| Com_checksum                                                 | 0                                                |
| Com_commit                                                   | 0                                                |
| Com_compound_sql                                             | 0                                                |
| Com_create_db                                                | 0                                                |
| Com_create_event                                             | 0                                                |
| Com_create_function                                          | 0                                                |
| Com_create_index                                             | 0                                                |
| Com_create_procedure                                         | 0                                                |
| Com_create_role                                              | 0                                                |
| Com_create_server                                            | 0                                                |
| Com_create_table                                             | 0                                                |
| Com_create_temporary_table                                   | 0                                                |
| Com_create_trigger                                           | 0                                                |
| Com_create_udf                                               | 0                                                |
| Com_create_user                                              | 0                                                |
| Com_create_view                                              | 0                                                |
| Com_dealloc_sql                                              | 0                                                |
| Com_delete                                                   | 8354                                             |
| Com_delete_multi                                             | 0                                                |
| Com_do                                                       | 0                                                |
| Com_drop_db                                                  | 0                                                |
| Com_drop_event                                               | 0                                                |
| Com_drop_function                                            | 0                                                |
| Com_drop_index                                               | 0                                                |
| Com_drop_procedure                                           | 0                                                |
| Com_drop_role                                                | 0                                                |
| Com_drop_server                                              | 0                                                |
| Com_drop_table                                               | 0                                                |
| Com_drop_temporary_table                                     | 0                                                |
| Com_drop_trigger                                             | 0                                                |
| Com_drop_user                                                | 0                                                |
| Com_drop_view                                                | 0                                                |
| Com_empty_query                                              | 0                                                |
| Com_execute_immediate                                        | 0                                                |
| Com_execute_sql                                              | 0                                                |
| Com_flush                                                    | 0                                                |
| Com_get_diagnostics                                          | 0                                                |
| Com_grant                                                    | 0                                                |

488 rows in set (0.00 sec)

MariaDB [(none)]>

我的cnf

[root@host ~]# cat /etc/my.cnf
#
# This group is read both both by the client and the server
# use it for options that affect everything
#
[client-server]

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d

[mysqld]
log-error=/var/lib/mysql/s102.halabtech.net.err
performance-schema=0
innodb_file_per_table=1
default-storage-engine=MyISAM
max_allowed_packet=268435456
open_files_limit=40000
skip-name-resolve

sql_mode=''

local-infile=0
connect_timeout=25
wait_timeout=30
interactive_timeout=30
slow-query-log=1
long_query_time=5
slow_query_log_file="/var/log/mysql-slow.log"

key_buffer_size = 128M
thread_stack = 128K
thread_cache_size = 8
max_heap_table_size = 256M
query_cache_limit = 4M
query_cache_size = 512M
innodb_buffer_pool_size = 2G

服务器规格:

Intel(R) Core(TM) i7-8700 CPU @ 3.20GHz

Memory: 4107488k/68927488k available (7792k kernel code, 1900528k absent, 1353716k reserved, 5950k data, 1984k init)

Filesystem      Size  Used Avail Use% Mounted on
devtmpfs         32G     0   32G   0% /dev
tmpfs            32G     0   32G   0% /dev/shm
tmpfs            32G   28M   32G   1% /run
tmpfs            32G     0   32G   0% /sys/fs/cgroup
/dev/md2        437G  273G  142G  66% /
/dev/md1        488M  401M   62M  87% /boot
tmpfs           6.3G     0  6.3G   0% /run/user/0
tmpfs           6.3G     0  6.3G   0% /run/user/987
tmpfs           6.3G     0  6.3G   0% /run/user/1034

更新

MYSQL 慢查询转储:(数据库真实姓名已更改)

Reading mysql slow query log from /var/log/mysql-slow.log
Count: 2  Time=20.58s (41s)  Lock=0.00s (0s)  Rows_sent=4089261.5 (8178523), Rows_examined=4089261.5 (8178523), Rows_affected=0.0 (0), root[root]@localhost
  SELECT /*!40001 SQL_NO_CACHE */ * FROM `hb_udownloads`

Count: 3  Time=15.38s (46s)  Lock=0.00s (0s)  Rows_sent=81.0 (243), Rows_examined=10026558.0 (30079674), Rows_affected=0.0 (0), server***_fusion[server***_fusion]@localhost
  SELECT *,(select count(id) from tbl_swkey
  where group_id=tbl_keygroup.id and used=0) as selled,(select count(id) from tbl_swkey
  where group_id=tbl_keygroup.id and used=1) as used FROM tbl_keygroup
  order by `displayorder` asc

Count: 1  Time=8.68s (8s)  Lock=0.00s (0s)  Rows_sent=0.0 (0), Rows_examined=4090482.0 (4090482), Rows_affected=0.0 (0), support***_res[support***_res]@localhost
  UPDATE hb_udownloads SET `upackage_id` = 0 WHERE  upackage_id = 403775

Count: 1  Time=5.64s (5s)  Lock=0.00s (0s)  Rows_sent=0.0 (0), Rows_examined=4088258.0 (4088258), Rows_affected=0.0 (0), support***_res[support***_res]@localhost
  SELECT  udownload_id AS retval FROM hb_udownloads WHERE  user_id = 415064  AND file_id = 78499  LIMIT 1

Count: 1  Time=5.58s (5s)  Lock=0.00s (0s)  Rows_sent=0.0 (0), Rows_examined=4088256.0 (4088256), Rows_affected=0.0 (0), support***_res[support***_res]@localhost
  SELECT  udownload_id AS retval FROM hb_udownloads WHERE  user_id = 208286  AND file_id = 202629  LIMIT 1

Count: 1  Time=5.35s (5s)  Lock=0.00s (0s)  Rows_sent=0.0 (0), Rows_examined=4088255.0 (4088255), Rows_affected=0.0 (0), support***_res[support***_res]@localhost
  SELECT  udownload_id AS retval FROM hb_udownloads WHERE  user_id = 235082  AND file_id = 473624  LIMIT 1

Count: 1  Time=5.21s (5s)  Lock=0.00s (0s)  Rows_sent=0.0 (0), Rows_examined=4088254.0 (4088254), Rows_affected=0.0 (0), support***_res[supporth***_res]@localhost
  SELECT  udownload_id AS retval FROM hb_udownloads WHERE  user_id = 61350  AND file_id = 493488  LIMIT 1

Count: 1  Time=5.17s (5s)  Lock=0.00s (0s)  Rows_sent=0.0 (0), Rows_examined=4088259.0 (4088259), Rows_affected=0.0 (0), support***_res[support***_res]@localhost
  SELECT  udownload_id AS retval FROM hb_udownloads WHERE  user_id = 338554  AND file_id = 439150  LIMIT 1

请告知为什么我有时会出现高达 500% 的峰值,以及由于我收到大量 50x 错误,应该采取什么措施来解决这个问题。也请原谅我的英语不好。提前谢谢您。

答案1

高 CPU 使用率通常意味着大量的内存表扫描。

查看您的一个慢速查询:

Time=5.64s (5s)  Rows_sent=0.0 (0)  Rows_examined=4088258.0 (4088258)
SELECT  udownload_id AS retval 
FROM hb_udownloads 
WHERE  user_id = 415064  
AND file_id = 78499  
LIMIT 1

该查询正在读取整个 4M 行表,但最多返回(在本例中,没有)。
你需要索引在该表上支持该查询。复合的user_idfile_id将是一个好的开始。

另一个性能杀手是:

Time=20.58s (41s)  Rows_sent=4089261.5 (8178523)  Rows_examined=4089261.5 (8178523)
SELECT /*!40001 SQL_NO_CACHE */ * 
FROM `hb_udownloads`

切勿使用“选择 *“在生产代码中。
总是明确指定您想要的列。我猜这个表最近一直在“增长”(增加列)。
好吧,如果没有 where 子句,这无论如何都会进行表扫描,但这引出了一个问题 - 可怜的客户端[应用程序]将做什么这个查询“抛出”了 4M 行数据?

答案2

其中一些查询看起来可能需要优化。

例如:

Rows_examined=10026558.0 (30079674), Rows_affected=0.0 (0), server***_fusion[server***_fusion]@localhost
  SELECT *,(select count(id) from tbl_swkey
  where group_id=tbl_keygroup.id and used=0) as selled,(select count(id) from tbl_swkey
  where group_id=tbl_keygroup.id and used=1) as used FROM tbl_keygroup
  order by `displayorder` asc

此查询正在扫描 30000000 行。

您可以将其更改为两个查询:

SELECT * FROM tbl_keygroup ORDER by `displayorder` asc;
SELECT count(id) FROM tbl_swkey WHERE used IN (0,1) GROUP BY used;

因为查询本身涉及无用的连接和两次可以减半的全表扫描。

您所展示的大部分内容都指向查询优化问题,我会查看那个慢速查询日志,以确定以最少的表扫描来检索该数据的最佳方式。

出于同一目的,您可能还想对大量数据进行索引。

答案3

** 这应该会使第一个更快:

SELECT  k.*, s.sold, s.used
    FROM ( SELECT group_id,
                  SUM(used = 0) AS sold,
                  SUM(used = 1) AS used
               FROM tbl_swkey
         ) AS s
    JOIN tbl_keygroup AS k  ON s.group_id = k.id
    ORDER BY displayorder

并且有

tbl_swkey:    INDEX(group_id, used)
tbl_keygroup: INDEX(displayorder)

如果您需要进一步讨论此查询,请提供SHOW CREATE TABLE两个表格、表格大小和EXPLAIN SELECT ...

** 这

SELECT  udownload_id AS retval
    FROM  hb_udownloads
    WHERE  user_id = 415064
      AND  file_id = 78499
    LIMIT  1 

需要一个 3 列复合索引才能更快:

INDEX(user_id, file_id, udownload_id)

您不关心获得哪一行匹配?还是应该添加一个ORDER BY?如果添加一个ORDER BY,我的索引建议可能需要更改。

** 这是从哪里来的mysqldump

SELECT /*!40001 SQL_NO_CACHE */ * FROM `hb_udownloads`

如果您对转储的侵入性感到不满,dba.stackexchange.com 上有几个关于该主题的讨论。

相关内容