Mysql 5.6(Percona):如何提高UPDATE和INSERT的查询性能?

Mysql 5.6 (Percona): How to Improve Query Performance of UPDATE and INSERT?

我是 运行 lxc 容器上的 Percona 5.6 集群(3 个节点)。各节点硬件如下:

4 x Intel(R) Xeon(R) CPU E5-2630 v3 @ 2.40GHz (2 Sockets)
16GB RAM
130GB HD

我的数据库大小不到 4GB。我已经设法调整配置设置,以便快速处理选择。但是,我看到一些 UPDATE 和 INSERT 的执行速度非常慢(5 秒以上)。所以一定有某种瓶颈,我想知道我是否可以通过配置调整来解决。

我知道有几个 table 可能需要删除一些未使用的索引,但我想知道还有什么可以提高查询的性能。负载并没有那么大,大约有 100 个用户,因为它是一个内部系统。

innodb_buffer_pool_size: 8G
innodb_log_file_size: 256M
innodb_log_buffer_size: 128M
innodb_flush_log_at_trx_commit: 2
innodb_thread_concurrency: 0
innodb_flush_method: O_DIRECT
innodb_file_per_table: ON

这是我的 SHOW ENGINE INNODB 状态:

| InnoDB |      | 
=====================================
2018-08-14 15:22:02 7f3330863700 INNODB MONITOR OUTPUT
=====================================
Per second averages calculated from the last 9 seconds
-----------------
BACKGROUND THREAD
-----------------
srv_master_thread loops: 22626 srv_active, 0 srv_shutdown, 79338 srv_idle
srv_master_thread log flush and writes: 101963
----------
SEMAPHORES
----------
OS WAIT ARRAY INFO: reservation count 86267
OS WAIT ARRAY INFO: signal count 1537634
Mutex spin waits 800672, rounds 1115180, OS waits 19238
RW-shared spins 605666, rounds 2660513, OS waits 58073
RW-excl spins 79517, rounds 2104544, OS waits 8150
Spin rounds per wait: 1.39 mutex, 4.39 RW-shared, 26.47 RW-excl
------------
TRANSACTIONS
------------
Trx id counter 1057692300
Purge done for trx's n:o < 1057692300 undo n:o < 0 state: running but idle
History list length 2030
LIST OF TRANSACTIONS FOR EACH SESSION:
---TRANSACTION 0, not started
MySQL thread id 621427, OS thread handle 0x7f3330863700, query id 7014924 localhost myuser init
SHOW ENGINE INNODB STATUS
---TRANSACTION 0, not started
MySQL thread id 608823, OS thread handle 0x7f33311c8700, query id 6968932 10.0.10.200 myuser
---TRANSACTION 1057692298, not started
MySQL thread id 2, OS thread handle 0x7f35905c8700, query id 7014913 committed 7569847
--------
FILE I/O
--------
I/O thread 0 state: waiting for completed aio requests (insert buffer thread)
I/O thread 1 state: waiting for completed aio requests (log thread)
I/O thread 2 state: waiting for completed aio requests (read thread)
I/O thread 3 state: waiting for completed aio requests (read thread)
I/O thread 4 state: waiting for completed aio requests (read thread)
I/O thread 5 state: waiting for completed aio requests (read thread)
I/O thread 6 state: waiting for completed aio requests (write thread)
I/O thread 7 state: waiting for completed aio requests (write thread)
I/O thread 8 state: waiting for completed aio requests (write thread)
I/O thread 9 state: waiting for completed aio requests (write thread)
Pending normal aio reads: 0 [0, 0, 0, 0] , aio writes: 0 [0, 0, 0, 0] ,
 ibuf aio reads: 0, log i/o's: 0, sync i/o's: 0
