I am comparing queries on PostgreSQL 8.3.14 which return the same result set.
I have used EXPLAIN
on my queries to track the estimated total cost. I have also run the queries a few times and recorded the total time it took to run. I understand consecutive runs will cause more data to be cached and skew the actual no-cache runtime.
Still I would expect that EXPLAIN
cost would be somewhat proportional to the total runtime (with cache skew).
My data denies this. I compared 4 queries.
- Query A
- Total Cost: 119 500
- Average Runtime: 28.101 seconds
- Query B
- Total Cost: 115 700
- Average Runtime: 28.291 seconds
- Query C
- Total Cost: 116 200
- Average Runtime: 32.409 seconds
- Query D
- Total Cost: 93 200
- Average Runtime: 37.503 seconds
I ran Query D last and if anything it should be the fastest because of the caching problem. Since running the queries without cache seems to be difficult based on this Q+A:
[SO]:See and clear Postgres caches/buffers?
How can I measure which query is the most efficient?