比较同一表上的2个DateTime字段时出现MySQL性能问题
My比较同一表上的2个DateTime字段时出现MySQL性能问题,mysql,Mysql,Myfeed\u listingjob有两个日期时间字段: +------------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +------------+-------------+------+-----+---------+----------------+ | id
feed\u listingjob
有两个日期时间字段:
+------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| data | longtext | NO | | NULL | |
| meta_data | longtext | NO | | NULL | |
| state | varchar(25) | NO | | NULL | |
| error | longtext | NO | | NULL | |
| job_id | int(11) | NO | MUL | NULL | |
| created_at | datetime(6) | NO | MUL | NULL | |
| updated_at | datetime(6) | NO | MUL | NULL | |
| es_sync_at | datetime(6) | YES | MUL | NULL | |
+------------+-------------+------+-----+---------+----------------+
updated_at
和es_sync_at
都单独编制了索引,如下所示:
mysql> show indexes from feed_listingjob;
+-----------------+------------+--------------------------------------------------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-----------------+------------+--------------------------------------------------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| feed_listingjob | 0 | PRIMARY | 1 | id | A | 64534 | NULL | NULL | | BTREE | | |
| feed_listingjob | 1 | feed_listingjob_job_id_4c3b1b514481f269_fk_feed_importjob_id | 1 | job_id | A | 2081 | NULL | NULL | | BTREE | | |
| feed_listingjob | 1 | feed_listingjob_fde81f11 | 1 | created_at | A | 64534 | NULL | NULL | | BTREE | | |
| feed_listingjob | 1 | feed_listingjob_afd1a1a8 | 1 | updated_at | A | 64534 | NULL | NULL | | BTREE | | |
| feed_listingjob | 1 | feed_listingjob_381895a2 | 1 | es_sync_at | A | 2 | NULL | NULL | YES | BTREE | | |
+-----------------+------------+--------------------------------------------------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
5 rows in set (0.00 sec)
我的查询没有使用索引:
mysql> explain SELECT `feed_listingjob`.`id` FROM `feed_listingjob` WHERE `feed_listingjob`.`es_sync_at` < `feed_listingjob`.`updated_at` LIMIT 10;
+----+-------------+-----------------+------+---------------+------+---------+------+-------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-----------------+------+---------------+------+---------+------+-------+-------------+
| 1 | SIMPLE | feed_listingjob | ALL | NULL | NULL | NULL | NULL | 53534 | Using where |
+----+-------------+-----------------+------+---------------+------+---------+------+-------+-------------+
1 row in set (0.01 sec)
mysql>explain选择'feed\u listingjob`.'id`来自'feed\u listingjob`其中的'feed\u listingjob`.'es\u sync\u位于`<'feed\u listingjob`.'updated\u位于'LIMIT 10;
+----+-------------+-----------------+------+---------------+------+---------+------+-------+-------------+
|id |选择|类型|类型|可能的|键|键|列|参考|行|额外|
+----+-------------+-----------------+------+---------------+------+---------+------+-------+-------------+
|1 | SIMPLE | feed | u listingjob | ALL | NULL | NULL | NULL | NULL | 5353534 |使用where|
+----+-------------+-----------------+------+---------------+------+---------+------+-------+-------------+
一行一组(0.01秒)
有人能告诉我为什么以及如何优化此查询吗?此处不能使用索引,因为
提要\u列表作业
更新的\u at
不是一个常数。这意味着必须检查每一行以检查状况。使用索引的一种方法是使用另一列来存储es_sync_at和updated_at时间戳之间的差异。如果将其存储为difference=(es_sync_at-updated_at timestamps),则查询变为(difference<0)。请注意,没有ORDER BY的限制是毫无意义的。您希望返回行的顺序可能会影响索引的选择。因此,让it使用索引的一种方法是使用order by。此外,join这也是我的想法,看起来应该避免在大表上进行字段比较。字段比较是可以的,只要将其与常量进行比较,并且不需要对字段本身进行求值。例如,将整型列与字符串(如“123”)进行比较要比将其与123进行比较慢得多,因为将其与“123”进行比较意味着整型字段需要转换为字符串。常量是什么意思?我想您的意思是field<123
其中123
是常量,因为它在整个查询过程中不会改变,但你上面的评论似乎有别的意思。我的意思是“123”是一个字符串,而123是一个整数。两者都是常量,但其中一个涉及转换。