我需要開發一個搜索引擎。所有資料都保存在包含大約 300GB 資料的 Oracle 資料庫中。我需要加入多個表來收集我需要的所有資訊并從那里搜索。當我將 4 個表連接在一起時,它在沒有索引的情況下變得非常慢(總是超時)。這是我的查詢示例:
select distinct t1.a, t1.b, t2.c , t1.d, t3.e, t5.f, t1.g, t4.k
from table1 t1
inner join table2 t2 on t2.a = t1.a
inner join table3 t3 on t3.l = t2.l
left outer join table4 t4 on t4.d = t1.d
left outer join table5 t5 on t5.a = t1.a
where t1.a > 0 and t1.b < 2 and t2.c > 3 and t1.d > 4
and t3.e > 5 and t5.f > 6;
我知道有一種叫做索引的東西可以加快運行時間。問題是如何制作索引?
我應該為每個表中的每個連接鍵創建索引嗎?
同樣對于 where 陳述句,我如何制作交叉表索引?例如,a 列和 c 列來自不同的表,我如何創建索引以同時擁有它們?
另外,有沒有辦法讓選擇更快?我嘗試了按解決方案分組,但并沒有太大幫助。示例:查詢將花費 0.07 秒:
select distinct t1.a, t1.b, t2.c , t1.d, t1.g
from table1 t1
inner join table2 t2 on t2.a = t1.a
where rownum < 100;
但不同的是它會超時。
uj5u.com熱心網友回復:
您肯定希望在要連接的表中用于連接條件的所有列的索引,但您應該在鍵(使用的地方)之后添加范圍條件:
create index table2_ac on table2(a, c);
create index table3_le on table3(l, e);
create index table4_d on table4(d);
create index table5_af on table5(a, f);
您不能制作“交叉表”索引。
where在子句中使用的列上創建索引可能會有所幫助:
create index table1_abd on table1(a, b, d);
最后,將連接表上的條件從where子句移到連接條件中:
select distinct t1.a, t1.b, t2.c , t1.d, t3.e, t5.f, t1.g, t4.k
from table1 t1
inner join table2 t2 on t2.a = t1.a and t2.c > 3
inner join table3 t3 on t3.l = t2.l and t3.e > 5
left outer join table4 t4 on t4.d = t1.d
left outer join table5 t5 on t5.a = t1.a and t5.f > 6
where t1.a > 0 and t1.b < 2 and t1.d > 4
這還解決了查詢的細微問題,即通過將條件放在where子句中的左連接表上,將連接型別從 external更改為inner。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/483337.html
