简单的MySQL查询速度很慢,`发送数据`

问题描述 投票:1回答:1

我正在为运行一些不再维护的旧版软件的客户端调整数据库。

该数据库包含一个表(文章),其中包含716,141条记录。下表如下:

 CREATE TABLE `Articles` (
  `id` int(6) NOT NULL AUTO_INCREMENT,
  `category_id` int(6) NOT NULL DEFAULT '0',
  `user_id` int(11) DEFAULT NULL,
  `user_domain` varchar(255) CHARACTER SET latin1 DEFAULT 'example@localhost',
  `author` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `title` varchar(255) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
  `image` varchar(255) CHARACTER SET latin1 DEFAULT NULL,
  `created` datetime NOT NULL,
  `body` longtext COLLATE utf8_unicode_ci,
  `last_read` datetime DEFAULT NULL,
  `times_read` int(6) DEFAULT '0',
  `today_read` int(6) DEFAULT '0',
  `status` int(3) NOT NULL DEFAULT '0',
  `sefriendly` varchar(64) CHARACTER SET latin1 DEFAULT NULL,
  `link` varchar(255) CHARACTER SET latin1 DEFAULT NULL,
  `order_num` int(6) DEFAULT NULL,
  `show_poll` enum('0','1') COLLATE utf8_unicode_ci DEFAULT '1',
  `show_comment` enum('0','1') COLLATE utf8_unicode_ci DEFAULT '1',
  `rss_feed` enum('0','1') COLLATE utf8_unicode_ci DEFAULT '1',
  `keywords` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `description` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `emailed` int(11) DEFAULT '0',
  `vote_num` int(11) DEFAULT '0',
  `vote_sum` int(11) DEFAULT '0',
  `abstract` text COLLATE utf8_unicode_ci,
  `image_caption` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `member_submition` tinyint(4) DEFAULT '0',
  `lat` double DEFAULT NULL,
  `lng` double DEFAULT NULL,
  `last_edited` datetime NOT NULL,
  `video_link` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `video_link2` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `video_link3` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `video_code_1` longtext COLLATE utf8_unicode_ci,
  `video_code_2` longtext COLLATE utf8_unicode_ci,
  `video_code_3` longtext COLLATE utf8_unicode_ci,
  PRIMARY KEY (`id`),
  KEY `sefriendly_category_id` (`sefriendly`,`category_id`),
  KEY `order_num` (`order_num`),
  KEY `today_read` (`today_read` DESC),
  KEY `created` (`created` DESC),
  KEY `created_2` (`created`,`category_id`,`status`),
  FULLTEXT KEY `title_body` (`title`,`body`,`abstract`),
  FULLTEXT KEY `title_abstract` (`title`,`abstract`)
) ENGINE=InnoDB AUTO_INCREMENT=718440 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci ROW_FORMAT=COMPRESSED

慢速查询是:

SELECT a.* FROM Articles as a WHERE ((a.created < '2019-12-19 03:02:00')) AND ((a.category_id IN ('118'))) AND ((a.status > 0)) ORDER BY a.today_read DESC  LIMIT 0,18

说明输出:

mysql> explain SELECT a.* FROM Articles as a WHERE ((a.created < '2019-12-19 03:02:00')) AND ((a.category_id IN ('118'))) AND ((a.status > 0)) ORDER BY a.today_read DESC  LIMIT 0,18;
+----+-------------+-------+------------+-------+-------------------+------------+---------+------+------+----------+-------------+
| id | select_type | table | partitions | type  | possible_keys     | key        | key_len | ref  | rows | filtered | Extra       |
+----+-------------+-------+------------+-------+-------------------+------------+---------+------+------+----------+-------------+
|  1 | SIMPLE      | a     | NULL       | index | created,created_2 | today_read | 5       | NULL |   36 |     1.67 | Using where |
+----+-------------+-------+------------+-------+-------------------+------------+---------+------+------+----------+-------------+
1 row in set, 1 warning (0.00 sec)

此查询平均需要70秒才能完成。服务器绝不是随便在SSD上运行32GB RAM的地方。 MySQL已经过调整,可以将索引保留在RAM中,并且服务器不受I / O约束。

编辑:根据要求提供InnoDB状态

mysql> show engine innodb status\G
*************************** 1. row ***************************
  Type: InnoDB
  Name: 
Status: 
=====================================
2019-12-19 03:14:39 0x7f4d2fef8700 INNODB MONITOR OUTPUT
=====================================
Per second averages calculated from the last 5 seconds
-----------------
BACKGROUND THREAD
-----------------
srv_master_thread loops: 13556 srv_active, 0 srv_shutdown, 11958 srv_idle
srv_master_thread log flush and writes: 0
----------
SEMAPHORES
----------
OS WAIT ARRAY INFO: reservation count 29474283
--Thread 139965468940032 has waited at buf0buf.cc line 3699 for 0 seconds the semaphore:
Mutex at 0x7f4d38338d28, Mutex BUF_POOL_LRU_LIST created buf0buf.cc:1249, lock var 0

