1

I can't understand EXPLAIN of quite simple query:

select * from store  order by id desc limit 1;
QUERY PLAN Limit  (cost=0.00..0.03 rows=1 width=31)
  ->  Index Scan Backward using store_pkey on store  (cost=0.00..8593.28 rows=250000 width=31)

Why does top level node (limit) have cost lower than nested(index scan) has? As I read from documentation it should be cumulative cost, i.e. 8593.28 + 0.03

2 Answers 2

1

The docs (emphasis added) say;

Actually two numbers are shown: the start-up cost before the first row can be returned, and the total cost to return all the rows. For most queries the total cost is what matters, but in contexts such as a subquery in EXISTS, the planner will choose the smallest start-up cost instead of the smallest total cost (since the executor will stop after getting one row, anyway).

In other words, 8593.28 would be the cost to return all the rows, but due to the limit you're only returning one so the actual cost is much lower (more or less equal to the startup cost)

Sign up to request clarification or add additional context in comments.

Comments

0

The numbers you see in the top node (0.00..0.03) are (per documentation)

  • 0.00 .. the estimated start-up cost of the node
  • 0.03 .. the estimated total cost of the node

If you want actual total times, run EXPLAIN ANALYZE, which appends actual times for every node. Like:

Limit (cost=0.29..0.31 rows=1 width=30) (actual time=xxx.xxx..xxx.xxx rows=1 loops=1)

Bold emphasis mine.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.