我有一个查询,它只接受.001秒,没有部分订单。但是,当我在查询中添加order时,大约需要.736秒的时间。时间太长了。我已将该列编入索引,但它不像预期的那样起作用。
以下是查询:
SELECT DISTINCT
n0_.id AS id0, n0_.published_at AS published_at1
FROM
news n0_
INNER JOIN
news_translations n1_ ON n0_.id = n1_.translatable_id
WHERE
((n0_.unpublished_at IS NOT NULL
AND (CURRENT_TIMESTAMP >= n0_.published_at
AND CURRENT_TIMESTAMP < n0_.unpublished_at))
OR (CURRENT_TIMESTAMP >= n0_.published_at
AND n0_.unpublished_at IS NULL))
AND
(n0_.status = 1
AND n0_.content_type_id = 1)
AND n1_.locale = 'zh_CN'
and n0_.id NOT IN (139476 , 226, 225, 224, 223, 218, 213, 69, 65, 62)
GROUP BY n0_.id
#ORDER BY n0_.published_at DESC
LIMIT 10 OFFSET 0;以下是显示键的输出:
Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type
news 0 PRIMARY 1 id A 139656 NULL NULL BTREE
news 0 UNIQ_1DD399501204232D 1 comment_box_id A 139656 NULL NULL YES BTREE
news 1 IDX_1DD39950510C33D5 1 approval_type_id A 4 NULL NULL YES BTREE
news 1 IDX_1DD399501A445520 1 content_type_id A 36 NULL NULL YES BTREE
news 1 IDX_1DD39950A76ED395 1 user_id A 4 NULL NULL YES BTREE
news 1 IDX_1DD3995042A9A1DE 1 smiley_group_id A 4 NULL NULL YES BTREE
news 1 IDX_1DD39950B3FE509D 1 survey_id A 4 NULL NULL YES BTREE
news 1 IDX_1DD3995029C1004E 1 video_id A 4 NULL NULL YES BTREE
news 1 IDX_1DD399501CDF82CA 1 cover_image A 4 NULL NULL YES BTREE
news 1 IDX_1DD399504E7AF8F 1 gallery_id A 4 NULL NULL YES BTREE
news 1 IDX_1DD399506048130B 1 home_position_id A 4 NULL NULL YES BTREE
news 1 news_published_at_index 1 published_at A 139656 NULL NULL BTREE发布于 2015-01-28 09:02:01
排序是一项昂贵的操作,因此排序许多行将需要时间。一种方法是在订购前减少结果,但这并不总是可能的。
还可以考虑在您的表中可以找到哪些其他模式并使用它。我非常肯定,您的数据总是由id升序(可能也是发布时间)排序的。正如我所看到的,您是按发布时间排序的,因此,如果数据是由表中的这一列自然排序的,那么您可以将其用于您的优势。但要确保是这样的,所以没有例外。例如,如果在插入新行时总是使用NOW(),那么很有可能对它们进行排序。
https://stackoverflow.com/questions/28187273
复制相似问题