Pending flushes (fsync) log: 0; buffer pool: 0
189853 OS file reads, 666579 OS file writes, 119023 OS fsyncs
0.00 reads/s, 0 avg bytes/read, 14.78 writes/s, 2.89 fsyncs/s
-------------------------------------
INSERT BUFFER AND ADAPTIVE HASH INDEX
-------------------------------------
Ibuf: size 1, free list len 926, seg size 928, 3164 merges
merged operations:
 insert 3331, delete mark 1275, delete 655
discarded operations:
 insert 0, delete mark 0, delete 0
9.67 hash searches/s, 95.66 non-hash searches/s
---
LOG
---
Log sequence number 75281381079
Log flushed up to   75281381079
Pages flushed up to 75281381079
Last checkpoint at  75281381079
Max checkpoint age    434154333
Checkpoint age target 420587011
Modified age          0
Checkpoint age        0
0 pending log writes, 0 pending chkp writes
142005 log i/o's done, 2.00 log i/o's/second
----------------------
BUFFER POOL AND MEMORY
----------------------
Total memory allocated 8942256128; in additional pool allocated 0
Total memory allocated by read views 776
Internal hash tables (constant factor + variable factor)
    Adaptive hash index 411468128   (151384312 + 260083816)
    Page hash           1107208 (buffer pool 0 only)
    Dictionary cache    39612436    (35402768 + 4209668)
    File system         1602712     (812272 + 790440)
    Lock system         21251648    (21250568 + 1080)
    Recovery system     0   (0 + 0)
