我有一个类似于这样的模式:
create table image_tags (
image_tag_id serial primary key,
image_id int not null
);
create index on image_tags(image_id);当我用两列执行查询时,它的速度太慢了(例如,select * from image_tags order by image_id desc, image_tag_id desc limit 10;)。如果我把其中一列放在排序中(不管是哪一列),它是超快的。
我在这两个查询中都使用了explain,但这并不能帮助我理解为什么order by子句中的两列速度这么慢,它只是向我展示了使用两列的速度有多慢。
发布于 2014-09-29 03:52:31
要通过索引优化order by image_id desc, image_tag_id desc排序,需要有以下索引:
create index image_tags_id_tag on image_tags(image_id, image_tag_id);只有有一个复合索引(我假设只有很少的例外,但在本例中不例外),才能帮助优化器立即使用它来确定顺序。
发布于 2014-09-29 03:54:43
create index on image_tags(image_id, image_tag_id);试试索引..。
发布于 2014-09-29 03:53:57
您只为要执行的查询关联的一个列建立索引,为了更快地执行,您应该创建一个两列索引,如
create index on image_tags(image_id, image_tag_id);https://stackoverflow.com/questions/26092250
复制相似问题