我使用Union编写此查询以从3个不同的表中获取结果。 Explain
表示它执行2次全表扫描。如何优化此查询以使其不执行全表扫描?这是Explain See Explain image here的直观表示。 Explain Results part 1 Explain Results part 2
(SELECT f.name, f.description, f.location, f.copyright, f.location, f.orientation, f.cws_asset_key, pst.title, f.related_media_key, f.media_type, f.is_visible_at_press, f.created_at, 'content' AS 'type'
FROM ContentRelatedMedia f
INNER JOIN Content c ON f.content_id = c.id
INNER JOIN ParentSeries ps ON c.parent_series_id = ps.id
INNER JOIN ParentSeriesTitle pst ON pst.parent_series_id = ps.id
WHERE ps.is_visible_at_press = true
AND f.is_visible_at_press = true
AND ps.company_id = :company
AND f.is_archived = false
AND f.media_type = 'image'
AND (f.name LIKE '%' :name '%'
OR f.description LIKE '%' :description '%'
OR pst.title LIKE '%' :title '%')
)
UNION
(SELECT ff.name, ff.description, ff.location, ff.copyright, ff.location, ff.orientation, ff.cws_asset_key, pst.title, ff.related_media_key, ff.media_type, ff.is_visible_at_press, ff.created_at, 'season' AS 'type'
FROM SeasonRelatedMedia ff
INNER JOIN Season s ON ff.season_id = s.id
INNER JOIN ParentSeries ps ON s.parent_series_id = ps.id
INNER JOIN ParentSeriesTitle pst ON pst.parent_series_id = ps.id
WHERE ps.is_visible_at_press = true
AND ff.is_visible_at_press = true
AND ps.company_id = :company
AND ff.is_archived = false
AND ff.media_type = 'image'
AND (ff.name LIKE '%' :name '%'
OR ff.description LIKE '%' :description '%'
OR pst.title LIKE '%' :title '%')
)
UNION
(SELECT fff.name, fff.description, fff.location, fff.copyright, fff.location, fff.orientation, fff.cws_asset_key, pst.title, fff.related_media_key, fff.media_type, fff.is_visible_at_press[![enter image description here][1]][1], fff.created_at, 'parent_series' AS 'type'
FROM ParentSeriesRelatedMedia fff
INNER JOIN ParentSeries ps ON fff.parent_series_id = ps.id
INNER JOIN ParentSeriesTitle pst ON pst.parent_series_id = ps.id
WHERE ps.is_visible_at_press = true
AND fff.is_visible_at_press = true
AND ps.company_id = :company
AND fff.is_archived = false
AND fff.media_type = 'image'
AND (fff.name LIKE '%' :name '%'
OR fff.description LIKE '%' :description '%'
OR pst.title LIKE '%' :title '%')
)
由于