【问题标题】:MySQL tmp_table_size for a small database小型数据库的 MySQL tmp_table_size
【发布时间】:2018-08-22 16:45:29
【问题描述】:

我在 Ubuntu 18.04 上有 MySQL 5.7.23。这是一个具有 2 个 vCore 和 8 GiB RAM 的 VPS。该服务器托管两个 WordPress 网站。每个网站都有一个大小为 7.5 MB 的数据库。所以总共有 15 MB 的数据库。

我尝试使用mysqltuner 优化数据库。它每次都向我推荐同样的事情:

Variables to adjust:
    tmp_table_size (> 64M)
    max_heap_table_size (> 64M)

我从 16M 开始,然后建议我将值增加到 32M,现在建议我将值增加到 64M 或更多。但为什么?我所有数据库的总大小约为 15M。

我有足够的内存,可用内存约为 6.5 GiB,因此我可以为 MySQL 使用更多内存。

来自mysqltuner的完整日志是

root@web:~# perl mysqltuner.pl
 >>  MySQLTuner 1.7.10 - Major Hayden <major@mhtx.net>
 >>  Bug reports, feature requests, and downloads at http://mysqltuner.com/
 >>  Run with '--help' for additional options and output filtering

[--] Skipped version check for MySQLTuner script
[OK] Logged in using credentials from Debian maintenance account.
[OK] Currently running supported MySQL version 5.7.23-0ubuntu0.18.04.1
[OK] Operating on 64-bit architecture

-------- Log file Recommendations ----------------------------------------------                                                                                                                                                             --------------------
[--] Log file: /var/log/mysql/error.log(0B)
[OK] Log file /var/log/mysql/error.log exists
[OK] Log file /var/log/mysql/error.log is readable.
[!!] Log file /var/log/mysql/error.log is empty
[OK] Log file /var/log/mysql/error.log is smaller than 32 Mb
[OK] /var/log/mysql/error.log doesn't contain any warning.
[OK] /var/log/mysql/error.log doesn't contain any error.
[--] 0 start(s) detected in /var/log/mysql/error.log
[--] 0 shutdown(s) detected in /var/log/mysql/error.log

-------- Storage Engine Statistics ---------------------------------------------                                                                                                                                                             --------------------
[--] Status: +ARCHIVE +BLACKHOLE +CSV -FEDERATED +InnoDB +MEMORY +MRG_MYISAM +My                                                                                                                                                             ISAM +PERFORMANCE_SCHEMA
[--] Data in InnoDB tables: 14.2M (Tables: 45)
[OK] Total fragmented tables: 0

-------- Security Recommendations ----------------------------------------------                                                                                                                                                             --------------------
[OK] There are no anonymous accounts for any database users
[OK] All database users have passwords assigned
[--] Bug #80860 MySQL 5.7: Avoid testing password when validate_password is acti                                                                                                                                                             vated

-------- CVE Security Recommendations ------------------------------------------                                                                                                                                                             --------------------
[--] Skipped due to --cvefile option undefined

-------- Performance Metrics ---------------------------------------------------                                                                                                                                                             --------------------
[--] Up for: 1d 2h 43m 42s (84K q [0.879 qps], 1K conn, TX: 441M, RX: 12M)
[--] Reads / Writes: 98% / 2%
[--] Binary logging is disabled
[--] Physical Memory     : 7.8G
[--] Max MySQL memory    : 384.4M
[--] Other process memory: 252.8M
[--] Total buffers: 224.0M global + 1.1M per thread (151 max threads)
[--] P_S Max memory usage: 72B
[--] Galera GCache Max memory usage: 0B
[OK] Maximum reached memory usage: 228.3M (2.86% of installed RAM)
[OK] Maximum possible memory usage: 384.4M (4.82% of installed RAM)
[OK] Overall possible memory usage with other process is compatible with memory                                                                                                                                                              available
[OK] Slow queries: 0% (0/84K)
[OK] Highest usage of available connections: 2% (4/151)
[OK] Aborted connections: 0.00%  (0/1561)
[OK] Query cache is disabled by default due to mutex contention on multiprocesso                                                                                                                                                             r machines.
[OK] Sorts requiring temporary tables: 0% (0 temp sorts / 12K sorts)
[OK] No joins without indexes
[!!] Temporary tables created on disk: 44% (3K on disk / 7K total)
[OK] Thread cache hit rate: 99% (4 created / 1K connections)
[OK] Table cache hit rate: 77% (1K open / 1K opened)
[OK] Open file limit used: 1% (56/5K)
[OK] Table locks acquired immediately: 100% (202 immediate / 202 locks)

