【问题标题】:Rails: Reducing the amount of ActiveStorage queriesRails:减少 ActiveStorage 查询的数量
【发布时间】:2023-03-09 14:11:02
【问题描述】:

我有一个显示 3 种不同资源(Ablum、Samplepack 和 Demo)的索引页面,其中 2 种带有附加图像。我正在通过我的控制器加载它们,如下所示:

# static_controller.rb   
  @resources = []
  @resources.push(Album.all, Demo.all, Samplepack.all)

它会产生 很多 的 SQL 查询以加载它们。我想知道是否有最有效的加载附件的方法?如果有人可以复制/粘贴我一些阅读的链接,那就太棒了! 我在考虑includesjoins 的一些东西,但我在网上找不到任何参考。

祝你有美好的一天

编辑:正如 Sebastian Palma 提到的,with_attached_<attachment> 是必需的(为简洁起见,非详尽的日志)

 [Album.all, Demo.all, Samplepack.all]

# resulting SQL

  # Album n°1
  ActiveStorage::Attachment Load (1.8ms)   SELECT  "active_storage_attachments".* FROM "active_storage_attachments" WHERE "active_storage_attachments"."record_id" = $1 AND "active_storage_attachments"."record_type" = $2 AND "active_storage_attachments"."name" = $3 LIMIT $4  [["record_id", 82], ["record_type", "Album"], ["name", "album_artwork"], ["LIMIT", 1]]
  ActiveStorage::Blob Load (2.4ms)         SELECT  "active_storage_blobs".*       FROM "active_storage_blobs"       WHERE "active_storage_blobs"."id" = $1 LIMIT $2  [["id", 1227], ["LIMIT", 1]]
  # Ablum n°2 
  ActiveStorage::Attachment Load (0.2ms)   SELECT  "active_storage_attachments".* FROM "active_storage_attachments" WHERE "active_storage_attachments"."record_id" = $1 AND "active_storage_attachments"."record_type" = $2 AND "active_storage_attachments"."name" = $3 LIMIT $4  [["record_id", 83], ["record_type", "Album"], ["name", "album_artwork"], ["LIMIT", 1]]
  ActiveStorage::Blob Load (0.1ms)         SELECT  "active_storage_blobs".*       FROM "active_storage_blobs"       WHERE "active_storage_blobs"."id" = $1 LIMIT $2  [["id", 1228], ["LIMIT", 1]]
  # Album n°3
  ActiveStorage::Attachment Load (0.2ms)   SELECT  "active_storage_attachments".* FROM "active_storage_attachments" WHERE "active_storage_attachments"."record_id" = $1 AND "active_storage_attachments"."record_type" = $2 AND "active_storage_attachments"."name" = $3 LIMIT $4  [["record_id", 84], ["record_type", "Album"], ["name", "album_artwork"], ["LIMIT", 1]]
  ActiveStorage::Blob Load (0.2ms)         SELECT  "active_storage_blobs".*       FROM "active_storage_blobs"       WHERE "active_storage_blobs"."id" = $1 LIMIT $2  [["id", 1229], ["LIMIT", 1]]
  ...
  ... N+1 town (even N+2 ?)

使用with_attached_<attachment>

 @resources = [
   Album.with_attached_album_artwork.all,
   Demo.all,
   Samplepack.with_attached_album_artwork.all
 ]

  # Albums
  ActiveStorage::Attachment Load (0.6ms)  SELECT "active_storage_attachments".* FROM "active_storage_attachments" WHERE "active_storage_attachments"."record_type" = $1 AND "active_storage_attachments"."name" = $2 AND "active_storage_attachments"."record_id" IN ($3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14)  [["record_type", "Album"], ["name", "album_artwork"], ["record_id", 82], ["record_id", 83], ["record_id", 84], ["record_id", 85], ["record_id", 86], ["record_id", 87], ["record_id", 88], ["record_id", 89], ["record_id", 91], ["record_id", 93], ["record_id", 94], ["record_id", 95]]
  ActiveStorage::Blob Load (0.4ms)        SELECT "active_storage_blobs".*       FROM "active_storage_blobs"       WHERE "active_storage_blobs"."id" IN ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12)  [["id", 1239], ["id", 1242], ["id", 1244], ["id", 1227], ["id", 1228], ["id", 1229], ["id", 1230], ["id", 1231], ["id", 1232], ["id", 1236], ["id", 1237], ["id", 1241]]

  # Samplepacks
  ActiveStorage::Attachment Load (0.3ms)  SELECT "active_storage_attachments".* FROM "active_storage_attachments" WHERE "active_storage_attachments"."record_type" = $1 AND "active_storage_attachments"."name" = $2 AND "active_storage_attachments"."record_id" = $3  [["record_type", "Samplepack"], ["name", "album_artwork"], ["record_id", 56]]
  ActiveStorage::Blob Load (0.2ms)        SELECT "active_storage_blobs".*       FROM "active_storage_blobs"       WHERE "active_storage_blobs"."id" = $1  [["id", 1243]]

【问题讨论】:

  • @resources.push(Album.with_attached_images.all ... 更好

标签: sql ruby-on-rails performance rails-activestorage


【解决方案1】:

根据您的情况(has_many/has_one)使用with_attached_<attachment>

为避免 N+1 查询,您可以在查询中包含附加的 blob 像这样:

Gallery.where(user: Current.user).with_attached_photos

所以:

[
  Album.all,
  Demo.with_attached_images.all,
  Samplepack.with_attached_images.all
]

我认为没有必要创建一个空数组来填充它。

可能还有一个问题,因为您使用的是all,请检查您对每个模型的真正需求。

【讨论】:

  • 感谢您的回答。你是对的,没有必要创建一个空数组。使用with_attached,我从 39 次查询到 18 次查询(由于变体,我相信我会找到一些相关信息)。再次感谢:)
  • 不错。您可以查看this 博客文章以进一步阅读。
猜你喜欢
  • 1970-01-01
  • 2017-06-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-22
  • 1970-01-01
  • 2022-06-10
相关资源
最近更新 更多