Created
June 22, 2012 07:34
-
-
Save bogdan/2971045 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE INDEX "index_looks_on_created_at" ON "looks" ("created_at") | |
explain select * | |
from looks where looks.created_at = (select max(l.created_at) from looks l where l.user_id = looks.user_id) limit 5 | |
-- QUERY PLAN | |
-- --------------------------------------------------------------------------- | |
-- Limit (cost=0.00..26.50 rows=1 width=24) | |
-- -> Seq Scan on looks (cost=0.00..26.50 rows=1 width=24) | |
-- Filter: (created_at = (SubPlan 1)) | |
-- SubPlan 1 | |
-- -> Aggregate (cost=1.25..1.26 rows=1 width=8) | |
-- -> Seq Scan on looks l (cost=0.00..1.25 rows=1 width=8) | |
-- Filter: (user_id = looks.user_id) | |
-- (7 rows) | |
explain | |
select * | |
from looks left join looks l on l.created_at > looks.created_at and l.user_id = looks.user_id | |
where l.id is null | |
-- QUERY PLAN | |
-- --------------------------------------------------------------------- | |
-- Hash Left Join (cost=1.45..2.97 rows=1 width=48) | |
-- Hash Cond: (looks.user_id = l.user_id) | |
-- Join Filter: (l.created_at > looks.created_at) | |
-- Filter: (l.id IS NULL) | |
-- -> Seq Scan on looks (cost=0.00..1.20 rows=20 width=24) | |
-- -> Hash (cost=1.20..1.20 rows=20 width=24) | |
-- -> Seq Scan on looks l (cost=0.00..1.20 rows=20 width=24) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment