我想知道为什么这样做
UserView.where(:user_viewable_type => 'Song').order('created_at desc').limit(5)
运行需要 1213 毫秒。有点慢 innit?
在 schema.rbuser_viewable_type
中被索引:
add_index "user_views", ["user_viewable_type"], :name => "index_views_on_viewable_type"
即使在 psql 中:
"index_views_on_viewable_type" btree (user_viewable_type)
运行.explain
返回:
UserView.where(:user_viewable_type => 'Song').order('created_at desc').limit(5).explain
UserView Load (1801.4ms) SELECT "user_views".* FROM "user_views" WHERE "user_views"."user_viewable_type" = 'Song' ORDER BY created_at desc LIMIT 5
EXPLAIN (1.6ms) EXPLAIN SELECT "user_views".* FROM "user_views" WHERE "user_views"."user_viewable_type" = 'Song' ORDER BY created_at desc LIMIT 5
=> "EXPLAIN for: SELECT \"user_views\".* FROM \"user_views\" WHERE \"user_views\".\"user_viewable_type\" = 'Song' ORDER BY created_at desc LIMIT 5\n QUERY PLAN\n-------------------------------------------------------------------------------\n Limit (cost=17113.28..17113.28 rows=5 width=37)\n -> Sort (cost=17113.28..17147.82 rows=69085 width=37)\n Sort Key: created_at\n -> Seq Scan on user_views (cost=0.00..16883.78 rows=69085 width=37)\n Filter: ((user_viewable_type)::text = 'Song'::text)\n(5 rows)\n"
所以我决定分解查询并删除order
,我得到:
UserView.where(:user_viewable_type => 'Song').limit(5).explain
UserView Load (1.6ms) SELECT "user_views".* FROM "user_views" WHERE "user_views"."user_viewable_type" = 'Song' LIMIT 5
似乎order('created_at desc')
是导致查询变慢。但为什么?不能快速订购吗?
order
我在id
专栏上试过:
UserView.where(:user_viewable_type => 'Song').order('id desc').limit(5).explain
UserView Load (44.8ms) SELECT "user_views".* FROM "user_views" WHERE "user_views"."user_viewable_type" = 'Song' ORDER BY id desc LIMIT 5
这么快!有什么问题?
我应该提几点:
- 如果您还没有注意到,这是一个多态表。
- 过去,我通过 Rails 迁移重命名
:live_viewable_type
为. 但是,我不认为这应该是问题,因为索引仍然在右列。:user_viewable_type
t.rename :live_viewable_type, :user_viewable_type