media assets: optimize /media_assets?search[is_posted] query.

Followup to 093a808a3. Using a NOT EXISTS clause is much faster than the
`LEFT OUTER JOIN posts WHERE posts.id IS NULL` clause generated by
`.where.missing(:post)`.
This commit is contained in:
evazion
2022-02-18 04:24:33 -06:00
parent 77515915a4
commit fc5aec7de0

View File

@@ -189,9 +189,11 @@ class MediaAsset < ApplicationRecord
end
if params[:is_posted].to_s.truthy?
q = q.where.associated(:post)
#q = q.where.associated(:post)
q = q.where(Post.where("posts.md5 = media_assets.md5").arel.exists)
elsif params[:is_posted].to_s.falsy?
q = q.where.missing(:post)
#q = q.where.missing(:post)
q = q.where.not(Post.where("posts.md5 = media_assets.md5").arel.exists)
end
q.apply_default_order(params)