Skip to content

Instantly share code, notes, and snippets.

@jehie
Created May 4, 2017 10:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jehie/284e7852089f6debe22e05c63e73027f to your computer and use it in GitHub Desktop.
Save jehie/284e7852089f6debe22e05c63e73027f to your computer and use it in GitHub Desktop.
sql_performance_issue_limit_approach
SQL and Query Plan for the LIMIT approach (Cannot search for multiple author_ids)
SQL:
EXPLAIN ANALYZE
SELECT released, author_id
FROM book
WHERE author_id = '1'
AND released<=to_timestamp(2e9)
AND released>=to_timestamp(0)
ORDER BY released desc
LIMIT 1
Query plan:
"Limit (cost=0.43..1.55 rows=1 width=10) (actual time=0.112..0.113 rows=1 loops=1)"
" -> Index Only Scan Backward using book_idx1 on book (cost=0.43..117791.74 rows=105146 width=10) (actual time=0.109..0.109 rows=1 loops=1)"
" Index Cond: ((author_id = '1'::text) AND (released <= '2033-05-18 06:33:20+03'::timestamp with time zone) AND (released >= '1970-01-01 02:00:00+02'::timestamp with time zone))"
" Heap Fetches: 1"
"Planning time: 0.446 ms"
"Execution time: 0.168 ms"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment