用wm_concat 產生索引引用的欄位

來源:互聯網
上載者:User

要到了要清理和整理資料庫的時期了,看到那一對對的表和索引,感覺就像一團麻一樣。沒多一段時間就需要清理下。

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.