-------- Performance schema ----------------------------------------------------                                                                                                                                                             --------------------
[--] Memory used by P_S: 72B
[--] Sys schema is installed.

-------- ThreadPool Metrics ----------------------------------------------------                                                                                                                                                             --------------------
[--] ThreadPool stat is disabled.

-------- MyISAM Metrics --------------------------------------------------------                                                                                                                                                             --------------------
[!!] Key buffer used: 18.3% (3M used / 16M cache)
[OK] Key buffer size / total MyISAM indexes: 16.0M/43.0K
[OK] Read Key buffer hit rate: 96.3% (190 cached / 7 reads)

-------- InnoDB Metrics --------------------------------------------------------                                                                                                                                                             --------------------
[--] InnoDB is enabled.
[--] InnoDB Thread Concurrency: 0
[OK] InnoDB File per table is activated
[OK] InnoDB buffer pool / data size: 128.0M/14.2M
[OK] Ratio InnoDB log file size / InnoDB Buffer pool size: 16.0M * 2/128.0M shou                                                                                                                                                             ld be equal 25%
[OK] InnoDB buffer pool instances: 1
[--] Number of InnoDB Buffer Pool Chunk : 1 for 1 Buffer Pool Instance(s)
[OK] Innodb_buffer_pool_size aligned with Innodb_buffer_pool_chunk_size & Innodb                                                                                                                                                             _buffer_pool_instances
[OK] InnoDB Read buffer efficiency: 99.96% (2124360 hits/ 2125302 total)
[!!] InnoDB Write Log efficiency: 83.91% (9286 hits/ 11067 total)
[OK] InnoDB log waits: 0.00% (0 waits / 1781 writes)

-------- AriaDB Metrics --------------------------------------------------------                                                                                                                                                             --------------------
[--] AriaDB is disabled.

-------- TokuDB Metrics --------------------------------------------------------                                                                                                                                                             --------------------
[--] TokuDB is disabled.

-------- XtraDB Metrics --------------------------------------------------------                                                                                                                                                             --------------------
[--] XtraDB is disabled.

-------- RocksDB Metrics -------------------------------------------------------                                                                                                                                                             --------------------
[--] RocksDB is disabled.

-------- Spider Metrics --------------------------------------------------------                                                                                                                                                             --------------------
[--] Spider is disabled.

-------- Connect Metrics -------------------------------------------------------                                                                                                                                                             --------------------
[--] Connect is disabled.

-------- Galera Metrics --------------------------------------------------------                                                                                                                                                             --------------------
[--] Galera is disabled.

-------- Replication Metrics ---------------------------------------------------                                                                                                                                                             --------------------
[--] Galera Synchronous replication: NO
[--] No replication slave(s) for this server.
[--] Binlog format: ROW
[--] XA support enabled: ON
[--] Semi synchronous replication Master: Not Activated
[--] Semi synchronous replication Slave: Not Activated
[--] This is a standalone server

-------- Recommendations -------------------------------------------------------                                                                                                                                                             --------------------
General recommendations:
    When making adjustments, make tmp_table_size/max_heap_table_size equal
    Reduce your SELECT DISTINCT queries which have no LIMIT clause