--Thread 139965470496512 has waited at buf0buf.cc line 3699 for 0 seconds the semaphore:
Mutex at 0x7f4d38338d28, Mutex BUF_POOL_LRU_LIST created buf0buf.cc:1249, lock var 0

--Thread 139963171264256 has waited at buf0buf.cc line 3699 for 0 seconds the semaphore:
Mutex at 0x7f4d38338d28, Mutex BUF_POOL_LRU_LIST created buf0buf.cc:1249, lock var 0

--Thread 139969309939456 has waited at buf0flu.cc line 1954 for 0 seconds the semaphore:
Mutex at 0x7f4d38338d28, Mutex BUF_POOL_LRU_LIST created buf0buf.cc:1249, lock var 0

--Thread 139963181815552 has waited at buf0buf.cc line 3699 for 0 seconds the semaphore:
Mutex at 0x7f4d38338d28, Mutex BUF_POOL_LRU_LIST created buf0buf.cc:1249, lock var 0

--Thread 139965480539904 has waited at buf0buf.cc line 3699 for 0 seconds the semaphore:
Mutex at 0x7f4d38338d28, Mutex BUF_POOL_LRU_LIST created buf0buf.cc:1249, lock var 0

--Thread 139965469685504 has waited at buf0buf.cc line 2969 for 0 seconds the semaphore:
Mutex at 0x7f4d38338d28, Mutex BUF_POOL_LRU_LIST created buf0buf.cc:1249, lock var 0

--Thread 139963186280192 has waited at buf0buf.cc line 2969 for 0 seconds the semaphore:
Mutex at 0x7f4d38338d28, Mutex BUF_POOL_LRU_LIST created buf0buf.cc:1249, lock var 0

OS WAIT ARRAY INFO: signal count 268177117
RW-shared spins 204431063, rounds 218976597, OS waits 11030754
RW-excl spins 259209669, rounds 698509416, OS waits 10044516
RW-sx spins 93, rounds 2759, OS waits 77
Spin rounds per wait: 1.07 RW-shared, 2.69 RW-excl, 29.67 RW-sx
------------
TRANSACTIONS
------------
Trx id counter 3904031320
Purge done for trx's n:o < 3904031029 undo n:o < 0 state: running but idle
History list length 81
LIST OF TRANSACTIONS FOR EACH SESSION:
---TRANSACTION 421444691213536, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
---TRANSACTION 421444691183440, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
--------
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] , aio writes: [0, 0, 0, 0] ,
 ibuf aio reads:, log i/o's:, sync i/o's:
Pending flushes (fsync) log: 0; buffer pool: 2
22371193 OS file reads, 995730 OS file writes, 349388 OS fsyncs
861.23 reads/s, 8192 avg bytes/read, 0.00 writes/s, 0.00 fsyncs/s
-------------------------------------
INSERT BUFFER AND ADAPTIVE HASH INDEX
-------------------------------------
Ibuf: size 1, free list len 25, seg size 27, 42244 merges
merged operations:
 insert 13382, delete mark 41950, delete 207
discarded operations:
 insert 0, delete mark 0, delete 0
