I was curious. And, as we all know, curiosity has a reputation for killing cats.
So what is the fastest way cat cats?
The exact environment of the sketas for this test:
- PostgreSQL 9.0 on Debian Squeeze with decent memory and settings.
- 6,000 students, 24,000 club members (data copied from a similar database with real-life data.)
- A slight deviation from the naming scheme in the question:
student.id is student.stud_id and club.id here club.club_id . - I named the queries after their author in this thread with an index in which there are two.
- Several times I ran all queries to fill the cache, and then I selected the best of 5 with EXPLAIN ANALYZE.
Corresponding indexes (should be optimal - if we lack knowledge of which clubs will be requested):
ALTER TABLE student ADD CONSTRAINT student_pkey PRIMARY KEY(stud_id ); ALTER TABLE student_club ADD CONSTRAINT sc_pkey PRIMARY KEY(stud_id, club_id); ALTER TABLE club ADD CONSTRAINT club_pkey PRIMARY KEY(club_id ); CREATE INDEX sc_club_id_idx ON student_club (club_id);
club_pkey not required for most requests here. Primary keys automatically implement unique indexes in PostgreSQL.
The latter index should compensate for this well-known drawback of PostgreSQL multi-column indexes :
The B-tree multi-column index can be used with query conditions that include any subset of the index columns, but the index is most effective when there are restrictions on the leading (left) columns.
Results:
Total run time from EXPLAIN ANALYZE.
1) Martin 2: 44.594 ms
SELECT s.stud_id, s.name FROM student s JOIN student_club sc USING (stud_id) WHERE sc.club_id IN (30, 50) GROUP BY 1,2 HAVING COUNT(*) > 1;
2) Erwin 1: 33.217 ms
SELECT s.stud_id, s.name FROM student s JOIN ( SELECT stud_id FROM student_club WHERE club_id IN (30, 50) GROUP BY 1 HAVING COUNT(*) > 1 ) sc USING (stud_id);
3) Martin 1: 31.735 ms
SELECT s.stud_id, s.name FROM student s WHERE student_id IN ( SELECT student_id FROM student_club WHERE club_id = 30 INTERSECT SELECT stud_id FROM student_club WHERE club_id = 50);
4) Derek: 2.287 ms
SELECT s.stud_id, s.name FROM student s WHERE s.stud_id IN (SELECT stud_id FROM student_club WHERE club_id = 30) AND s.stud_id IN (SELECT stud_id FROM student_club WHERE club_id = 50);
5) Erwin 2: 2.181 ms
SELECT s.stud_id, s.name FROM student s WHERE EXISTS (SELECT * FROM student_club WHERE stud_id = s.stud_id AND club_id = 30) AND EXISTS (SELECT * FROM student_club WHERE stud_id = s.stud_id AND club_id = 50);
6) Sean: 2.043 ms
SELECT s.stud_id, s.name FROM student s JOIN student_club x ON s.stud_id = x.stud_id JOIN student_club y ON s.stud_id = y.stud_id WHERE x.club_id = 30 AND y.club_id = 50;
The last three do almost the same thing. 4) and 5) lead to the same tariff plan.
Late additions:
Unusual SQL, but performance cannot keep up.
7) ypercube 1: 148.649 ms
SELECT s.stud_id, s.name FROM student AS s WHERE NOT EXISTS ( SELECT * FROM club AS c WHERE c.club_id IN (30, 50) AND NOT EXISTS ( SELECT * FROM student_club AS sc WHERE sc.stud_id = s.stud_id AND sc.club_id = c.club_id ) );
8) ypercube 2: 147.497 ms
SELECT s.stud_id, s.name FROM student AS s WHERE NOT EXISTS ( SELECT * FROM ( SELECT 30 AS club_id UNION ALL SELECT 50 ) AS c WHERE NOT EXISTS ( SELECT * FROM student_club AS sc WHERE sc.stud_id = s.stud_id AND sc.club_id = c.club_id ) );
As expected, the two do pretty much the same thing. In terms of the query plan, when scanning tables, the scheduler does not find a way to use indexes here.
9) wildplasser 1: 49.849 ms
WITH RECURSIVE two AS ( SELECT 1::int AS level , stud_id FROM student_club sc1 WHERE sc1.club_id = 30 UNION SELECT two.level + 1 AS level , sc2.stud_id FROM student_club sc2 JOIN two USING (stud_id) WHERE sc2.club_id = 50 AND two.level = 1 ) SELECT s.stud_id, s.student FROM student s JOIN two USING (studid) WHERE two.level > 1;
Unusual SQL, decent performance for CTE. Very exotic query plan.
Again, it would be interesting how 9.1 handles this. I am going to upgrade the db cluster used here to 9.1 in the near future. Maybe I'll repeat the whole shaban ...
10) wildplasser 2: 36.986 ms
WITH sc AS ( SELECT stud_id FROM student_club WHERE club_id IN (30,50) GROUP BY stud_id HAVING COUNT(*) > 1 ) SELECT s.* FROM student s JOIN sc USING (stud_id);
CTE request variant 2). Surprisingly, this can lead to a slightly different tariff plan with the same data. I found a sequential scan on student where an index was used as a subquery.
11) ypercube 3: 101.482 ms
Another latest addition is @ypercube. It is amazing how many ways.
SELECT s.stud_id, s.student FROM student s JOIN student_club sc USING (stud_id) WHERE sc.club_id = 10
12) erwin 3: 2.377 ms
@ypercube 11) is actually just the reverse of the reverse of this simpler option, which is also still missing. It runs almost as fast as the best cats.
SELECT s.* FROM student s JOIN student_club x USING (stud_id) WHERE sc.club_id = 10
13) erwin 4: 2.375 ms
It's hard to believe, but here's another, really new option. I see the potential of more than two members, but he is also among the best cats in just two.
SELECT s.* FROM student AS s WHERE EXISTS ( SELECT * FROM student_club AS x JOIN student_club AS y USING (stud_id) WHERE x.stud_id = s.stud_id AND x.club_id = 14 AND y.club_id = 10 )
Dynamic number of club members
In other words: a different number of filters. This question asked exactly two club memberships. But many use cases need to be prepared for varying amounts.
Detailed discussion in this next later answer:
- Using the same column multiple times in the WHERE clause