Variables to adjust:
    tmp_table_size (> 64M)
    max_heap_table_size (> 64M)

【问题讨论】:

  • 在您将获得 >10GB 磁盘空间的现代系统上,16MB 和 64MB 有什么区别?这些默认设置来自一个 2GB 驱动器被认为不可能巨大而 1GB 内存仍然是假设的时代。除非您有非常具体的理由不这样做,否则我会遵循调整工具的建议。
  • 谢谢。我不知道是否甚至不能减慢数据库的速度。我不知道数据库系统的背景,所以我不知道这会如何影响整个服务器。如果需要将更多数据保存到磁盘而不是 RAM,它甚至可能会降低服务器的速度。
  • 您说的是 48MB 的内存,这在该规格的机器上几乎无关紧要。这不会产生可衡量的影响。如果您说的是从 16MB 增加到 4GB,我会理解您的担忧,这是系统内存的 50%,而 48MB 为 0.6%。

标签: mysql mysqltuner


【解决方案1】:

您应该对 mysqltuner 的建议持保留态度。它的建议不是在对您的数据库或查询有任何了解的情况下提出的,有时它们只是基于不完整信息的猜测。

增加 tmp_table_size 的建议可能基于读取指示在磁盘上创建了一些临时表的状态值。

mysql> show global status like 'created%tables';
+-------------------------+-------+
| Variable_name           | Value |
+-------------------------+-------+
| Created_tmp_disk_tables | 35    |
| Created_tmp_tables      | 274   |
+-------------------------+-------+

“啊哈!” mysqltuner 说,“13% 的临时表在磁盘上。如果我们允许更大的临时表留在 RAM 中,这些可能会留在 RAM 中。tmp_table_size 加倍!”

但有些临时表总是在磁盘上创建。

https://dev.mysql.com/doc/refman/5.7/en/internal-temporary-tables.html 说:

某些查询条件会阻止使用内存中的临时表, 在这种情况下,服务器使用磁盘表代替:

  • 表中存在 BLOB 或 TEXT 列。这包括 具有字符串值的用户定义变量,因为它们已被处理 作为 BLOB 或 TEXT 列,取决于它们的值是否为二进制 或非二进制字符串。

  • 存在最大长度大于 512 的任何字符串列 (二进制字符串的字节,非二进制字符串的字符)在 SELECT 列表,如果使用 UNION 或 UNION ALL。

  • SHOW COLUMNS 和 DESCRIBE 语句使用 BLOB 作为某些类型 列,因此用于结果的临时表是磁盘 表。

mysqltuner 如何区分因太大而进入磁盘的临时表与因上述条件之一进入磁盘的临时表之间的区别?

它无法区分。它必须阅读您的查询日志并分析每个查询,但它不会这样做。它只知道磁盘上有多少临时表,而不知道为什么。

Mysqltuner 只假设临时表存储在磁盘上,因为它大于 tmp_table_size,并且假设增加 tmp_table_size 会有一些好处。

【讨论】:

  • 谢谢比尔的解释。因此,在没有负面影响的情况下将值提高得更高是“安全的”,对吗?因此,即使是 512M 也适用于我的配置(可能主要是 RAM 容量)。
  • 可能是安全的。除非创建临时表的查询正在运行,否则它不会使用该内存,如果临时表较小,它将不会使用整个 512M。
  • 另一方面,如果许多个查询可以同时运行,所有的查询都创建临时表,并且它们都使用高达512M的内存,那么这可以加起来要大得多,而且可能没有警告。
  • 再次感谢比尔。我将遵循该工具的建议。现在看来256M就够了,我就不高了,我会监控的。对于整个系统,我仍然处于相同的内存使用水平,大约 1.2 GB。
猜你喜欢
  • 2012-10-26
  • 2013-03-29
  • 2012-03-31
  • 2011-10-16
  • 2017-12-21
  • 2018-06-06
  • 2020-07-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多