Hash table size 796967, node heap has 1 buffer(s)
Hash table size 796967, node heap has 1 buffer(s)
Hash table size 796967, node heap has 344 buffer(s)
Hash table size 796967, node heap has 1 buffer(s)
Hash table size 796967, node heap has 0 buffer(s)
Hash table size 796967, node heap has 2 buffer(s)
Hash table size 796967, node heap has 1 buffer(s)
Hash table size 796967, node heap has 1 buffer(s)
193618.28 hash searches/s, 256874.23 non-hash searches/s
---
LOG
---
Log sequence number          1688350147697
Log buffer assigned up to    1688350147697
Log buffer completed up to   1688350147697
Log written up to            1688350147697
Log flushed up to            1688350147697
Added dirty pages up to      1688350147697
Pages flushed up to          1688350147697
Last checkpoint at           1688350147697
Checkpoint age target 725689856
Modified age no less than 1688350147697
Checkpoint age        0
337075 log i/o's done, 0.00 log i/o's/second
----------------------
BUFFER POOL AND MEMORY
----------------------
Total large memory allocated 3298295808
Dictionary memory allocated 928170
Buffer pool size   196584
Buffer pool size, bytes 3220832256
Free buffers       2015
Database pages     269834
Old database pages 99545
Modified db pages  0
Pending reads      0
Pending writes: LRU 1, flush list 0, single page 0
Pages made young 430174930, not young 1036444558
0.00 youngs/s, 0.00 non-youngs/s
Pages read 22371308, created 13828, written 593768
0.00 reads/s, 0.00 creates/s, 0.00 writes/s
Buffer pool hit rate 1000 / 1000, young-making rate 11 / 1000 not 52 / 1000
Pages read ahead 0.00/s, evicted without access 0.00/s, Random read ahead 0.00/s
LRU len: 269834, unzip_LRU len: 59238
I/O sum[115395]:cur[867], unzip sum[4081080]:cur[53229]
----------------------
INDIVIDUAL BUFFER POOL INFO
----------------------
---BUFFER POOL 0
Buffer pool size   65528
Buffer pool size, bytes 1073610752
Free buffers       836
Database pages     89951
Old database pages 33184
Modified db pages  0
Pending reads      0
Pending writes: LRU 0, flush list 0, single page 0
Pages made young 142189681, not young 229262950
0.00 youngs/s, 0.00 non-youngs/s
Pages read 7349759, created 4478, written 209859
0.00 reads/s, 0.00 creates/s, 0.00 writes/s
Buffer pool hit rate 1000 / 1000, young-making rate 14 / 1000 not 13 / 1000
Pages read ahead 0.00/s, evicted without access 0.00/s, Random read ahead 0.00/s
LRU len: 89951, unzip_LRU len: 19582
I/O sum[38465]:cur[289], unzip sum[1360360]:cur[17743]
---BUFFER POOL 1
Buffer pool size   65528
Buffer pool size, bytes 1073610752
Free buffers       806
Database pages     89892
Old database pages 33162
Modified db pages  0
Pending reads      0
Pending writes: LRU 0, flush list 0, single page 0
Pages made young 144045149, not young 427557132
0.00 youngs/s, 0.00 non-youngs/s
Pages read 7476223, created 4229, written 199170
0.00 reads/s, 0.00 creates/s, 0.00 writes/s
Buffer pool hit rate 1000 / 1000, young-making rate 7 / 1000 not 74 / 1000
Pages read ahead 0.00/s, evicted without access 0.00/s, Random read ahead 0.00/s
LRU len: 89892, unzip_LRU len: 19640
I/O sum[38465]:cur[289], unzip sum[1360360]:cur[17743]
---BUFFER POOL 2
Buffer pool size   65528
Buffer pool size, bytes 1073610752
Free buffers       373
Database pages     89991
Old database pages 33199
Modified db pages  0
Pending reads      0
Pending writes: LRU 1, flush list 0, single page 0
Pages made young 143940100, not young 379624476
0.00 youngs/s, 0.00 non-youngs/s
Pages read 7545326, created 5121, written 184739
0.00 reads/s, 0.00 creates/s, 0.00 writes/s
Buffer pool hit rate 1000 / 1000, young-making rate 16 / 1000 not 45 / 1000
Pages read ahead 0.00/s, evicted without access 0.00/s, Random read ahead 0.00/s
LRU len: 89991, unzip_LRU len: 20016
I/O sum[38465]:cur[289], unzip sum[1360360]:cur[17743]
--------------
ROW OPERATIONS
--------------
0 queries inside InnoDB, 0 queries in queue
32 read views open inside InnoDB
0 RW transactions active inside InnoDB
---OLDEST VIEW---
Read view low limit trx n:o 3904031026
Trx read view will not see trx with id >= 3904031026, sees < 3904031026
Read view individually stored trx ids:
-----------------
Process ID=26910, Main thread ID=139965763704576 , state=sleeping
Number of rows inserted 30707, updated 64458, deleted 88302, read 47467692182
0.00 inserts/s, 0.00 updates/s, 0.00 deletes/s, 455069.99 reads/s
----------------------------
END OF INNODB MONITOR OUTPUT
============================

编辑,使用@Barmar建议的力索引

mysql> explain SELECT a.* FROM Articles as a FORCE INDEX (created_2) WHERE ((a.created < '2019-12-19 03:02:00')) AND ((a.category_id IN ('118'))) AND ((a.status > 0)) ORDER BY a.today_read DESC  LIMIT 0,18;
+----+-------------+-------+------------+-------+---------------+-----------+---------+------+--------+----------+---------------------------------------+
| id | select_type | table | partitions | type  | possible_keys | key       | key_len | ref  | rows   | filtered | Extra                                 |
+----+-------------+-------+------------+-------+---------------+-----------+---------+------+--------+----------+---------------------------------------+
|  1 | SIMPLE      | a     | NULL       | range | created_2     | created_2 | 5       | NULL | 281975 |     3.33 | Using index condition; Using filesort |
+----+-------------+-------+------------+-------+---------------+-----------+---------+------+--------+----------+---------------------------------------+
1 row in set, 1 warning (0.00 sec)

此方法返回的速度更快,但是已求助于文件排序。

mysql
1个回答
0
投票

使用FORCE INDEX使其更喜欢使用created_2索引进行过滤,而不是使用today_read索引进行排序。

SELECT a.* FROM 
Articles as a 
FORCE INDEX (created_2) 
WHERE ((a.created < '2019-12-19 03:02:00')) AND ((a.category_id IN ('118'))) AND ((a.status > 0)) 
ORDER BY a.today_read DESC  
LIMIT 0,18;
© www.soinside.com 2019 - 2024. All rights reserved.