【问题标题】:How to join USER_TABLES with its corresponding indexes in USER_OBJECTS?如何将 USER_TABLES 与其在 USER_OBJECTS 中的相应索引连接起来?
【发布时间】:2018-10-03 09:56:32
【问题描述】:

我有 for in 循环来重建某些已更改主键的表的索引。无论如何从USER_OBJECTS 中选择这些表的索引与USER_TABLES 中的TABLE_NAME 链接,也排除任何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

【问题讨论】:

    标签: oracle indexing rebuild user-object


    【解决方案1】:

    我根本不会使用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,但它可能对调试有用。

    当然,您首先需要质疑为什么要重建所有索引...

    【讨论】:

      【解决方案2】:
      select * from user_tables where iot_type is not null;
      

      将返回按索引组织的表。

      正如我所说,使用索引时,请考虑查询USER_INDEXES

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-06-06
        • 2021-12-06
        • 2018-12-02
        • 2019-07-17
        • 1970-01-01
        • 1970-01-01
        • 2021-02-07
        相关资源
        最近更新 更多