Re: Simply join in PostrgeSQL takes too long - Mailing list pgsql-performance

From Atesz
Subject Re: Simply join in PostrgeSQL takes too long
Date
Msg-id 002a01c42ca2$7492ac30$0b02010a@atesz
Whole thread Raw
In response to Simply join in PostrgeSQL takes too long  (Vitaly Belman <vitalib@012.net.il>)
List pgsql-performance
Hi,

You can try some variation:

SELECT
  book_id
FROM
  bookgenres, genre_children
WHERE
   bookgenres.genre_id = genre_children.genre_child_id AND
   genre_children.genre_id = 1
GROUP BY book_id
LIMIT 10

The next works if the 'genre_child_id' is UNIQUE on the 'genre_children'
table.

SELECT
  book_id
FROM
  bookgenres
WHERE
   bookgenres.genre_id = (SELECT genre_child_id FROM genre_children
WHERE genre_id = 1)
GROUP BY book_id
LIMIT 10

You may need some index. Try these with EXPLAIN!
CREATE INDEX bookgenres_genre_id_book_id ON bookgenres(genre_id,
book_id);  or
CREATE INDEX bookgenres_book_id_genre_id ON bookgenres(book_id,
genre_id);
CREATE INDEX genre_children_genre_id ON genre_children(genre_id);

Regards, Antal Attila



pgsql-performance by date:

Previous
From: Edoardo Ceccarelli
Date:
Subject: Re: [JDBC] is a good practice to create an index on the
Next
From: Nick Barr
Date:
Subject: Re: Simply join in PostrgeSQL takes too long