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

时间:2018-08-14 14:28:04

标签: mysql percona percona-xtradb-cluster

我正在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秒)。因此,肯定存在某种瓶颈,我想知道是否可以通过配置调整来解决。

我知道有几个表可能需要删除一些未使用的索引,但是我想知道还有什么要研究以提高查询的性能。负载并不是那么大,它是一个内部系统,大约有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

以下是有关该表最常出现问题的一些详细信息:

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)

我认为此表的最大问题是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

1 个答案:

答案 0 :(得分:0)

首先,如果您尚未监视,未收集相同的数据,或者只是外观类似,则可以受益于使用PMM来实时获得更好的洞察力。您可以比较服务器并查找潜在的问题,例如I / O饱和,提交堆积等。有一个live demo version可以为您提供想法。

没有表结构和示例查询就没有什么可做的了,这将有助于排除查询本身,而不是其他事情,但是这里有一些观察结果:

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

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