Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Oracle SQL - check missing indexes on SQL query

I need to check if in a query, index or indexes are missing.

For check indexes in a table I use syntax:

SELECT index_name
     , column_position
     , column_name
  FROM user_ind_columns
 WHERE table_name = 'table_name'
 ORDER BY index_name, COLUMN_POSITION;

 SELECT index_name 
 FROM user_indexes
 WHERE table_name = 'table_name';

It is possible to use a program or SQL script to find automatically if is missing indexes on a query and show it for creation them?

Thanks!

like image 861
Catalin Avatar asked Dec 24 '22 14:12

Catalin


2 Answers

Not really. Checking the execution plan will tell you which indexes, if any, the optimizer will use; but if the plan shows no indexes it might be that

  1. There are no indexes.
  2. There are indexes but they cannot be used for the particular query.
  3. There are indexes that can technically be used, but the optimizer calculates that they will not improve the performance.

In the case of #3, it is always possible that it is wrong (e.g. due to incorrect stats or a query beyond the optimizer's ability to model it accurately). If this is the situation you are looking for then there is no simple way to detect it.

You might look at dbms_sqltune for automated tuning suggestions. It'll generally tell you to gather stats, or sometimes suggest a SQL profile or a new index. You can call it from Enterprise Manager or from a script:

http://www.williamrobertson.net/documents/automated-sql-tuning-advice.html http://www.williamrobertson.net/documents/automated-sql-tuning-advice-sqlid.html

like image 120
William Robertson Avatar answered Dec 29 '22 14:12

William Robertson


With an Oracle Autonomous Database you can enable Automatic indexing. Auto Indexing automates the index management tasks and eliminates the need to knowledge and expertise in creating, maintaining, and retiring/not-using indexes appropriately based on your workload patterns.

Enable Auto Indexing

EXEC DBMS_AUTO_INDEX.CONFIGURE('AUTO_INDEX_MODE','IMPLEMENT');

Disable Auto Indexing

EXEC DBMS_AUTO_INDEX.CONFIGURE('AUTO_INDEX_MODE','OFF');

Refer to Auto Indexing in Oracle documentation for more details.

like image 24
RaviSharma-Oracle Avatar answered Dec 29 '22 14:12

RaviSharma-Oracle