Dictionary memory allocated 4209668
Buffer pool size        524280
Buffer pool size, bytes 8589803520
Free buffers            82805
Database pages          425581
Old database pages      157253
Modified db pages       0
Pending reads 0
Pending writes: LRU 0, flush list 0, single page 0
Pages made young 223, not young 0
0.00 youngs/s, 0.00 non-youngs/s
Pages read 189492, created 236089, written 496948
0.00 reads/s, 0.00 creates/s, 12.55 writes/s
Buffer pool hit rate 1000 / 1000, young-making rate 0 / 1000 not 0 / 1000
Pages read ahead 0.00/s, evicted without access 0.00/s, Random read ahead 0.00/s
LRU len: 425581, unzip_LRU len: 38
I/O sum[0]:cur[0], unzip sum[0]:cur[0]
----------------------
INDIVIDUAL BUFFER POOL INFO
----------------------
---BUFFER POOL 0
Buffer pool size        65535
Buffer pool size, bytes 1073725440
Free buffers            9846
Database pages          53705
Old database pages      19844
Modified db pages       0
Pending reads 0
Pending writes: LRU 0, flush list 0, single page 0
Pages made young 27, not young 0
0.00 youngs/s, 0.00 non-youngs/s
Pages read 24150, created 29555, written 83723
0.00 reads/s, 0.00 creates/s, 3.56 writes/s
Buffer pool hit rate 1000 / 1000, young-making rate 0 / 1000 not 0 / 1000
Pages read ahead 0.00/s, evicted without access 0.00/s, Random read ahead 0.00/s
LRU len: 53705, unzip_LRU len: 0
I/O sum[0]:cur[0], unzip sum[0]:cur[0]
---BUFFER POOL 1
Buffer pool size        65535
Buffer pool size, bytes 1073725440
Free buffers            9114
Database pages          54443
Old database pages      20117
Modified db pages       0
Pending reads 0
Pending writes: LRU 0, flush list 0, single page 0
Pages made young 28, not young 0
0.00 youngs/s, 0.00 non-youngs/s
Pages read 24455, created 29988, written 46033
0.00 reads/s, 0.00 creates/s, 0.67 writes/s
Buffer pool hit rate 1000 / 1000, young-making rate 0 / 1000 not 0 / 1000
Pages read ahead 0.00/s, evicted without access 0.00/s, Random read ahead 0.00/s
LRU len: 54443, unzip_LRU len: 0
I/O sum[0]:cur[0], unzip sum[0]:cur[0]
---BUFFER POOL 2
Buffer pool size        65535
Buffer pool size, bytes 1073725440
Free buffers            10545
Database pages          53012
Old database pages      19588
Modified db pages       0
Pending reads 0
Pending writes: LRU 0, flush list 0, single page 0
Pages made young 27, not young 0
0.00 youngs/s, 0.00 non-youngs/s
Pages read 23505, created 29507, written 64763
0.00 reads/s, 0.00 creates/s, 1.89 writes/s
Buffer pool hit rate 1000 / 1000, young-making rate 0 / 1000 not 0 / 1000
Pages read ahead 0.00/s, evicted without access 0.00/s, Random read ahead 0.00/s
LRU len: 53012, unzip_LRU len: 0
I/O sum[0]:cur[0], unzip sum[0]:cur[0]
---BUFFER POOL 3
Buffer pool size        65535
Buffer pool size, bytes 1073725440
Free buffers            9551
Database pages          53984
Old database pages      19947
Modified db pages       0
Pending reads 0
Pending writes: LRU 0, flush list 0, single page 0
Pages made young 16, not young 0
0.00 youngs/s, 0.00 non-youngs/s
Pages read 24437, created 29547, written 61138
0.00 reads/s, 0.00 creates/s, 0.89 writes/s
Buffer pool hit rate 1000 / 1000, young-making rate 0 / 1000 not 0 / 1000
Pages read ahead 0.00/s, evicted without access 0.00/s, Random read ahead 0.00/s
LRU len: 53984, unzip_LRU len: 5
I/O sum[0]:cur[0], unzip sum[0]:cur[0]
---BUFFER POOL 4
Buffer pool size        65535
Buffer pool size, bytes 1073725440
Free buffers            10559
Database pages          52987
Old database pages      19579
Modified db pages       0
Pending reads 0
Pending writes: LRU 0, flush list 0, single page 0
Pages made young 29, not young 0
0.00 youngs/s, 0.00 non-youngs/s
Pages read 23524, created 29463, written 69996
0.00 reads/s, 0.00 creates/s, 2.11 writes/s
Buffer pool hit rate 1000 / 1000, young-making rate 0 / 1000 not 0 / 1000
Pages read ahead 0.00/s, evicted without access 0.00/s, Random read ahead 0.00/s
LRU len: 52987, unzip_LRU len: 0
I/O sum[0]:cur[0], unzip sum[0]:cur[0]
---BUFFER POOL 5
Buffer pool size        65535
Buffer pool size, bytes 1073725440
Free buffers            10906
Database pages          52633
Old database pages      19448
Modified db pages       0
Pending reads 0
Pending writes: LRU 0, flush list 0, single page 0
Pages made young 33, not young 0
0.00 youngs/s, 0.00 non-youngs/s
Pages read 23070, created 29563, written 60299
0.00 reads/s, 0.00 creates/s, 1.56 writes/s
Buffer pool hit rate 1000 / 1000, young-making rate 0 / 1000 not 0 / 1000
Pages read ahead 0.00/s, evicted without access 0.00/s, Random read ahead 0.00/s
LRU len: 52633, unzip_LRU len: 33
I/O sum[0]:cur[0], unzip sum[0]:cur[0]
---BUFFER POOL 6
Buffer pool size        65535
Buffer pool size, bytes 1073725440
Free buffers            10544
Database pages          53001
Old database pages      19583
Modified db pages       0
Pending reads 0
Pending writes: LRU 0, flush list 0, single page 0
Pages made young 20, not young 0
0.00 youngs/s, 0.00 non-youngs/s
Pages read 23685, created 29316, written 52866
0.00 reads/s, 0.00 creates/s, 0.78 writes/s
Buffer pool hit rate 1000 / 1000, young-making rate 0 / 1000 not 0 / 1000
Pages read ahead 0.00/s, evicted without access 0.00/s, Random read ahead 0.00/s
LRU len: 53001, unzip_LRU len: 0
I/O sum[0]:cur[0], unzip sum[0]:cur[0]
---BUFFER POOL 7
Buffer pool size        65535
Buffer pool size, bytes 1073725440
Free buffers            11740
Database pages          51816
Old database pages      19147
Modified db pages       0
Pending reads 0
Pending writes: LRU 0, flush list 0, single page 0
Pages made young 43, not young 0
0.00 youngs/s, 0.00 non-youngs/s
Pages read 22666, created 29150, written 58130
0.00 reads/s, 0.00 creates/s, 1.11 writes/s
Buffer pool hit rate 1000 / 1000, young-making rate 0 / 1000 not 0 / 1000
Pages read ahead 0.00/s, evicted without access 0.00/s, Random read ahead 0.00/s
LRU len: 51816, unzip_LRU len: 0
I/O sum[0]:cur[0], unzip sum[0]:cur[0]
--------------
ROW OPERATIONS
--------------
0 queries inside InnoDB, 0 queries in queue
0 read views open inside InnoDB
0 RW transactions active inside InnoDB
0 RO transactions active inside InnoDB
0 out of 1000 descriptors used
Main thread process no. 74272, id 139857921410816, state: sleeping
Number of rows inserted 12277268, updated 41111, deleted 326, read 31539377489
0.11 inserts/s, 0.67 updates/s, 0.00 deletes/s, 27069.99 reads/s
----------------------------
END OF INNODB MONITOR OUTPUT

