<p>When attempting to understand how a SQL statement is executing, it is sometimes recommended to look at the explain plan. What is the process one should go through in interpreting (making sense) of an explain plan? What should stand out as, "Oh, this is working splendidly?" versus "Oh no, that's not right."</p>

[{'answer_id': 79294, 'author': 'Steven A. Lowe', 'author_id': 9345, 'author_profile': 'https://Stackoverflow.com/users/9345', 'pm_score': 1, 'selected': False, 'text': '<p>look at the percentage of time spent in each subsection of the plan, and consider what the engine is doing. for example, if it is scanning a table, consider putting an index on the field(s) that is is scanning for</p>\n'}, {'answer_id': 79296, 'author': 'Tom Leys', 'author_id': 11440, 'author_profile': 'https://Stackoverflow.com/users/11440', 'pm_score': 2, 'selected': False, 'text': '<p>The output of the explain tells you how long each step has taken. The first thing is to find the steps that have taken a long time and understand what they mean. Things like a sequential scan tell you that you need better indexes - it is mostly a matter of research into your particular database and experience.</p>\n'}, {'answer_id': 79300, 'author': 'convex hull', 'author_id': 10747, 'author_profile': 'https://Stackoverflow.com/users/10747', 'pm_score': 2, 'selected': False, 'text': '<p>One "Oh no, that\'s not right" is often in the form of a <strong>table scan</strong>.  Table scans don\'t utilize any special indexes and can contribute to purging of every useful in memory caches.  In postgreSQL, for example, you will find it looks like this.</p>\n\n<pre><code>Seq Scan on my_table  (cost=0.00..15558.92 rows=620092 width=78)\n</code></pre>\n\n<p>Sometimes table scans are ideal over, say, using an index to query the rows.  However, this is one of those red-flag patterns that you seem to be looking for.</p>\n'}, {'answer_id': 79309, 'author': 'Jonathan Rupp', 'author_id': 12502, 'author_profile': 'https://Stackoverflow.com/users/12502', 'pm_score': 2, 'selected': False, 'text': '<p>Basically, you take a look at each operation and see if the operations "make sense" given your knowledge of how it should be able to work.</p>\n\n<p>For example, if you\'re joining two tables, A and B on their respective columns C and D (A.C=B.D), and your plan shows a clustered index scan (SQL Server term -- not sure of the oracle term) on table A, then a nested loop join to a series of clustered index seeks on table B, you might think there was a problem.  In that scenario, you might expect the engine to do a pair of index scans (over the indexes on the joined columns) followed by a merge join. Further investigation might reveal bad statistics making the optimizer choose that join pattern, or an index that doesn\'t actually exist.</p>\n'}, {'answer_id': 79326, 'author': 'dpollock', 'author_id': 7884, 'author_profile': 'https://Stackoverflow.com/users/7884', 'pm_score': 1, 'selected': False, 'text': '<p>I mainly look for index or table scans. This usually tells me I\'m missing an index on an important column that\'s in the where statement or join statement.</p>\n\n<p>From <a href="http://www.sql-server-performance.com/tips/query_execution_plan_analysis_p1.aspx" rel="nofollow noreferrer">http://www.sql-server-performance.com/tips/query_execution_plan_analysis_p1.aspx</a>:</p>\n\n<blockquote>\n  <p>If you see any of the following in an\n  execution plan, you should consider\n  them warning signs and investigate\n  them for potential performance\n  problems. Each of them are less than\n  ideal from a performance perspective.</p>\n\n<pre><code>* Index or table scans: May indicate a need for better or  additional indexes.\n* Bookmark Lookups: Consider changing the current clustered index,\n  consider using a covering index, limit\n  the number of columns in the SELECT\n  statement.\n* Filter: Remove any functions in the WHERE clause, don\'t include wiews\n  in your Transact-SQL code, may need\n  additional indexes.\n* Sort: Does the data really need to be sorted? Can an index be used to\n  avoid sorting? Can sorting be done at\n  the client more efficiently? \n</code></pre>\n  \n  <p>It is not always possible to avoid\n  these, but the more you can avoid\n  them, the faster query performance\n  will be.</p>\n</blockquote>\n'}, {'answer_id': 79725, 'author': 'Mark Nold', 'author_id': 4134, 'author_profile': 'https://Stackoverflow.com/users/4134', 'pm_score': 3, 'selected': False, 'text': '<p>The two examples below show a FULL scan and a FAST scan using an INDEX.</p>\n\n<p>It\'s best to concentrate on your Cost and Cardinality. Looking at the examples the use of the index reduces the Cost of running the query.</p>\n\n<p>It\'s a bit more complicated (and i don\'t have a 100% handle on it) but basically the Cost is a function of CPU and IO cost, and the Cardinality is the number of rows Oracle expects to parse. Reducing both of these is a good thing.</p>\n\n<p>Don\'t forget that the Cost of a query can be influenced by your query and the Oracle optimiser model (eg: COST, CHOOSE etc) and how often you run your statistics.</p>\n\n<p><strong>Example 1:</strong></p>\n\n<p><a href="http://docs.google.com/a/shanghainetwork.org/File?id=dd8xj6nh_7fj3cr8dx_b">SCAN http://docs.google.com/a/shanghainetwork.org/File?id=dd8xj6nh_7fj3cr8dx_b</a></p>\n\n<p><strong>Example 2 using Indexes:</strong></p>\n\n<p><a href="http://docs.google.com/a/fukuoka-now.com/File?id=dd8xj6nh_9fhsqvxcp_b">INDEX http://docs.google.com/a/fukuoka-now.com/File?id=dd8xj6nh_9fhsqvxcp_b</a></p>\n\n<p>And as already suggested, watch out for TABLE SCAN. You can generally avoid these.</p>\n'}, {'answer_id': 80342, 'author': 'decibel', 'author_id': 11116, 'author_profile': 'https://Stackoverflow.com/users/11116', 'pm_score': 2, 'selected': False, 'text': "<p>Looking for things like sequential scans can be somewhat useful, but the reality is in the numbers... except when the numbers are just estimates! What is usually <strong>far</strong> more useful than looking at a query <em>plan</em> is looking at the actual <em>execution</em>. In Postgres, this is the difference between EXPLAIN and EXPLAIN ANALYZE. EXPLAIN ANALYZE actually executes the query, and gets real timing information for every node. That lets you see what's <em>actually</em> happening, instead of what the planner <em>thinks</em> will happen. Many times you'll find that a sequential scan isn't an issue at all, instead it's something else in the query.</p>\n\n<p>The other key is identifying what the actual expensive step is. Many graphical tools will use different sized arrows to indicate how much different parts of the plan cost. In that case, just look for steps that have thin arrows coming in and a thick arrow leaving. If you're not using a GUI you'll need to eyeball the numbers and look for where they suddenly get much larger. With a little practice it becomes fairly easy to pick out the problem areas.</p>\n"}, {'answer_id': 81802, 'author': 'AJ.', 'author_id': 7211, 'author_profile': 'https://Stackoverflow.com/users/7211', 'pm_score': -1, 'selected': False, 'text': '<h2>Rules of Thumb</h2>\n<p>(you probably want to read up on the details too:</p>\n<ul>\n<li><a href="http://download.oracle.com/docs/cd/B28359_01/server.111/b28274/ex_plan.htm#PFGRF009" rel="nofollow noreferrer">Oracle Docs</a></li>\n<li><a href="http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:231814117467" rel="nofollow noreferrer">ASKTOM</a></li>\n<li><a href="http://www.sql-server-performance.com/tips/query_execution_plan_analysis_p1.aspx" rel="nofollow noreferrer">SQL Server Docs</a><br />\n)</li>\n</ul>\n<h3>Bad</h3>\n<p>Table Scans of Several Large Tables</p>\n<h3>Good</h3>\n<p>Using a unique index<br />\nIndex includes all required fields</p>\n<h3>Most Common Win</h3>\n<p>In about 90% of performance problems I have seen, the easiest win is to break up a query with lots (4 or more) of tables into 2 smaller queries and a temporary table.</p>\n'}, {'answer_id': 98580, 'author': 'EvilTeach', 'author_id': 7734, 'author_profile': 'https://Stackoverflow.com/users/7734', 'pm_score': 2, 'selected': False, 'text': '<p>Really for issues like these, the best thing to do is <a href="http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:231814117467" rel="nofollow noreferrer">ASKTOM</a>.  In particular his answer to that question contains links to the online Oracle doc, where a lot of the those sorts of rules are explained.</p>\n\n<p>One thing to keep in mind, is that explain plans are really best guesses.</p>\n\n<p>It would be a good idea to learn to use sqlplus, and experiment with the AUTOTRACE command.  With some hard numbers, you can generally make better decisions.</p>\n\n<p>But you should ASKTOM.  He knows all about it :)</p>\n'}, {'answer_id': 101140, 'author': 'Tony Andrews', 'author_id': 18747, 'author_profile': 'https://Stackoverflow.com/users/18747', 'pm_score': 4, 'selected': False, 'text': '<p>This subject is too big to answer in a question like this.  You should take some time to read <a href="http://download.oracle.com/docs/cd/B19306_01/server.102/b14211/toc.htm" rel="noreferrer">Oracle\'s Performance Tuning Guide</a></p>\n'}, {'answer_id': 157340, 'author': 'David Aldridge', 'author_id': 6742, 'author_profile': 'https://Stackoverflow.com/users/6742', 'pm_score': 7, 'selected': True, 'text': '<p>I shudder whenever I see comments that full tablescans are bad and index access is good. Full table scans, index range scans, fast full index scans, nested loops, merge join, hash joins etc. are simply access mechanisms that must be understood by the analyst and combined with a knowledge of the database structure and the purpose of a query in order to reach any meaningful conclusion.</p>\n\n<p>A full scan is simply the most efficient way of reading a large proportion of the blocks of a data segment (a table or a table (sub)partition), and, while it often can indicate a performance problem, that is only in the context of whether it is an efficient mechanism for achieving the goals of the query. Speaking as a data warehouse and BI guy, my number one warning flag for performance is an index based access method and a nested loop.</p>\n\n<p>So, for the mechanism of how to read an explain plan the Oracle documentation is a good guide: <a href="http://download.oracle.com/docs/cd/B28359_01/server.111/b28274/ex_plan.htm#PFGRF009" rel="noreferrer">http://download.oracle.com/docs/cd/B28359_01/server.111/b28274/ex_plan.htm#PFGRF009</a></p>\n\n<p>Have a good read through the Performance Tuning Guide also.</p>\n\n<p>Also have a google for "cardinality feedback", a technique in which an explain plan can be used to compare the estimations of cardinality at various stages in a query with the actual cardinalities experienced during the execution. Wolfgang Breitling is the author of the method, I believe.</p>\n\n<p>So, bottom line: understand the access mechanisms. Understand the database. Understand the intention of the query. Avoid rules of thumb.</p>\n'}]
['https://Stackoverflow.com/questions/79266', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/-1/']