当试图理解SQL语句的执行方式时,有时建议查看执行计划。解释(理解)执行计划的过程应该如何进行?哪些因素应该引起我们注意,并认为"噢,这很棒!",哪些因素则表明出现了问题,让我们感到"不对劲"呢?
当试图理解SQL语句的执行方式时,有时建议查看执行计划。解释(理解)执行计划的过程应该如何进行?哪些因素应该引起我们注意,并认为"噢,这很棒!",哪些因素则表明出现了问题,让我们感到"不对劲"呢?
这个主题太大了,无法在这样的问题中回答。您应该花些时间阅读Oracle性能调整指南。
INDEX http://docs.google.com/a/fukuoka-now.com/File?id=dd8xj6nh_9fhsqvxcp_b
正如已经建议的那样,请注意表扫描。通常可以避免这些情况。
“Oh no,那不对”通常是由表扫描引起的。表扫描不利用任何特殊索引,可能导致内存缓存中的所有有用信息被清除。例如,在PostgreSQL中,你会发现它长成这样。
Seq Scan on my_table (cost=0.00..15558.92 rows=620092 width=78)
解释的输出会告诉您每个步骤所花费的时间。首先要找出花费时间较长的步骤并了解它们的含义。像顺序扫描这样的东西告诉您需要更好的索引-这主要是对您特定数据库的研究和经验的问题。
基本上,您需要查看每个操作并确定它们是否“合理”,根据您对其应该如何工作的了解。
例如,如果您正在将两个表A和B连接在它们各自的列C和D(A.C = B.D)上,并且您的计划显示表A上的聚集索引扫描(SQL Server术语-不确定Oracle术语),然后是一个嵌套循环连接到一系列表B上的聚集索引搜索,您可能会认为存在问题。在这种情况下,您可能希望引擎执行一对索引扫描(在连接列上的索引上)后跟随一个合并连接。进一步调查可能会发现糟糕的统计数据使优化器选择了该连接模式,或者是一个实际上不存在的索引。
我主要寻找索引或表扫描。这通常告诉我,在where语句或join语句中缺少一个重要列的索引。
来自http://www.sql-server-performance.com/tips/query_execution_plan_analysis_p1.aspx:
If you see any of the following in an execution plan, you should consider them warning signs and investigate them for potential performance problems. Each of them are less than ideal from a performance perspective.
* Index or table scans: May indicate a need for better or additional indexes. * Bookmark Lookups: Consider changing the current clustered index, consider using a covering index, limit the number of columns in the SELECT statement. * Filter: Remove any functions in the WHERE clause, don't include wiews in your Transact-SQL code, may need additional indexes. * Sort: Does the data really need to be sorted? Can an index be used to avoid sorting? Can sorting be done at the client more efficiently?
It is not always possible to avoid these, but the more you can avoid them, the faster query performance will be.
查看计划中每个子段所花费的时间百分比,并考虑引擎正在执行什么操作。例如,如果正在扫描表格,请考虑在正在扫描的字段上放置索引。