更新 1

以下是最常出现问题的 table 的一些详细信息:

Engine: Innodb
Row format: Compact
Column Count: 124
Table Row: 581705 (I know, not alot!)
AVG row length: 560
Data lenght: 310.9MB
Index lenght: 411.4MB
Max data length: 0.0bytes
Data free: 4.0mb
Table size (estimate): 722.3MB
file format: antelope
Auto Increment: 745745
Table collation: utf8_unicode_ci

Number of Indices: 34 (most are BTREE, single column, non-unique Indices)

我认为这个 table 最大的问题是 1) 索引的数量和 2) 使用的数据类型。 'bigint(20) unsigned' 有很多用法,我无法想象它是完全没有必要的。还有 'mediumtext' 个字段。

我怀疑由于更新缓慢导致插入缓慢。我怀疑这是对索引数量和使用的大数据类型的影响。

但是,我有很多可用的 CPU/Ram,如果需要我可以使用它,并且想知道是否有任何我可能想要查看的进一步配置。

更新 2

自上次 mysql 重新启动 (https://www.percona.com/blog/2012/06/30/find-unused-indexes/) 以来,我已启用 userstat 来确定一些未使用的索引。事实证明,我只使用了 34 个索引中的 15 个。

更新 3

这是线程备份期间的状态输出:

https://pastebin.com/QhkHGH5s

首先,如果您还没有进行监控,没有收集相同的数据,或者只是喜欢它的外观,您可能会从使用 PMM to get a better insight in real-time. You can compare the servers and look for potential issues, such as saturating I/O, commit pile-ups, etc. There's a live demo version 中获益,这会给您带来想法。

没有 table 结构和示例查询就没有那么多事情要做,这将有助于排除查询本身而不是其他东西,但这里有一些观察结果:

  • 假设统计信息是最新的,索引中的数据比单独的数据更多,这可能表明您需要清理索引并且必须做更多不必要的索引更新工作
  • 发布的 table 显示正在使用的旧 Antelope 格式已被 Barracuda 格式取代;您提到了 mediumtext 字段,通常最有效的方法是将所有值存储在页外,dynamic row format of Barracuda 允许
  • 您已启用 innodb_adaptive_hash_index,但这可能不是 beneficial for your workload,因为您的未命中率看起来比命中率高得多,但仍需要维护 AHI

也不清楚您是将写入发送到单个节点,还是分散它们。 Galera 不会帮助您扩展写入,因此如果您正在写入多个节点并且看到 conflicts 那么切换到使用单个节点可能会有所帮助。