Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If the query planner makes bad decisions it's mostly one of two things:</p> <h2>1. The <strong>statistics</strong> are off.</h2> <p><sub>Meaning "inaccurate", not "turned off".</sub></p> <p>Do you run <code>ANALYZE</code> enough? Also popular in it's combined form <code>VACUUM ANALYZE</code>. If <a href="http://www.postgresql.org/docs/current/static/routine-vacuuming.html#AUTOVACUUM" rel="noreferrer">autovacuum</a> is on (which is the default in modern-day Postgres), <code>ANALYZE</code> is run automatically. But consider:</p> <ul> <li><a href="https://dba.stackexchange.com/questions/18664/are-regular-vacuum-analyze-still-recommended-under-9-1/18694#18694">Are regular VACUUM ANALYZE still recommended under 9.1?</a></li> </ul> <p><sup>(Top two answers still apply for Postgres 9.6.)</sup></p> <p>If your table is <em>big</em> and data distribution is <em>irregular</em>, raising the <a href="http://www.postgresql.org/docs/current/interactive/runtime-config-query.html#RUNTIME-CONFIG-QUERY-OTHER" rel="noreferrer"><code>default_statistics_target</code></a> may help. Or rather, just <a href="http://www.postgresql.org/docs/current/interactive/sql-altertable.html" rel="noreferrer">set the statistics target</a> for relevant columns (those in <code>WHERE</code> or <code>JOIN</code> clauses of your queries, basically): </p> <pre><code>ALTER TABLE ... ALTER COLUMN ... SET STATISTICS 1234; -- calibrate number </code></pre> <blockquote> <p>The target can be set in the range 0 to 10000;</p> </blockquote> <p>Run <code>ANALYZE</code> again after that (on relevant tables). </p> <h2>2. The <strong>cost settings</strong> for planner estimates are off.</h2> <p>Read the chapter <a href="http://www.postgresql.org/docs/current/static/runtime-config-query.html#RUNTIME-CONFIG-QUERY-CONSTANTS" rel="noreferrer">Planner Cost Constants</a> in the manual. </p> <p>Look at the chapters <strong>default_statistics_target</strong> and <strong>random_page_cost</strong> on this <a href="http://wiki.postgresql.org/wiki/Tuning_Your_PostgreSQL_Server" rel="noreferrer">generally helpful PostgreSQL Wiki page</a>.</p> <p>Of course, there can be many other possible reasons, but these are the most common ones by far.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload