MySQL performance issue comparing 2 DateTime fields on the same table -
my feed_listingjob
has 2 datetime fields:
+------------+-------------+------+-----+---------+----------------+ | field | type | null | key | default | | +------------+-------------+------+-----+---------+----------------+ | 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
both indexed individually below:
mysql> show indexes 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 | | 64534 | null | null | | btree | | | | feed_listingjob | 1 | feed_listingjob_job_id_4c3b1b514481f269_fk_feed_importjob_id | 1 | job_id | | 2081 | null | null | | btree | | | | feed_listingjob | 1 | feed_listingjob_fde81f11 | 1 | created_at | | 64534 | null | null | | btree | | | | feed_listingjob | 1 | feed_listingjob_afd1a1a8 | 1 | updated_at | | 64534 | null | null | | btree | | | | feed_listingjob | 1 | feed_listingjob_381895a2 | 1 | es_sync_at | | 2 | null | null | yes | btree | | | +-----------------+------------+--------------------------------------------------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 5 rows in set (0.00 sec)
and query not using indexes:
mysql> explain select `feed_listingjob`.`id` `feed_listingjob` `feed_listingjob`.`es_sync_at` < `feed_listingjob`.`updated_at` limit 10; +----+-------------+-----------------+------+---------------+------+---------+------+-------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | | +----+-------------+-----------------+------+---------------+------+---------+------+-------+-------------+ | 1 | simple | feed_listingjob | | null | null | null | null | 53534 | using | +----+-------------+-----------------+------+---------------+------+---------+------+-------+-------------+ 1 row in set (0.01 sec)
can please tell me why , how optimise query?
the index can't used here because feed_listingjob
.updated_at
not constant. means every row have examined check condition. 1 way use index have column stores difference between es_sync_at , updated_at timestamps. if store difference = (es_sync_at - updated_at timestamps) query becomes (difference < 0).
Comments
Post a Comment