包含多个关联的查找速度慢

发布于 2024-11-08 15:17:05 字数 963 浏览 0 评论 0原文

在 Rails 控制器操作中,我调用

@grid = Grid.find(params[:id], :include => [:grid_properties, :grid_entities => [:entity => :properties]])

不幸的是它需要很长时间。 Benchmark.realtime(在开发模式下)告诉我1.8812830448150635,所以大约2秒(Rails 3.0.7,Postgres 8.4)。

当打开 SQL 日志记录时,我得到这行代码的以下信息:

Grid Load (0.9ms)  SELECT "grids".* FROM "grids" WHERE "grids"."id" = 2 LIMIT 1
GridProperty Load (2.5ms)  SELECT "grid_properties".* FROM "grid_properties" WHERE ("grid_properties".grid_id = 2) ORDER BY row_order
GridEntity Load (1.3ms)  SELECT "grid_entities".* FROM "grid_entities" WHERE ("grid_entities".grid_id = 2) ORDER BY row_order
Entity Load (3.0ms)  SELECT "entities".* FROM "entities" WHERE ("entities"."id" IN (28,7,3,6,25,11,2,12))
Property Load (6.4ms)  SELECT "properties".* FROM "properties" WHERE ("properties".entity_id IN (28,7,3,6,25,11,2,12))

每个数据库访问似乎都在低毫秒范围内。为什么最后要花这么长时间?

In a Rails controller action I call

@grid = Grid.find(params[:id], :include => [:grid_properties, :grid_entities => [:entity => :properties]])

Unfortunately it takes really long. Benchmark.realtime (in development mode) tells me 1.8812830448150635, so about 2 seconds (Rails 3.0.7, Postgres 8.4).

When turning on the SQL logging I get the following for this line of code:

Grid Load (0.9ms)  SELECT "grids".* FROM "grids" WHERE "grids"."id" = 2 LIMIT 1
GridProperty Load (2.5ms)  SELECT "grid_properties".* FROM "grid_properties" WHERE ("grid_properties".grid_id = 2) ORDER BY row_order
GridEntity Load (1.3ms)  SELECT "grid_entities".* FROM "grid_entities" WHERE ("grid_entities".grid_id = 2) ORDER BY row_order
Entity Load (3.0ms)  SELECT "entities".* FROM "entities" WHERE ("entities"."id" IN (28,7,3,6,25,11,2,12))
Property Load (6.4ms)  SELECT "properties".* FROM "properties" WHERE ("properties".entity_id IN (28,7,3,6,25,11,2,12))

Every database access seems to be in the low millisecond range. Why does it take so long in the end?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦冥 2024-11-15 15:17:05

我猜想时间都花在了 Ruby 创建对象上。该网格中有多少个属性? “SELECT FROM Properties”的时间看起来比较长。

有一些可以放置检查点的函数 - logger.debug "CHECKPOINT: #{Time.now} #{caller(0).first}" 您可以进一步调查该问题

如果您 也许有任何“on_load”回调?

I guess that the time is spent on creating the object by Ruby. How many Properties are in that Grid? The time of 'SELECT FROM properties' looks relatively high.

You could further investigate the problem, if you have some functions where you could place checkpoints - logger.debug "CHECKPOINT: #{Time.now} #{caller(0).first}"

Do you have any 'on_load' callbacks, maybe?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文