スクリプト #1 を使用して作成された tparent と tchild の 2 つのテーブルがあります。uname が「sky」に等しい 2 つのテーブルからすべての情報を照会する必要があります。次のクエリ ステートメント #2 は、レコード数が少ない場合に高速です。ただし、次のスクリプト #3 を使用して膨大な数のレコードを挿入すると、非常に遅くなります。テーブル間のOR条件が原因で、インデックスがクエリに役に立たないと思います。したがって、ステートメントを 3 つのサブステートメントに変更し、#4 のように結果を結合するという迅速な解決策が得られました。知りたいのですが、より良い解決策はありますか?それは何です?
ありがとう!
#1
drop table tparent;
drop table tchild;
create table tparent(typeid int,sno number,uname varchar2(50) );
create table tchild(typeid int,sno number,seqno int,uname varchar2(50));
create unique index uidx_tparent_typeidsno on tparent(typeid,sno);
create unique index uidx_tchild_typeidsnoseqno on tchild(typeid,sno,seqno);
create index idx_tparent_name on tparent(uname);
create index idx_tchild_name on tchild(uname);
insert into tparent values (1,10,'lily');
insert into tparent values (1,11,'eric');
insert into tparent values (2,10,'tom');
insert into tparent values (2,11,'eric');
insert into tparent values (3,10,'sky');
insert into tchild values (1,10,1,'sam');
insert into tchild values (1,10,2,'sky');
insert into tchild values (1,11,1,'eric');
insert into tchild values (1,11,2,'john');
insert into tchild values (2,10,1,'sky');
insert into tchild values (2,11,1,'eric');
insert into tchild values (3,10,1,'tony');
#2
select p.typeid,p.sno,p.uname,c1.uname as uname1,c2.uname as uname2 from tparent p
left join tchild c1 on c1.typeid=p.typeid and c1.sno = p.sno and c1.seqno=1
left join tchild c2 on c2.typeid=p.typeid and c2.sno = p.sno and c2.seqno=2
where (p.uname='sky' or c1.uname='sky' or c2.uname='sky');
#3
BEGIN
FOR x IN 1..10
LOOP
BEGIN
FOR y IN 10000..100000
LOOP
BEGIN
insert into tparent values (x,y,'name'|| y);
insert into tchild values (x,y,1,'name'|| y);
insert into tchild values (x,y,2,'name'|| y);
END;
END LOOP;
COMMIT;
END;
END LOOP;
END;
#4
select typeid,sno,max(uname),max(uname1),max(uname2) from (
select p.typeid,p.sno,p.uname,c1.uname as uname1,c2.uname as uname2 from tparent p
left join tchild c1 on c1.typeid=p.typeid and c1.sno = p.sno and c1.seqno=1
left join tchild c2 on c2.typeid=p.typeid and c2.sno = p.sno and c2.seqno=2
where (p.uname='sky' )
union
select p.typeid,p.sno,p.uname,c1.uname as uname1,c2.uname as uname2 from tparent p
left join tchild c1 on c1.typeid=p.typeid and c1.sno = p.sno and c1.seqno=1
left join tchild c2 on c2.typeid=p.typeid and c2.sno = p.sno and c2.seqno=2
where ( c1.uname='sky' )
union
select p.typeid,p.sno,p.uname,c1.uname as uname1,c2.uname as uname2 from tparent p
left join tchild c1 on c1.typeid=p.typeid and c1.sno = p.sno and c1.seqno=1
left join tchild c2 on c2.typeid=p.typeid and c2.sno = p.sno and c2.seqno=2
where ( c2.uname='sky')
) tb group by typeid,sno
order by typeid,sno
;