简体   繁体   English

Oracle SQL - 检查 SQL 查询中缺少的索引

[英]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?是否可以使用程序或 SQL 脚本自动查找查询中是否缺少索引并显示它以创建它们?

Thanks!谢谢!

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 (eg due to incorrect stats or a query beyond the optimizer's ability to model it accurately). 在#3的情况下,总是有可能是错误的(例如,由于错误的统计信息或超出优化器准确建模能力的查询)。 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. 您可以查看dbms_sqltune以获得自动调整建议。 It'll generally tell you to gather stats, or sometimes suggest a SQL profile or a new index. 它通常会告诉您收集统计信息,或者有时会建议您使用SQL配置文件或新索引。 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 http://www.williamrobertson.net/documents/automated-sql-tuning-advice.html http://www.williamrobertson.net/documents/automated-sql-tuning-advice-sqlid.html

This is one good resource: https://www.dba-scripts.com/scripts/diagnostic-and-tuning/troubleshooting/find-missing-index/这是一个很好的资源: https : //www.dba-scripts.com/scripts/diagnostic-and-tuning/troubleshooting/find-missing-index/

ACCEPT SCHEMA_NAME PROMPT 'Choose the schema to analyze:'

select * from (
  select 'the column ' || c.name || ' of the table ' || us.name || '.' || o.name || ' was used ' || u.equality_preds || ' times in an equality predicate and ' || u.equijoin_preds || ' times in an equijoin predicate and is not indexed' as colum_to_index
  from sys.col_usage$ u,
       sys.obj$ o,
       sys.col$ c,
       sys.user$ us
  where u.obj# = o.obj#
  and   u.obj# = c.obj#
  and   us.user# = o.owner#
  and   u.intcol# = c.col#
  and   us.name='&SCHEMA_NAME'
  and   c.name not in (select column_name from dba_ind_columns where index_owner ='&SCHEMA_NAME')
  and   (u.equality_preds > 100 OR u.equijoin_preds > 100)
  order by u.equality_preds+u.equijoin_preds desc)
WHERE rownum <11;

A reliable method I have found for index suggestion (and many other suggestions for improvement) is the SQL Tuning Advisor in OEM (Oracle Enterprise Manager).我为索引建议(以及许多其他改进建议)找到的可靠方法是 OEM (Oracle Enterprise Manager) 中的 SQL Tuning Advisor。 It sometimes finds a better plan for your statement which it adds into a SQL Profile which you can check under dba_sql_profiles .它有时会为您的语句找到更好的计划,并将其添加到 SQL 配置文件中,您可以在dba_sql_profiles下检查该配置文件。

SQL Advisor will give you index suggestions in this format: SQL Advisor 将以这种格式为您提供索引建议:

Consider running the Access Advisor to improve the physical schema design or creating the recommended index.考虑运行 Access Advisor 以改进物理架构设计或创建推荐的索引。

 <schema.table>(field list)

Creating the recommended indices significantly improves the execution plan of this statement.创建推荐索引显着改善了该语句的执行计划。

You will have to run your SQL first and find it in OEM under Top Activity of your instance, or search for it by SQL ID.您必须先运行您的 SQL,然后在您实例的 Top Activity 下的 OEM 中找到它,或者通过 SQL ID 搜索它。

With an Oracle Autonomous Database you can enable Automatic indexing.使用 Oracle 自治数据库,您可以启用自动索引。 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'); EXEC DBMS_AUTO_INDEX.CONFIGURE('AUTO_INDEX_MODE','IMPLEMENT');

Disable Auto Indexing禁用自动索引

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

Refer to Auto Indexing in Oracle documentation for more details.有关更多详细信息,请参阅 Oracle 文档中的自动索引。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM