Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1UCspc-0000mw-JD for pgsql-performance@arkaria.postgresql.org; Tue, 05 Mar 2013 14:27:04 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.72) (envelope-from ) id 1UCspc-0004e7-2c for pgsql-performance@arkaria.postgresql.org; Tue, 05 Mar 2013 14:27:04 +0000 Received: from makus.postgresql.org ([2001:4800:7903:4::125]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1UCspa-0004ch-9c for pgsql-performance@postgresql.org; Tue, 05 Mar 2013 14:27:02 +0000 Received: from mxin.ulb.ac.be ([164.15.128.112]) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1UCspY-0007O9-JA for pgsql-performance@postgresql.org; Tue, 05 Mar 2013 14:27:01 +0000 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: ApYBAG8ANlGkD30E/2dsb2JhbAANN75bgRiCYIEXgxIBAQEDATIBBUAGCwsYCRYPCQMCAQIBRRMIAQGICaozgzGHKIkOjxMWgyoDnC6EAIhcgTc Received: from bebif01.ulb.ac.be (HELO [10.0.0.194]) ([164.15.125.4]) by smtp.ulb.ac.be with ESMTP/TLS/DHE-RSA-CAMELLIA256-SHA; 05 Mar 2013 15:26:44 +0100 Message-ID: <513600A4.7090602@ulb.ac.be> Date: Tue, 05 Mar 2013 15:26:44 +0100 From: Julien Cigar User-Agent: Mozilla/5.0 (X11; FreeBSD amd64; rv:17.0) Gecko/20130211 Thunderbird/17.0.2 MIME-Version: 1.0 To: pgsql-performance@postgresql.org Subject: Re: Optimize SELECT * from table WHERE foreign_key_id IN (key1,key2,key3,key4...) References: In-Reply-To: Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 8bit X-Pg-Spam-Score: -4.8 (----) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-performance Precedence: bulk Sender: pgsql-performance-owner@postgresql.org On 03/05/2013 15:00, Niels Kristian Schjødt wrote: > Hi, > > I'm running a rails app, where I have a model called Car that has_many Images. Now when I tell rails to include those images, when querying say 50 cars, then it often decides to use a SELECT * from images WHERE car_id IN (id1,id2,id3,id4…) instead of doing a join. why do you want a join here ? if you don't need any "cars" data there is no need to JOIN that table. Now a select ... from ... where id in (id1, id2, ..., idn) isn't very scalable. Instead of passing id1, id2, ..., idn you'be better pass the condition and do a where id in (select ... ), or where exists (select 1 ... where ...), or a join, or ... > Now either way it uses the index I have on car_id: > > Index Scan using car_id_ix on adverts (cost=0.47..5665.34 rows=1224 width=234) > Index Cond: (car_id = ANY ('{7097561,7253541,5159633,6674471,...}'::integer[])) > > But it's slow, it's very slow. In this case it took 3,323ms 3ms isn't slow > Can I do anything to optimize that query or maybe the index or something? your index is already used > The table has 16.000.000 rows > -- No trees were killed in the creation of this message. However, many electrons were terribly inconvenienced. -- Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-performance