目前我正在开发一个大型 Web 应用程序并使其运行更快,我决定重构所有 N+1 查询(以减少对数据库的请求数量,http://rails-bestpractices.com/posts/29-fix-n -1-查询)。所以我安装了现在不适用于 Rails 3.1.1 的 gem "bullet"(你可以使用来自https://github.com/flyerhzm/bullet的 fork )。在每个页面上使用 declarative_authorization gem 时,我都会收到相同的警报:
N+1 Query detected
Role => [:permissions]
Add to your finder: :include => [:permissions]
N+1 Query detected
Permission => [:permission_rules]
Add to your finder: :include => [:permission_rules]
CACHE (0.0ms) SELECT "roles".* FROM "roles"
CACHE (0.0ms) SELECT "permissions".* FROM "permissions" WHERE "permissions"."role_id" = 1
CACHE (0.0ms) SELECT "permissions".* FROM "permissions" WHERE "permissions"."role_id" = 2
CACHE (0.0ms) SELECT "permissions".* FROM "permissions" WHERE "permissions"."role_id" = 3
CACHE (0.0ms) SELECT "permissions".* FROM "permissions" WHERE "permissions"."role_id" = 4
CACHE (0.0ms) SELECT "permissions".* FROM "permissions" WHERE "permissions"."role_id" = 6
CACHE (0.0ms) SELECT "permissions".* FROM "permissions" WHERE "permissions"."role_id" = 7
CACHE (0.0ms) SELECT "permissions".* FROM "permissions" WHERE "permissions"."role_id" = 8
CACHE (0.0ms) SELECT "permission_rules".* FROM "permission_rules" INNER JOIN "permission_rules_permissions" ON "permission_rules"."id" = "permission_rules_permissions"."permission_rule_id" WHERE "permission_rules_permissions"."permission_id" = 30
CACHE (0.0ms) SELECT "permission_rules".* FROM "permission_rules" INNER JOIN "permission_rules_permissions" ON "permission_rules"."id" = "permission_rules_permissions"."permission_rule_id" WHERE "permission_rules_permissions"."permission_id" = 31
...
您能帮我解决这个问题并加快查询速度吗?