- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在为运行一些不再维护的遗留软件的客户端调整数据库。
数据库包含一个包含 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)
返回速度要快得多,但是它使用了文件排序。
最佳答案
使用 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;
关于mysql - 简单的 MySQL 查询非常慢, `sending data`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59402060/
我有三张 table 。表 A 有选项名称(即颜色、尺寸)。表 B 有选项值名称(即蓝色、红色、黑色等)。表C通过将选项名称id和选项名称值id放在一起来建立关系。 我的查询需要显示值和选项的名称,而
在mysql中,如何计算一行中的非空单元格?我只想计算某些列之间的单元格,比如第 3-10 列之间的单元格。不是所有的列...同样,仅在该行中。 最佳答案 如果你想这样做,只能在 sql 中使用名称而
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 7 年前。 Improve this ques
我正在为版本7.6进行Elasticsearch查询 我的查询是这样的: { "query": { "bool": { "should": [ {
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 7 年前。 Improve this ques
是否可以编写一个查询来检查任一子查询(而不是一个子查询)是否正确? SELECT * FROM employees e WHERE NOT EXISTS (
我找到了很多关于我的问题的答案,但问题没有解决 我有表格,有数据,例如: Data 1 Data 2 Data 3
以下查询返回错误: 查询: SELECT Id, FirstName, LastName, OwnerId, PersonEmail FROM Account WHERE lower(PersonEm
以下查询返回错误: 查询: SELECT Id, FirstName, LastName, OwnerId, PersonEmail FROM Account WHERE lower(PersonEm
我从 EditText 中获取了 String 值。以及提交查询的按钮。 String sql=editQuery.getText().toString();// SELECT * FROM empl
我有一个或多或少有效的查询(关于结果),但处理大约需要 45 秒。这对于在 GUI 中呈现数据来说肯定太长了。 所以我的需求是找到一个更快/更高效的查询(几毫秒左右会很好)我的数据表大约有 3000
这是我第一次使用 Stack Overflow,所以我希望我以正确的方式提出这个问题。 我有 2 个 SQL 查询,我正在尝试比较和识别缺失值,尽管我无法将 NULL 字段添加到第二个查询中以识别缺失
什么是动态 SQL 查询?何时需要使用动态 SQL 查询?我使用的是 SQL Server 2005。 最佳答案 这里有几篇文章: Introduction to Dynamic SQL Dynami
include "mysql.php"; $query= "SELECT ID,name,displayname,established,summary,searchlink,im
我有一个查询要“转换”为 mysql。这是查询: select top 5 * from (select id, firstName, lastName, sum(fileSize) as To
通过我的研究,我发现至少从 EF 4.1 开始,EF 查询上的 .ToString() 方法将返回要运行的 SQL。事实上,这对我来说非常有用,使用 Entity Framework 5 和 6。 但
我在构造查询来执行以下操作时遇到问题: 按activity_type_id过滤联系人,仅显示最近事件具有所需activity_type_id或为NULL(无事件)的联系人 表格结构如下: 一个联系人可
如何让我输入数据库的信息在输入数据 5 分钟后自行更新? 假设我有一张 table : +--+--+-----+ |id|ip|count| +--+--+-----+ |
我正在尝试搜索正好是 4 位数字的 ID,我知道我需要使用 LENGTH() 字符串函数,但找不到如何使用它的示例。我正在尝试以下(和其他变体)但它们不起作用。 SELECT max(car_id)
我有一个在 mysql 上运行良好的 sql 查询(查询 + 连接): select sum(pa.price) from user u , purchase pu , pack pa where (
我是一名优秀的程序员,十分优秀!