mysql 5.6(percona):如何提高update和insert的查询性能?

2jcobegt  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(383)

我正在lxc容器上运行percona5.6集群(3个节点)。各节点硬件如下:

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

我的数据库大小不到4gb。我已经设法调整了配置设置,以便快速处理选择。但是,我看到一些更新和插入执行得非常慢(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重启以来,我已经启用userstat来确定一些未使用的索引(https://www.percona.com/blog/2012/06/30/find-unused-indexes/). 结果发现我只使用了34个索引中的15个。
更新3
以下是线程备份期间的状态输出:
https://pastebin.com/qhkhgh5s

yebdmbv4

yebdmbv41#

首先,如果您还没有进行监控,没有收集相同的数据,或者只是喜欢它的外观,那么使用pmm可以获得更好的实时洞察效果。您可以比较服务器并查找潜在的问题,如i/o饱和、提交堆积等。有一个实时演示版本可以让您了解这一点。
如果没有表结构和示例查询,就不会有太多问题,这将有助于排除查询本身,而不是其他查询,但以下是一些观察结果:
假设统计数据是最新的,那么索引中的数据比单独的数据中的数据要多,这可能表明您需要清理索引,并且更新索引的工作要比必要的多
发布的表显示了较旧的 Antelope 正在使用的格式,已被 Barracuda 格式;你提到 mediumtext 字段,通常最有效的方法是在页外存储所有值,这是barracuda的动态行格式所允许的
你有 innodb_adaptive_hash_index 启用,但这可能对您的工作负载没有好处,因为您的未命中率看起来比命中率高得多,但仍然需要维护ahi
还不清楚您是将写操作发送到单个节点,还是将它们分散。galera不能帮助您扩展写操作,因此如果您正在向多个节点进行写操作并且看到冲突,那么切换到使用单个节点可能会有所帮助。

相关问题