- 浏览: 360756 次
文章分类
最新评论
-
jfeifjlafekae:
另外,为什么存储日志会有拆分过程,而不是rotation方式? ...
HBASE数据架构 -
jfeifjlafekae:
“当然,数据刷写时,一个Region只要有一个列族的memSt ...
HBASE数据架构 -
hmc52107521:
你好,params.keys,params.values以及# ...
mybatis中使用map类型参数,其中key为列名,value为列值 -
zhangxiong0301:
qindongliang1922 写道AM中其它与内存相关的参 ...
(转)YARN内存配置 -
qindongliang1922:
AM中其它与内存相关的参数,还有JVM相关的参数,这些参数可以 ...
(转)YARN内存配置
Allocating RAM for MySQL - The Short Answer
If using just MyISAM, set key_buffer_size to 20% of _available_ RAM. (Plus innodb_buffer_pool_size=0)
If using just InnoDB, set innodb_buffer_pool_size to 70% of _available_ RAM. (Plus key_buffer_size = 10M, small, but not zero.)
Rule of thumb for tuning mysql:
⚈ Start with released copy of my.cnf / my.ini.
⚈ Change key_buffer_size and innodb_buffer_pool_size according to engine usage and RAM.
⚈ Slow queries can usually be 'fixed' via indexes, schema changes, or SELECT changes, not by tuning.
⚈ Don't get carried away with the Query cache until you understand what it can and cannot do.
⚈ Don't change anything else unless you run into trouble (eg, max connections).
⚈ Be sure the changes are under the [mysqld] section, not some other section.
Now for the gory details. (NDB Cluster is not discussed here.)in
What is the key_buffer?
MyISAM does two different things for caching.
⚈ Index blocks (1KB each, BTree structured, from .MYI file) live in the "key buffer".
⚈ Data block caching (from .MYD file) is left to the OS, so be sure to leave a bunch of free space for this.
Caveat: Some flavors of OS always claim to be using over 90%, even when there is really lots of free space.
SHOW GLOBAL STATUS LIKE 'Key%'; then calculate Key_read_requests / Key_reads If it is high (say, over 10), then the key_buffer is big enough.
What is the buffer_pool?
InnoDB does all its caching in a the "buffer pool", whose size is controlled by innodb_buffer_pool_size. It contains 16KB data and index blocks from the open tables, plus some maintenance overhead.
MySQL 5.5 (and 5.1 with the "Plugin") lets you declare the block size to be 8KB or 4KB. MySQL 5.5 allows multiple buffer pools; this can help because there is one mutex per pool, thereby relieving some of the Mutex bottleneck.
More on InnoDB Tuning
Another algorithm
This will set the main cache settings to the minimum; it could be important to systems with lots of other processes and/or less than 2GB of RAM.
Do SHOW TABLE STATUS for all the tables in all the databases.
Add up Index_length for all the MyISAM tables. Set key_buffer_size no larger than that size.
Add up Data_length + Index_length for all the InnoDB tables. Set innodb_buffer_pool_size to no more than 110% of that total.
If that leads to swapping, cut both settings back. Suggest cutting them down proportionately.
Run this to see the values for you system. (If you have a lot of tables, it can take minute(s).)
SELECT ENGINE,
ROUND(SUM(data_length) /1024/1024, 1) AS "Data MB",
ROUND(SUM(index_length)/1024/1024, 1) AS "Index MB",
ROUND(SUM(data_length + index_length)/1024/1024, 1) AS "Total MB",
COUNT(*) "Num Tables"
FROM INFORMATION_SCHEMA.TABLES
WHERE table_schema not in ("information_schema", "performance_schema")
GROUP BY ENGINE;
Mutex bottleneck
MySQL was designed in the days of single-CPU machines, and designed to be easily ported to many different architectures. Unfortunately, that lead to some sloppiness in how to interlock actions. There are small number (too small) of "mutexes" to gain access to several critical processes. Of note:
⚈ MyISAM's key_buffer
⚈ The Query Cache
⚈ InnoDB's buffer_pool
With multi-core boxes, the mutex problem is causing performance problems. In general, past 4-8 cores, MySQL gets slower, not faster. MySQL 5.5 and Percona's XtraDB are making that somewhat better in InnoDB; the practical limit for cores is more like 32, and performance tends plateaus after that rather than declining. 5.6 claims to scale up to about 48 cores.
HyperThreading and Multiple cores (CPUs)
Short answers:
⚈ Turn off HyperThreading
⚈ Turn off any cores beyond 8
⚈ HyperThreading is mostly a thing of the past, so this section may not apply.
HyperThreading is great for marketing, lousy for performance. It involves having two processing units sharing a single hardware cache. If both units are doing the same thing, the cache will be reasonably useful. If the units are doing different things, they will be clobbering each other's cache entries.
Furthermore MySQL is not great on using multiple cores. So, if you turn off HT, the remaining cores run a little faster.
32-bit OS and MySQL
First, the OS (and the hardware?) may conspire to not let you use all 4GB, if that is what you have. If you have more than 4GB of RAM, the excess beyond 4GB is _totally_ inaccessable and unusable on a 32-bit OS.
Secondly, the OS probably has a limit on how much RAM it will allow any process to use.
Example: FreeBSD's maxdsiz, which defaults to 512MB.
Example:
$ ulimit -a
...
max memory size (kbytes, -m) 524288
So, once you have determined how much RAM is available to mysqld, then apply the 20%/70%, but round down some.
If you get an error like [ERROR] /usr/libexec/mysqld: Out of memory (Needed xxx bytes), it probably means that MySQL exceeded what the OS is willing to give it. Decrease the cache settings.
64-bit OS with 32-bit MySQL
The OS is not limited by 4GB, but MySQL is.
If you have at least 4GB of RAM, then maybe these would be good:
⚈ key_buffer_size = 20% of _all_ of RAM, but not more than 3G
⚈ buffer_pool = 3G
You should probably upgrade MySQL to 64-bit.
64-bit OS and MySQL
MyISAM only: key_buffer_size (before 5.0.52 / 5.1.23) had a hard limit of 4G. See also 5.1 restrictions
Otherwise, use about 20% of RAM. Set (in my.cnf / my.ini) innodb_buffer_pool_size = 0.
InnoDB only: innodb_buffer_pool_size = 70% of RAM. If you have lots of RAM and are using 5.5 (or later), then consider having multiple pools. Recommend 1-16 innodb_buffer_pool_instances, such that each one is no smaller than 1GB. (Sorry, no metric on how much this will help; probably not a lot.)
Meanwhile, set key_buffer_size = 20M (tiny, but non-zero)
If you have a mixture of engines, lower both numbers.
max_connections, thread_stack
Each "thread" takes some amount of RAM. This used to be about 200KB; 100 threads would be 20MB, not a signifcant size. If you have max_connections = 1000, then you are talking about 200MB, maybe more. Having that many connections probably implies other issues that should be addressed.
Thread stack overrun rarely happens. If it does, do something like thread_stack=256K
More on max_connections, wait_timeout, connection pooling, etc
table_cache (table_open_cache)
(The name changed in some version.)
The OS has some limit on the number of open files it will let a process have. Each table needs 1 to 3 open files. Each PARTITION is effectively a table. Most operations on a partitioned table open _all_ partitions.
In *nix, ulimit tells you what the file limit is. The maximum value is in the tens of thousands, but sometimes it is set to only 1024. This limits you to about 300 tables. More discussion on ulimit
(This paragraph is in disputed.) On the other side, the table cache is (was) inefficiently implemented -- lookups were done with a linear scan. Hence, setting table_cache in the thousands could actually slow down mysql. (Benchmarks have shown this.)
You can see how well your system is performing via SHOW GLOBAL STATUS; and computing the opens/second via Opened_files / Uptime If this is more than, say, 5, table_cache should be increased. If it is less than, say, 1, you might get improvement by decreasing table_cache.
Query Cache
Short answer: query_cache_type = OFF and query_cache_size = 0
The QC is effectively a hash mapping SELECT statements to resultsets.
Long answer... There are many aspects of the "Query cache"; many are negative.
⚈ Novice Alert! The QC is totally unrelated to the key_buffer and buffer_pool.
⚈ When it is useful, the QC is blazingly fast. It would not be hard to create a benchmark that runs 1000x faster.
⚈ There is a single mutex controlling the QC.
⚈ The QC, unless it is OFF & 0, is consulted for _every_ SELECT.
⚈ Yes, the mutex is hit even if query_cache_type = DEMAND (2).
⚈ Yes, the mutex is hit even for SQL_NO_CACHE.
⚈ Any change to a query (even adding a space) leads (potentially) to a different entry in the QC.
"Pruning" is costly and frequent:
⚈ When _any_ write happens on a table, _all_ entries in the QC for _that_ table are removed.
⚈ It happens even on a readonly Slave.
⚈ Purges are performed with a linear algorithm, so a large QC (even 200MB) can be noticeably slow.
To see how well your QC is performing, SHOW GLOBAL STATUS LIKE 'Qc%'; then compute the read hit rate: Qcache_hits / Qcache_inserts If it is over, say, 5, the QC might be worth keeping.
If you decide the QC is right for you, then I recommend
⚈ query_cache_size = no more than 50M
⚈ query_cache_type = DEMAND
⚈ SQL_CACHE or SQL_NO_CACHE in all SELECTs, based on which queries are likely to benefit from caching.
QC in depth
thread_cache_size
This is a minor tunable. Zero will slow down thread (connection) creation. A small (say, 10), non-zero number is good. The setting has essentially no impact on RAM usage.
It is the number of extra processes to hang onto. It does not restrict the number of threads; max_connections does.
swappiness
RHEL, in its infinite wisdom, decided to let you control how aggressively the OS will preemptively swap RAM. This is good in general, but lousy for MySQL.
MySQL would love for RAM allocations to be reasonably stable -- the caches are (mostly) pre-allocated; the threads, etc, are (mostly) of limited scope. ANY swapping is likely to severly hurt performance of MySQL.
With a high value for swappiness, you lose some RAM because the OS is trying to keep a lot of space free for future allocations (that MySQL is not likely to need).
With swappiness = 0, the OS will probably crash rather than swap. I would rather have MySQL limping than die.
Somewhere in between (say, 5?) might be a good value for a MySQL-only server.
NUMA
OK, it's time to complicate the architecture of how a CPU talks to RAM. NUMA (Non-Uniform Memory Access) enters the picture. Each CPU (or maybe socket with several cores) has a part of the RAM hanging off each. This leads to memory access being faster for local RAM, but slower (tens of cycles slower) for RAM hanging off other CPUs.
Then the OS enters the picture. In at least one case (RHEL?), two things seem to be done:
⚈ OS allocations are pinned to the 'first' CPU's RAM.]
⚈ Other allocations go by default to the first CPU until it is full.
Now for the problem.
⚈ The OS and MySQL have allocated all the 'first' RAM.
⚈ MySQL has allocated some of the second RAM.
⚈ The OS needs to allocate something.
Ouch -- it is out of room in the one CPU where it is willing to allocate its stuff, so it swaps out some of MySQL. Bad.
Possible solution: Configure the BIOS to "interleave" the RAM allocations. This should prevent the premature swapping, at the cost of off-CPU RAM accesses half the time. Well, you have the costly accesses anyway, since you really want to use all of RAM.
Overall performance loss/gain: A few percent.
huge pages
This is another hardware performance gimmick.
For a CPU to access RAM, especially mapping a 64-bit address to somewhere in, say, 128GB or 'real' RAM, the TLB is used. (TLB = Translation Lookup Buffer.) Think of the TLB as a hardware associative memory lookup table; given a 64-bit virtual address, what is the real address.
Because it is an associative memory of finite size, sometimes there will be "misses" that require reaching into real RAM to resolve the lookup. This is costly, so should be avoided.
Normally, RAM is 'paged' in 4KB pieces; the TLB actually maps the top (64-12) bits into a specific page. Then the bottom 12 bits of the virtual address are carried over intact.
For example, 128GB of RAM broken 4KB pages means 32M page-table entries. This is a lot, and probably far exceeds the capacity of the TLB. So, enter the "Huge page" trick.
With the help of both the hardware and the OS, it is possible to have some of RAM in huge pages, of say 4MB (instead of 4KB). This leads to far fewer TLB entries, but it means the unit of paging is 4MB for such parts of RAM. Hence, huge pages tend to be non-pagable.
Now RAM is broken into pagable and non pagable parts; what parts can reasonably be non pagable? In MySQL, the innodb_buffer_pool is a perfect candidate. So, by correctly configuring these, InnoDB can run a little faster:
⚈ Huge pages enabled
⚈ Tell the OS to allocate the right amount (namely to match the buffer_pool)
⚈ Tell MySQL to use huge pages
innodb memory usage vs swap
That thread has more details on what to look for and what to set.
Overall performance gain: A few percent. Yawn.
ENGINE=MEMORY
This is a little-used alternative to MyISAM and InnoDB. The data is not persistent, so it has limited uses. The size of a MEMORY table is limited to max_heap_table_size, which defaults to 16MB. I mention it in case you have changed the value to something huge; this would stealing from other possible uses of RAM.
Web server
A web server like Apache runs multiple threads. If each threads opens a connection to MySQL, you could run out of connections. Make sure MaxClients (or equivalent) is set to some civilized number (under 50).
Tools
⚈ MySQLTuner
⚈ TUNING-PRIMER
There are several tools that advise on memory. One misleading entry they come up with
Maximum possible memory usage: 31.3G (266% of installed RAM)
Don't let it scare you -- the formulas used are excessively conservative. They assume all of max_connections are in use and active, and doing something memory-intensive.
Total fragmented tables: 23 This implies that OPTIMIZE TABLE _might_ help. I suggest it for tables with either a high percentage of "free space" (see SHOW TABLE STATUS) or where you know you do a lot of DELETEs and/or UPDATEs. Still, don't bother to OPTIMIZE too often. Once a month might suffice.
Postlog
Refreshed -- Oct, 2012
More in-depth: Tocker's tuning for 5.6
Irfan's InnoDB performance optimization basics (redux)
Contact me by posting a question at MySQL Forums :: Performance
-- Rick James
MySQL Documents by Rick James
Tips, Debugging, HowTos, Optimizations, etc...
Rick's RoTs (Rules of Thumb -- lots of tips)
Memory Allocation (caching, etc)
Character Set and Collation problem solver
Converting from MyISAM to InnoDB -- includes differences between them
Big DELETEs - how to optimize
Compound INDEXes plus other insights into the mysteries of INDEXing
Partition Maintenance (DROP+REORG) for time series
Entity-Attribute-Value -- a common, poorly performing, design patter; plus an alternative
Find the nearest 10 pizza parlors (efficient searching on Latitude + Longitude)
Alter of a Huge table
Latest 10 news articles -- how to optimize the schema and code for such
Pagination, not with OFFSET, LIMIT
Data Warehouse techniques (esp., Summary Tables)
Techniques on efficiently finding a random row (On beyond ORDER BY RAND())
GUID/UUID Performance (type 1 only)
IP Range Table Performance
MySQL Limits
Galera Limitations (with Percona XtraDB Cluster / MariaDB)
Best of MySQL Forum
发表评论
-
MYSQL乱码问题解决
2015-08-03 12:37 1376在使用mysql过程中遇到乱码问题,具体流程是:同事通过pu ... -
两阶段提交
2015-07-30 18:19 634在分布式系统中,事务往往包含有多个参与者的活动,单个参与者上 ... -
mysql MVCC
2015-07-30 18:15 865Mysql到底是怎么实现MVCC的?这个问题无数人都在问,但 ... -
mysql乐观锁总结和实践
2015-07-30 14:56 778上一篇文章《MySQL悲观 ... -
MySQL InnoDB三大特性之 - 两次写
2015-07-27 14:37 1238今天我们来介绍InnoDB存储引擎的第二个特性 - 两次写( ... -
MySQL InnoDB三大特性之 - 插入缓冲
2015-07-27 14:36 765InnoDB存储引擎有三大特性非常令人激动,它们分别是插入缓 ... -
innodb事务操作过程
2015-07-25 18:08 1994本文是介绍MySQL数据库InnoDB存储引擎重做日志漫游 ... -
myisam和innodb索引实现的不同
2015-07-25 16:24 1116MyISAM引擎使用B+Tree作为索引结构,叶节点的dat ... -
MySQL的InnoDB索引原理详解
2015-07-24 18:06 719本篇介绍下Mysql的InnoDB ... -
mysql表改名
2015-03-30 15:14 12221.方法一 mysql5.1有改名数据库 ... -
MYSQL语句select中使用变量(转载)
2014-11-19 20:24 3353引用set语句的学习: 使用select定义用户变量的实践 ... -
存储过程动态获取表名,然后以表名做参数
2014-06-19 09:27 1330CREATE DEFINER=`coolcloudstat`@ ... -
mysql主从复制--从库跳过错误日志点
2014-03-24 09:47 5006(本文转载,特此声明) 在MYSQL进行Replicatio ... -
mysql数据整理命令
2014-01-28 15:06 1044Analyze Table(修复索引) MyS ... -
MYSQL innodb 每张表一个数据文件
2014-01-08 11:04 5434默认情况下,innodb引擎的所有表都存储在一个叫ibdata ... -
mysql查询优化
2013-12-18 09:32 928最近在做数据批量查询和更新,即从表1中group by 某个字 ... -
MYSQL优化之关闭文件系统日志
2013-12-12 21:47 2715通常数据库系统在数据大量操作时,会产生很大的磁盘io,关闭li ... -
mysql创建用户命令
2013-12-06 19:29 1097mysql创建用户:需要有root权限的账号 示例:creat ...
相关推荐
MySQL配置参数参考
虽然排版不好看,但是 都是硬货 ,记录了绝大部分的mysql 优化参数 以及作用案例,建议资深程序员必看
MySQL配置参数优化是数据库性能提升的关键环节,涉及到多个方面,包括连接管理、缓冲区管理和查询缓存等。以下是对这些关键参数的详细说明: 1. **最大连接数**(`max_connections`):这个参数决定了MySQL可以同时...
以下的文章主要描述的是对MySQL配置参数 my.ini/my.cnf的详细解析,我们主要是以实例演示的方式来对MySQL配置参数 my.ini/my.cnf的实际操作步骤进行说明,以下就是相关内容的具体描述。 1.获取当前配置参数 要优化...
#### 三、MySQL配置参数详解 1. **datadir**:数据目录是MySQL保存所有数据文件的地方,包括表空间文件、日志文件等。选择合适的位置非常重要,因为这直接影响到数据的读写性能。建议选择磁盘I/O性能较好的分区作为...
通过对my.cnf/my.ini配置文件的各项关键参数进行中文注释与解析,帮助DBA及运维人员更好地理解每个配置项的作用及其对MySQL性能的影响。 #### 关键配置项详解 ##### 1. **[client]** - **port=3306**:设定MySQL...
mysql 常用配置参数和状态变量 mysql 作为一个流行的开源关系数据库管理系统,提供了许多配置参数和状态变量,以便用户根据实际情况进行调整和优化。下面我们将详细介绍一些常用的 mysql 配置参数和状态变量。 一...
### MySQL JDBC配置参数详解 在Java应用程序中使用MySQL数据库时,通常会借助JDBC(Java Database Connectivity)技术来实现数据库连接与数据交互。为了优化性能、确保连接的有效性以及合理管理资源,正确设置JDBC...
#### 二、MySQL配置参数 在MySQL中,有许多参数可以调整以优化其性能和安全性。这些参数主要分为两大类:服务器级别的参数和会话级别的参数。本文将重点关注与操作系统配置相关的几个核心参数: - **innodb_buffer_...
通过web界面修改MySQL配置参数,简化操作 修改配置信息不需要重启直接生效,可以反复修改,所见即所得,提高效率 修改数据库配置需要有一定的先决条件,现在只要打开一个浏览器就可以 根据机器、操作系统信息推荐...
3. **性能优化**:根据实际需求调整MySQL配置参数,优化数据库性能。 #### 六、参考资源 - **官方文档**:MySQL官方网站提供了详尽的安装和配置指南,包括针对不同操作系统的特定指导。 - **社区支持**:MySQL社区...
- **优化**:通过对MySQL配置参数的调整,以提升数据库服务器的性能。 #### 描述解析:2G内存的MySQL数据库服务器优化 描述部分重申了标题的内容,强调了针对2GB内存的MySQL服务器进行优化的主题。 #### 配置参数...
本文将详细讲解Linux下MySQL的安装步骤以及关键配置参数的解析。 首先,我们来看一下MySQL的安装过程: 1. 下载MySQL源码包:在 `/usr/local/src/` 目录下,使用 `wget` 命令从指定URL下载MySQL的源码压缩包,例如...
#### 三、MySQL配置参数解释 - **数据库目录**:指定MySQL数据库文件的存储位置。 - **端口号**:MySQL默认监听的端口为3306,可以根据需要修改。 - **字符集**:MySQL支持多种字符集,如UTF-8等,需要在安装或配置...
本文详细介绍了MySQL的安装过程及一些基本配置方法,同时也列举了一些常用的MySQL配置参数及其作用。了解并掌握这些基础知识对于有效管理和优化MySQL数据库至关重要。在实际操作过程中,根据自己的需求灵活调整配置...
MySQL 5.1 的配置参数可以从多个文件中读取默认选项,包括 `/etc/my.cnf`、 `/etc/mysql/my.cnf`、 `MYSQLHOME/etc/my.cnf` 和 `~/.my.cnf`。这些文件中的配置参数将按照指定的顺序读取。 二、启动选项 MySQL 5.1 ...
在实际操作中,`mysql-compatibility-config` 可能会检查以下几个关键的MySQL配置参数: 1. **innodb_buffer_pool_size**:InnoDB存储引擎的缓冲池大小,对处理大量数据的性能至关重要。新版本的MySQL可能会建议更...
MySQL配置文件解析主要涉及到MySQL服务器的参数调整,这些参数直接影响数据库的性能和稳定性。配置文件通常命名为`my.cnf`或`my.ini`,在不同的操作系统路径可能不同。以下是几个关键参数的解释: 1. `port`:指定...
以下是一些关键的MySQL配置参数,这些参数的调整可以显著提高MySQL的性能: 1. **max_connections**:最大连接数。根据服务器资源和应用程序需求调整此值,过高可能会导致服务器资源紧张。 2. **back_log**:最大...