Whether you call it “Kris Kringle” or “Secret Santa“, there’s much pleasure in giving small gifts to people over the Christmas period. So rather than fully fledged blog posts, I’ll wrap up this year with a series of daily quick tips on the Oracle Database. Ho Ho Ho!
If you are loading a table, often we want to make the indexes unusable before loading the data to speed up the process. My target table has three indexes, so I’ll make them unusable before truncating and loading my data, and the rebuild them after.
SQL> select index_name, status
2 from user_indexes
3 where table_name = 'TARGET_TABLE';
INDEX_NAME STATUS
------------------------------ --------
TARGET_TABLE_IX1 VALID
TARGET_TABLE_IX2 VALID
TARGET_TABLE_IX3 VALID
SQL> alter index target_table_ix1 unusable;
Index altered.
SQL> alter index target_table_ix2 unusable;
Index altered.
SQL> alter index target_table_ix3 unusable;
Index altered.
SQL>
SQL> truncate table target_table;
Table truncated.
SQL>
SQL> insert into target_table
2 select * from source_data;
88343 rows created.
SQL> alter index target_table_ix1 rebuild;
Index altered.
SQL> alter index target_table_ix2 rebuild;
Index altered.
SQL> alter index target_table_ix3 rebuild;
Index altered.
Looks fine right? Unfortunately not. If you are using this method, remember that setting indexes to unusable must come after the truncate, because will that automatically make your indexes valid again.
SQL> alter index target_table_ix1 unusable;
Index altered.
SQL> alter index target_table_ix2 unusable;
Index altered.
SQL> alter index target_table_ix3 unusable;
Index altered.
SQL>
SQL> truncate table target_table;
Table truncated.
SQL>
SQL> select index_name, status
2 from user_indexes
3 where table_name = 'TARGET_TABLE';
INDEX_NAME STATUS
------------------------------ --------
TARGET_TABLE_IX1 VALID
TARGET_TABLE_IX2 VALID
TARGET_TABLE_IX3 VALID
Ho Ho Ho… Merry Christmas!
Yes, this is me, sacrificing my dignity to bring you Christmas tips 🙂




Got some thoughts? Leave a comment