小编Mar*_*rco的帖子

操纵ActiveRecord对象以构建JSON

我们正在尝试加快网页加载速度.主要问题是该页面上的一些activerecord生成的查询.

如果你看一下他们生成的SQL(Postgres),查询如下(所有代码都有点简化):

select * from feed_details where account_id = 5
select * from feeds where id in (VERY_LONG_LIST_OF_IDS_FROM_FIRST_QUERY)
select * from feeds_metadata where feed_id in (VERY_LONG_LIST_FROM_FIRST_QUERY)
select * from documents where feed_id in (VERY_LONG_LIST_FROM_FIRST_QUERY)
Run Code Online (Sandbox Code Playgroud)

所有索引都已到位,但我们通过将其更改为以下内容来验证他们只花了不到一半的时间:

select * from feeds, feed_details where id = feed_id and account_id = 5
select * from feeds_metadata fm, feed_details fd where fm.feed_id = fd.feed_id and account_id = 5
select * from documents where d, feed_details fd where d.feed_id = fd.feed_id and account_id = 5
Run Code Online (Sandbox Code Playgroud)

到目前为止,这么简单.以下是Rails问题:这些查询是由FeedDetail模型中的此代码生成的: …

postgresql activerecord json ruby-on-rails

5
推荐指数
0
解决办法
140
查看次数

标签 统计

activerecord ×1

json ×1

postgresql ×1

ruby-on-rails ×1