0

我需要改进我的查询,它有超过 8 个连接并消耗大量的总表空间。

以下是查询:

select r.id, uc.contributor_full_name,s.code,
       d.text, ucs.moderation_status, v.url
  from review r, user_contribution uc, user_contribution_status ucs,
       video v, description d, video_description vd, location_video lv,
       accommodation_video av, system s
 where r.user_contribution_id = ucs.user_contribution_id and
       uc.id = ucs.user_contribution_id and
       uc.system_id = s.id and
       r.accommodation_id = av.accommodation_id or
       r.location_id = lv.location_id and
       av.video_id = v.id and
       lv.video_id = v.id and
       v.id = vd.video_id and
       vd.description_id = d.id;

有没有更好的方法来编写这个查询?

4

1 回答 1

0

With a query like this it's possible Oracle will default to a whole lot of hash joins and full table scans, which may or may not be a good idea.

+1 to post the explain plan. Until then, don't upvote this answer!

I believe your query is equivalent to this, and it's possible that when you look at the explain plan that you'll see Oracle will convert it to something like this anyway:

select r.id
      ,uc.contributor_full_name,s.code
      ,d.text
      ,ucs.moderation_status
      ,v.url
from   review r
join   user_contribution_status ucs on r.user_contribution_id = ucs.user_contribution_id
join   user_contribution uc on uc.id = ucs.user_contribution_id
join   system s on uc.system_id = s.id
join   accommodation_video av on r.accommodation_id = av.accommodation_id
join   video_description vd on v.id = vd.video_id
join   description d on vd.description_id = d.id
join   video v on av.video_id = v.id
union all
select r.id
      ,uc.contributor_full_name,s.code
      ,d.text
      ,ucs.moderation_status
      ,v.url
from   review r
join   user_contribution_status ucs on r.user_contribution_id = ucs.user_contribution_id
join   user_contribution uc on uc.id = ucs.user_contribution_id
join   system s on uc.system_id = s.id
join   location_video lv on r.location_id = lv.location_id 
join   video_description vd on v.id = vd.video_id
join   description d on vd.description_id = d.id
join   video v on lv.video_id = v.id;

Warning: I may have made some assumptions about constraints (e.g. PK, FK and not null) on some of the ID columns, esp. with regards to accommodation_video and location_video. I removed them from the subclauses with the assumption that they weren't necessary - but this assumption may be wrong.

于 2013-08-08T08:35:14.937 回答