Skip to content

Instantly share code, notes, and snippets.

@jaylevitt
Created September 20, 2012 00:14
Show Gist options
  • Save jaylevitt/3753166 to your computer and use it in GitHub Desktop.
Save jaylevitt/3753166 to your computer and use it in GitHub Desktop.
Subquery in a JOIN not getting restricted
drop schema if exists jaytest cascade;
create schema jaytest;
set search_path to jaytest;
create table questions (
id int not null primary key,
user_id int not null
);
insert into questions
select generate_series(1,1100), (random()*200000)::int;
create table users (
id int not null primary key
);
insert into users select generate_series(1, 200000);
vacuum freeze analyze;
explain analyze
select questions.id
from questions
join (
select u.id
from users as u
group by u.id
) as s
on s.id = questions.user_id
where questions.id = 1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment