Skip to content

Instantly share code, notes, and snippets.

@paulghaddad
Created February 3, 2015 21:32
Show Gist options
  • Save paulghaddad/3a46a39a4d585f24c0dd to your computer and use it in GitHub Desktop.
Save paulghaddad/3a46a39a4d585f24c0dd to your computer and use it in GitHub Desktop.
LevelUp 6: Knows why and how to explain a query and check index use
2. Explain what the cost numbers mean for a given query plan.
The cost consists of two numbers, displayed as follows: cost=0.00..458.00. 0.00 is the "Estimated Startup Cost", which is the time expended before the output phase (sorting for a sort node) can begin. 458.00 is the "Estimated Total Cost", which is the cost to retrieve all the rows. It assumes the plan node is run to completion, although the node's parent may stop short of retrieving all the nodes.
The costs are measured in arbitrary units, usually based in units of disk page fetches. It is computed with the following equation:
(disk pages read * seq_page_cost) + (rows scanned * cpu_tuple_cost)
The goal of the query planner is to minimize the cost for a given query plan. By using "Explain Analyze" on a query, you can check the actual cost numbers and row counts for each plan node. By modifying the query with the goal of reducing the cost numbers, you can minimize the time to perform a query.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment