mysql innodb的性能与缓冲池的大小有很大的关系。可以通过以下两个参数进行调整。
innodb_buffer_pool_size 和 innodb_buffer_pool_instances
mysql> show variables like 'innodb_buffer_pool%';
+-------------------------------------+----------------+
| Variable_name | Value |
+-------------------------------------+----------------+
| innodb_buffer_pool_chunk_size | 134217728 |
| innodb_buffer_pool_dump_at_shutdown | ON |
| innodb_buffer_pool_dump_now | OFF |
| innodb_buffer_pool_dump_pct | 25 |
| innodb_buffer_pool_filename | ib_buffer_pool |
| innodb_buffer_pool_instances | 1 |
| innodb_buffer_pool_load_abort | OFF |
| innodb_buffer_pool_load_at_startup | ON |
| innodb_buffer_pool_load_now | OFF |
| innodb_buffer_pool_size | 134217728 |
+-------------------------------------+----------------+
10 rows in set (0.17 sec)
这里可以看出,innodb_buffer_pool_size的默认值为128M,和建议的3/4内存差距很大。而且只有一个实例。
MySQL [(none)]> show variables like 'innodb_buffer_pool%';
+-------------------------------------+----------------+
| Variable_name | Value |
+-------------------------------------+----------------+
| innodb_buffer_pool_dump_at_shutdown | OFF |
| innodb_buffer_pool_dump_now | OFF |
| innodb_buffer_pool_filename | ib_buffer_pool |
| innodb_buffer_pool_instances | 8 |
| innodb_buffer_pool_load_abort | OFF |
| innodb_buffer_pool_load_at_startup | OFF |
| innodb_buffer_pool_load_now | OFF |
| innodb_buffer_pool_size | 3007315968 |
+-------------------------------------+----------------+
8 rows in set (0.00 sec)
这里是8个实例,每个实例128M,总共2.8G。
通过sql进行设置为3G:
mysql> SET GLOBAL innodb_buffer_pool_size = 3221225472;
Query OK, 0 rows affected (0.00 sec)
在my.cnf文件中增加如下配置:
# 优化配置3GB, 8 ,128M
innodb_buffer_pool_size=3221225472
innodb_buffer_pool_instances=8
innodb_buffer_pool_chunk_size=128M
查看mysql版本
mysql> select version();
+------------+
| version() |
+------------+
| 5.7.28-log |
+------------+
更新配置以后的参数:
mysql> show variables like 'innodb_buffer_pool%';
+-------------------------------------+----------------+
| Variable_name | Value |
+-------------------------------------+----------------+
| innodb_buffer_pool_chunk_size | 134217728 |
| innodb_buffer_pool_dump_at_shutdown | ON |
| innodb_buffer_pool_dump_now | OFF |
| innodb_buffer_pool_dump_pct | 25 |
| innodb_buffer_pool_filename | ib_buffer_pool |
| innodb_buffer_pool_instances | 8 |
| innodb_buffer_pool_load_abort | OFF |
| innodb_buffer_pool_load_at_startup | ON |
| innodb_buffer_pool_load_now | OFF |
| innodb_buffer_pool_size | 3221225472 |
+-------------------------------------+----------------+
10 rows in set (0.00 sec)
最大连接数:
mysql> show variables like 'max_connections%';
+-----------------+-------+
| Variable_name | Value |
+-----------------+-------+
| max_connections | 1000 |
+-----------------+-------+
1 row in set (0.01 sec)
配置文件中配置的参数在重启之后,可以继续有效。
如果通过sql修改的参数,在配置文件中的参数没有修改,则重启之后,会以配置文件中配置的参数为准。
查看相关参数:
mysql> show global status like '%innodb_buffer_pool_%';
+---------------------------------------+--------------------------------------------------+
| Variable_name | Value |
+---------------------------------------+--------------------------------------------------+
| Innodb_buffer_pool_dump_status | Dumping of buffer pool not started |
| Innodb_buffer_pool_load_status | Buffer pool(s) load completed at 210821 11:14:41 |
| Innodb_buffer_pool_resize_status | |
| Innodb_buffer_pool_pages_data | 188042 |
| Innodb_buffer_pool_bytes_data | 3080880128 |
| Innodb_buffer_pool_pages_dirty | 80735 |
| Innodb_buffer_pool_bytes_dirty | 1322762240 |
| Innodb_buffer_pool_pages_flushed | 2126372 |
| Innodb_buffer_pool_pages_free | 7881 |
| Innodb_buffer_pool_pages_misc | 661 |
| Innodb_buffer_pool_pages_total | 196584 |
| Innodb_buffer_pool_read_ahead_rnd | 0 |
| Innodb_buffer_pool_read_ahead | 1468 |
| Innodb_buffer_pool_read_ahead_evicted | 0 |
| Innodb_buffer_pool_read_requests | 71446600 |
| Innodb_buffer_pool_reads | 872588 |
| Innodb_buffer_pool_wait_free | 217 |
| Innodb_buffer_pool_write_requests | 19198878 |
+---------------------------------------+--------------------------------------------------+
18 rows in set (0.00 sec)
说明:
Innodb_buffer_pool_pages_dirty
显示在内存中修改但尚未写入数据文件的InnoDB缓冲池数据页的数量(脏页刷新)。
Innodb_buffer_pool_pages_flushed
表示从InnoDB缓冲池中刷新脏页的请求数。
Innodb_buffer_pool_pages_free
显示InnoDB缓冲池中的空闲页面
Innodb_buffer_pool_pages_misc
InnoDB缓冲池中的页面数量很多,因为它们已被分配用于管理开销,例如行锁或自适应哈希索引。此值也可以计算为Innodb_buffer_pool_pages_total - Innodb_buffer_pool_pages_free - Innodb_buffer_pool_pages_data
。
Innodb_buffer_pool_pages_total
InnoDB缓冲池的总大小,以page为单位。
innodb_buffer_pool_reads
表示InnoDB缓冲池无法满足的请求数。需要从磁盘中读取。
innodb_buffer_pool_read_requests
它表示从内存中逻辑读取的请求数。
innodb_buffer_pool_wait_free
通常,对InnoDB缓冲池的写入发生在后台。 当InnoDB需要读取或创建页面并且没有可用的干净页面时,InnoDB首先刷新一些脏页并等待该操作完成。 此计数器计算这些等待的实例。 如果已正确设置innodb_buffer_pool_size,则此值应该很小。如果大于0,则表示InnoDb缓冲池太小。
innodb_buffer_pool_write_request
表示对缓冲池执行的写入次数。
另外几个优化建议:
建议innodb引擎表,采用整型自增数据做索引,当然雪花算法也可以。
应避免使用UUID作为主键,UUID作为主键,影响插入数据的效率。
Hash索引无法支持范围查找。
使用联合索引,避免过多的单值索引。
注意联合索引最左前缀优化原则;
尽量使用单表查询,避免执行表连接。
查询没有添加索引的时候,默认按照主键索引进行排序(升序);
Mysql索引采用B+树作为数据结构;