【发布时间】:2023-03-09 14:11:02
【问题描述】:
我有一个显示 3 种不同资源(Ablum、Samplepack 和 Demo)的索引页面,其中 2 种带有附加图像。我正在通过我的控制器加载它们,如下所示:
# static_controller.rb
@resources = []
@resources.push(Album.all, Demo.all, Samplepack.all)
它会产生 很多 的 SQL 查询以加载它们。我想知道是否有最有效的加载附件的方法?如果有人可以复制/粘贴我一些阅读的链接,那就太棒了!
我在考虑includes 或joins 的一些东西,但我在网上找不到任何参考。
祝你有美好的一天
编辑:正如 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