[英]Sort key too long error in oracle sql 9i
我正在使用Oracle 9i SQL數據庫。我想編寫這樣的查詢
select
id_process_inst,<br>
max(case when id_data=34756 then value_text end),<br>
max(case when id_data=34760 then value_text end),<br>
max(case when id_data=34793 then value_text end),<br>
max(case when id_data=34792 then value_text end),<br>
max(case when id_data=34790 then value_text end),<br>
max(case when id_data=34761 then value_text end),<br>
max(case when id_data=34791 then value_text end),<br>
max(case when id_data=34766 then value_text end),<br>
max(case when id_data=34778 then value_text end),<br>
max(case when id_data=34767 then value_text end),<br>
m....<br>
from<br>
(<br>
select <br>
procreldata.id_inst,<br>
dataset.value_text,<br>
procreldata.id_data<br>
from wfdata procreldata<br>
inner join wfvalue dataset<br>
on dataset.id_data= procreldata.id_data<br>
where procreldata.id_inst=177262<br>
)<br>
group by id_inst;<br>
每當我嘗試執行此查詢時,都會收到“ Sort key too long”錯誤。我想這是因為我在此查詢中使用了大約32個max函數。
請幫助我解決這個問題。
先感謝您。
我鏈接到的Ask Tom文章暗示這是數據值以及列數的問題,此處顯示的示例似乎確實是這樣。 但是在這種情況下,顯然只是列數。
這是一個真正的技巧,但似乎可行。 在這里, t42
有效地表示實際表之間的t42
結果。 如果我創建該虛擬表並添加幾行數據最少的數據:
create table t42 (inst_id number, id_data number, value_text varchar2(4000));
insert into t42 values (1, 1, 'w');
insert into t42 values (1, 2, 'x');
insert into t42 values (1, 3, 'y');
insert into t42 values (1, 4, 'z');
然后這仍然失敗:
select max(case when id_data = 1 then value_text end),
max(case when id_data = 2 then value_text end),
max(case when id_data = 3 then value_text end),
max(case when id_data = 4 then value_text end),
max(case when id_data = 5 then value_text end),
max(case when id_data = 6 then value_text end),
max(case when id_data = 7 then value_text end),
max(case when id_data = 8 then value_text end),
max(case when id_data = 9 then value_text end),
max(case when id_data = 10 then value_text end),
max(case when id_data = 11 then value_text end),
max(case when id_data = 12 then value_text end),
max(case when id_data = 13 then value_text end),
max(case when id_data = 14 then value_text end),
max(case when id_data = 15 then value_text end),
max(case when id_data = 16 then value_text end)
from t42
group by inst_id;
from t42
*
ERROR at line 17:
ORA-01467: sort key too long
我只需要16個聚合表達式,我想這仍然與我的塊大小有關,在我僅有的9i數據庫中,塊大小為2k。 如果您需要32列來獲取錯誤,那么我猜您的塊大小為4k。 除非您可以增加它,否則不確定是否真正重要。
經過一些試驗,這是我發現可行的第一個替代方法:
with t as (
select inst_id, id_data,
case when id_data = 1 then value_text end as x1,
case when id_data = 2 then value_text end as x2,
case when id_data = 3 then value_text end as x3,
case when id_data = 4 then value_text end as x4,
case when id_data = 5 then value_text end as x5,
case when id_data = 6 then value_text end as x6,
case when id_data = 7 then value_text end as x7,
case when id_data = 8 then value_text end as x8,
case when id_data = 9 then value_text end as x9,
case when id_data = 10 then value_text end as x10,
case when id_data = 11 then value_text end as x11,
case when id_data = 12 then value_text end as x12,
case when id_data = 13 then value_text end as x13,
case when id_data = 14 then value_text end as x14,
case when id_data = 15 then value_text end as x15,
case when id_data = 16 then value_text end as x16
from t42
)
select (select max(x1) from t t2 where t2.inst_id = t.inst_id),
(select max(x2) from t t2 where t2.inst_id = t.inst_id),
(select max(x3) from t t2 where t2.inst_id = t.inst_id),
(select max(x4) from t t2 where t2.inst_id = t.inst_id),
(select max(x5) from t t2 where t2.inst_id = t.inst_id),
(select max(x6) from t t2 where t2.inst_id = t.inst_id),
(select max(x7) from t t2 where t2.inst_id = t.inst_id),
(select max(x8) from t t2 where t2.inst_id = t.inst_id),
(select max(x9) from t t2 where t2.inst_id = t.inst_id),
(select max(x10) from t t2 where t2.inst_id = t.inst_id),
(select max(x11) from t t2 where t2.inst_id = t.inst_id),
(select max(x12) from t t2 where t2.inst_id = t.inst_id),
(select max(x13) from t t2 where t2.inst_id = t.inst_id),
(select max(x14) from t t2 where t2.inst_id = t.inst_id),
(select max(x15) from t t2 where t2.inst_id = t.inst_id),
(select max(x16) from t t2 where t2.inst_id = t.inst_id)
from t
group by inst_id;
(SEL (SEL (SEL (SEL (SEL (SEL (SEL (SEL (SEL (SEL (SEL (SEL (SEL (SEL (SEL (SEL
---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
w x y z
所有的自聯接都很痛苦,但是至少它們在您的主查詢的結果集中,因此您仍然只擊中實際表一次-其余的僅需要足夠的內存來管理所有帶回的數據。
由於現在在主查詢中沒有發生任何聚合,因此僅在子查詢中,盡管效果是相同的,但使用distinct
而不是group by
可能更清楚:
...
select distinct inst_id,
(select max(x1) from t t2 where t2.inst_id = t.inst_id),
...
(select max(x16) from t t2 where t2.inst_id = t.inst_id)
from t;
這只是一個示范。 在我使用t42
作為假數據占位符的地方,放回原始查詢,並根據需要添加t42
聚合; 因此在with
子句中:
with t as (
select inst_id, id_data,
case when id_data = 34756 then value_text end as x1,
...
case when id_data = 34999 then value_text end as x32
from (
select procreldata.id_inst,
dataset.value_text,
procreldata.id_data
from wfdata procreldata
inner join wfvalue dataset
on dataset.id_data= procreldata.id_data
where procreldata.id_inst=177262
)
)
select distinct inst_id,
(select max(x1) from t t2 where t2.inst_id = t.inst_id),
...
(select max(x32) from t t2 where t2.inst_id = t.inst_id)
from t;
當然,我並不是說它很漂亮...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.