oracle19c最值得關注十個新特性之自動化索引 ,通過機器學習算法自動創建管理索引,可以明顯減輕dba工作負擔。
Automatic indexing 提供如下功能
以指定時間間隔定期運行自動化索引特性。
• 分析應用程序工作負載,並相應地創建新索引並刪除現有性能不佳的索引以提高數 據庫性能。
• 重建由於表分區維護操作而標記為不可用的索引,例如ALTER TABLE MOVE。
• 提供了PL/SQL API接口,用於在數據庫中配置自動索引並生成與自動索引操作相 關的報告。
注:
1. 當前版本Auto indexes 是 Local B-tree 索引
2. 支持分區和非分區表(Only Local Index),不支持臨時表
3. 自動創建只針對 = 的查詢,不支持 范圍、模糊、Min/Max等查詢
Automatic indexing支持
• 對於On-Prem環境僅支持 Oracle Exadata 平台
• Oracle Cloud 支持 ExaCS, ADB
自動索引演示
啟用exadata特性
[oracle19@GF-qsht1 ~]$ sqlplus / as sysdba SQL*Plus: Release 19.0.0.0.0 - Production on Tue Jun 8 13:17:39 2021 Version 19.3.0.0.0 Copyright (c) 1982, 2019, Oracle. All rights reserved. Connected to: Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production Version 19.3.0.0.0 SQL> alter system set "_exadata_feature_on"=true scope=spfile; System altered. SQL> startup force ORACLE instance started. Total System Global Area 5016386992 bytes Fixed Size 9145776 bytes Variable Size 939524096 bytes Database Buffers 4060086272 bytes Redo Buffers 7630848 bytes Database mounted. Database opened.
pdb下查詢自動索引是否開啟 :AUTO_INDEX_MODE OFF代表未開
SQL> show pdbs CON_ID CON_NAME OPEN MODE RESTRICTED ---------- ------------------------------ ---------- ---------- 3 PDB READ WRITE NO SQL> SQL> SQL> SQL> COLUMN parameter_name FORMAT A40 COLUMN parameter_value FORMAT A20 SELECT con_id, parameter_name, parameter_value FROM cdb_auto_index_config;SQL> SQL> CON_ID PARAMETER_NAME PARAMETER_VALUE ---------- ---------------------------------------- -------------------- 3 AUTO_INDEX_COMPRESSION OFF 3 AUTO_INDEX_DEFAULT_TABLESPACE 3 AUTO_INDEX_MODE OFF 3 AUTO_INDEX_REPORT_RETENTION 31 3 AUTO_INDEX_RETENTION_FOR_AUTO 373 3 AUTO_INDEX_RETENTION_FOR_MANUAL 3 AUTO_INDEX_SCHEMA 3 AUTO_INDEX_SPACE_BUDGET 50 8 rows selected.
啟用自動化索引
啟用: EXEC DBMS_AUTO_INDEX.CONFIGURE('AUTO_INDEX_MODE','IMPLEMENT');
啟用,但是只作為不可見自動索引,無法被SQL所使用:EXEC DBMS_AUTO_INDEX.CONFIGURE('AUTO_INDEX_MODE','REPORT ONLY');
SQL> EXEC DBMS_AUTO_INDEX.CONFIGURE('AUTO_INDEX_MODE','IMPLEMENT'); PL/SQL procedure successfully completed.
也可以指定某個schema啟用自動索引,我們這里指定test用戶 看到參數 AUTO_INDEX_SCHEMA schema IN (TEST)
SQL> EXEC DBMS_AUTO_INDEX.CONFIGURE('AUTO_INDEX_SCHEMA', 'TEST', allow => TRUE); PL/SQL procedure successfully completed. SQL> COLUMN parameter_name FORMAT A40 COLUMN parameter_value FORMAT A20 SELECT con_id, parameter_name, parameter_value FROM cdb_auto_index_config;SQL> SQL> CON_ID PARAMETER_NAME PARAMETER_VALUE ---------- ---------------------------------------- -------------------- 3 AUTO_INDEX_COMPRESSION OFF 3 AUTO_INDEX_DEFAULT_TABLESPACE TS001 3 AUTO_INDEX_MODE IMPLEMENT 3 AUTO_INDEX_REPORT_RETENTION 31 3 AUTO_INDEX_RETENTION_FOR_AUTO 373 3 AUTO_INDEX_RETENTION_FOR_MANUAL 3 AUTO_INDEX_SCHEMA 3 AUTO_INDEX_SPACE_BUDGET 50 1 AUTO_INDEX_COMPRESSION OFF 1 AUTO_INDEX_DEFAULT_TABLESPACE 1 AUTO_INDEX_MODE OFF CON_ID PARAMETER_NAME PARAMETER_VALUE ---------- ---------------------------------------- -------------------- 1 AUTO_INDEX_REPORT_RETENTION 31 1 AUTO_INDEX_RETENTION_FOR_AUTO 373 1 AUTO_INDEX_RETENTION_FOR_MANUAL 1 AUTO_INDEX_SCHEMA schema IN (TEST) 1 AUTO_INDEX_SPACE_BUDGET 50 16 rows selected.
創建一個表空間存放自動索引
SQL> create tablespace ts001 datafile '/oracle/app/oracle/oradata/ORCL19C/pdb/ts001.dbf' size 500m; Tablespace created. SQL> EXEC DBMS_AUTO_INDEX.CONFIGURE('AUTO_INDEX_DEFAULT_TABLESPACE','ts001'); PL/SQL procedure successfully completed. SQL>
創建一張測試表test1 並查詢1萬次
SQL> create table test.test1 as select rownum id,t.* from dba_objects t; Table created. SQL> declare a varchar2(2000) := ''; begin for x in 1.. 10000 loop select object_name into a from test.test1 where id=x; end loop; end; / PL/SQL procedure successfully completed.
自動索引的周期默認是15分鍾,15分鍾之后我們查看是否創建成功
SQL> select parameter_name,PARAMETER_VALUE from SYS.SMB$CONFIG where parameter_name like '%AUTO_INDEX_TASK_INTERVAL%'; PARAMETER_NAME PARAMETER_VALUE ------------------------------ --------------- _AUTO_INDEX_TASK_INTERVAL 900
已經創建成功 SYS_AI開頭的索引是oracle自動創建的
SQL> COLUMN OWNER FORMAT a10 col INDEX_TYPE format a10 col INDEX_NAME format a20 col TABLE_NAME format a20 col TABLE_OWNER format a10 SELECT OWNER,INDEX_TYPE,INDEX_NAME,TABLE_NAME,TABLE_OWNER FROM DBA_INDEXES WHERE AUTO='YES' ORDER BY OWNER,INDEX_NAME;SQL> SQL> SQL> SQL> SQL> OWNER INDEX_TYPE INDEX_NAME TABLE_NAME TABLE_OWNE ---------- ---------- -------------------- -------------------- ---------- TEST NORMAL SYS_AI_1p2a9sk44mhwy TEST1 TEST TEST NORMAL SYS_AI_5msfmx7p4gkzr TEST1 TEST
執行計划已經走了索引
SQL> set pages 1000 set lines 1000 set autotrace on select object_name from test.test1 where object_id=1000;SQL> SQL> SQL> OBJECT_NAME -------------------------------------------------------------------------------------------------------------------------------- APPLY$_BATCH_SQL_STATS_I Execution Plan ---------------------------------------------------------- Plan hash value: 3029300800 ------------------------------------------------------------------------------------------------------------ | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | ------------------------------------------------------------------------------------------------------------ | 0 | SELECT STATEMENT | | 1 | 40 | 2 (0)| 00:00:01 | | 1 | TABLE ACCESS BY INDEX ROWID BATCHED| TEST1 | 1 | 40 | 2 (0)| 00:00:01 | |* 2 | INDEX RANGE SCAN | SYS_AI_1p2a9sk44mhwy | 1 | | 1 (0)| 00:00:01 | ------------------------------------------------------------------------------------------------------------ Predicate Information (identified by operation id): --------------------------------------------------- 2 - access("OBJECT_ID"=1000) Statistics ---------------------------------------------------------- 0 recursive calls 0 db block gets 4 consistent gets 0 physical reads 0 redo size 575 bytes sent via SQL*Net to client 638 bytes received via SQL*Net from client 2 SQL*Net roundtrips to/from client 0 sorts (memory) 0 sorts (disk) 1 rows processed
