簡體   English   中英

如何將USER_TABLES及其在USER_OBJECTS中的相應索引加入?

[英]How to join USER_TABLES with its corresponding indexes in USER_OBJECTS?

我有for循環來重建已更改主鍵的某些表的索引。 總之,從選擇這些表只索引USER_OBJECTS與鏈接TABLE_NAMEUSER_TABLES ,也排除任何IOT表的索引。

 FOR r IN (SELECT OBJECT_NAME AS OBJ FORM USER_OBJECTS WHERE OBJECT_TYPE = 'INDEX') LOOP
        l_sql := 'ALTER INDEX '||r.obj||' REBUILD'||'';
        EXECUTE IMMEDIATE l_sql;
 END LOOP; 

上面的代碼只是簡單地重建架構中的所有索引(包括IOT,因此會出現錯誤ORA-28650: Primary index on an IOT cannot be rebuilt

我根本不會使用user_objects 為什么不從user_indexes ,並將其加入到user_tables呢?

select ui.index_name from user_indexes ui
join user_tables ut on ut.table_name = ui.table_name
where ut.iot_type is null

因此,您的循環變為:

FOR r IN (
    select ui.index_name from user_indexes ui
    join user_tables ut on ut.table_name = ui.table_name
    where ut.iot_type is null
)
LOOP
    l_sql := 'ALTER INDEX "'||r.index_name||'" REBUILD';
    EXECUTE IMMEDIATE l_sql;
END LOOP; 

您實際上並不需要l_sql ,但是它對於調試很有用。

當然,您需要質疑為什么首先要重建所有索引...

select * from user_tables where iot_type is not null;

將返回索引組織的表。

正如我所說,在使用索引時 ,請考慮查詢USER_INDEXES

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM