要到了要清理和整理資料庫的時期了,看到那一對對的表和索引,感覺就像一團麻一樣。沒多一段時間就需要清理下。
1. 這段時間又增加了多少個表。
2.這段時間有修改了多少個表。
3.還有索引,又建了多少個索引。命名是否有規則。
因為目前的資料庫是資料倉儲,所以在索引方面希望能名稱標準化,而且能批量檢查和添加。知道oracle有wm_concat函數,於是google了下,有了下面的方案。
with sql_tmp as
(select INDEX_NAME,
wm_concat(COLUMN_POSITION) over(partition by INDEX_NAME order by COLUMN_POSITION) COLUMN_POSITION,
wm_concat(COLUMN_NAME) over(partition by INDEX_NAME order by COLUMN_NAME) COLUMN_NAME
from user_ind_columns)
select INDEX_NAME, max(COLUMN_POSITION), max(COLUMN_NAME)
from sql_tmp
group by INDEX_NAME
;
參考如下:
wm_concat在行轉列的時候非常有用,但在行轉列的過程中的排序問題常常難以控制。
可見下面例子:
準備測試表:
drop table t;
create table t (n number,m number);
insert into t values(1,1);
insert into t values(5,3);
insert into t values(3,3);
insert into t values(6,5);
insert into t values(7,2);
insert into t values(2,2);
insert into t values(0,1);
insert into t values(11,1);
insert into t values(15,3);
insert into t values(13,3);
insert into t values(16,5);
insert into t values(17,2);
insert into t values(12,2);
insert into t values(10,1);
commit;
SQL> select * from t order by 2,1;
N M
———- ———-
0 1
1 1
10 1
11 1
2 2
7 2
12 2
17 2
3 3
5 3
13 3
15 3
6 5
16 5
測試wm_concat後的順序:
測試1:
SQL> select m,wm_concat(n) from t group by m;
M WM_CONCAT(N)
———- ——————————————————————————–
1 11,0,1,10
2 17,2,7,12
3 15,3,5,13
5 16,6
可見wm_concat後的順序並沒有按大->小,或小->大的順序。
測試2:
–參考網上一些解決思路
SQL> select m,wm_concat(n)
2 from (select n,m from t order by m,n )
3 group by m;
M WM_CONCAT(N)
———- ——————————————————————————–
1 0,11,10,1
2 2,17,12,7
3 3,15,13,5
5 6,16
可見順序問題還是沒有解決
最終解決思路:
SQL> select m, max(r)
2 from (select m, wm_concat(n) over (partition by m order by n) r from t)
3 group by m ;
M MAX(R)
———- ——————————————————————————–
1 0,1,10,11
2 2,7,12,17
3 3,5,13,15
5 6,16