Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1XwWfc-0007JR-Fa for pgsql-sql@arkaria.postgresql.org; Thu, 04 Dec 2014 13:42:12 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1XwWfc-0003N1-0a for pgsql-sql@arkaria.postgresql.org; Thu, 04 Dec 2014 13:42:12 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1XwWfb-0003Mv-8q for pgsql-sql@postgresql.org; Thu, 04 Dec 2014 13:42:11 +0000 Received: from mail-qg0-x230.google.com ([2607:f8b0:400d:c04::230]) by makus.postgresql.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:256) (Exim 4.80) (envelope-from ) id 1XwWfY-0000UX-35 for pgsql-sql@postgresql.org; Thu, 04 Dec 2014 13:42:09 +0000 Received: by mail-qg0-f48.google.com with SMTP id q107so12678598qgd.35 for ; Thu, 04 Dec 2014 05:42:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=message-id:date:from:user-agent:mime-version:to:subject:references :in-reply-to:content-type:content-transfer-encoding; bh=W3jRyB6hFrIRyW4b33AOTMWjyoxF5uaAnt9V3XJDJf4=; b=G1eG+2v3Vgm8vQCb6NL7qHdMPmLcpdMMyFm9odZaj7kXP0/uou1vuTXIG/zX0ulG1O WXt47u+WuS+2phW4JuIol3HCZiCqDy12VQlVnscgrTXLg0l1VYeYS5o6qEt6h4ULRVO/ olnFdW++ZdIOxYX7kq5rZNZBo5vLhNZx3tKKGgfu+TSUmvyTnzP4XCxGRayEwd9VbItF j+Xh6XxVpb0ZYeqIhuju3T/drqiEAUuNR+cNDc4CDr+j8gIqnQfo81Ulps+PRAb/xU+z hfhlVvJ4cr7aX1F5pwk9Q2iafKCUhr1raWIttX/uhLlaDT65wOowBs409NyZs8lYiIRW BJ6A== X-Received: by 10.229.19.3 with SMTP id y3mr16187700qca.1.1417700527164; Thu, 04 Dec 2014 05:42:07 -0800 (PST) Received: from timbomac-2.local (host247.181-15-182.telecom.net.ar. [181.15.182.247]) by mx.google.com with ESMTPSA id 7sm9661006qak.20.2014.12.04.05.42.05 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Thu, 04 Dec 2014 05:42:06 -0800 (PST) Message-ID: <548064AE.4020407@gmail.com> Date: Thu, 04 Dec 2014 10:42:06 -0300 From: Tim Dudgeon User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:24.0) Gecko/20100101 Thunderbird/24.6.0 MIME-Version: 1.0 To: pgsql-sql@postgresql.org Subject: Re: Querying with arrays References: <547722A7.4040702@gmail.com> <3676.1417100070@sss.pgh.pa.us> In-Reply-To: <3676.1417100070@sss.pgh.pa.us> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: -2.7 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-sql Precedence: bulk Sender: pgsql-sql-owner@postgresql.org Looking into this further I don't seem able to get the index used. I created this simple example: create table lists ( id SERIAL PRIMARY KEY, name VARCHAR(32) NOT NULL, hits INTEGER[] NOT NULL ); CREATE INDEX idx_lists_hits ON lists USING gin (hits); INSERT INTO lists (name, hits) VALUES ('list1-10', ARRAY[1,2,3,4,5,6,7,8,9,10]); explain analyze SELECT id, name FROM lists WHERE hits @> array[7]; The plan for the query is this: "Seq Scan on lists (cost=0.00..16.88 rows=3 width=86) (actual time=0.006..0.008 rows=1 loops=1)" " Filter: (hits @> '{7}'::integer[])" "Planning time: 0.058 ms" "Execution time: 0.025 ms" What am I doing wrong? Tim On 27/11/2014 11:54, Tom Lane wrote: > Tim Dudgeon writes: >> I'm considering using arrays to handle managing "lists" of rows (I know >> this may not be the best approach, but bear with me). >> I create a table for my lists like this:** >> create table lists ( >> id SERIAL PRIMARY KEY, >> hits INTEGER[] NOT NULL >> ); >> Then I can insert the results of a query into that table as a new list >> of hits >> INSERT INTO lists (hits) >> SELECT array_agg(id) >> FROM some_table >> WHERE ...; >> Now the problem part. How to best use that array of primary key values >> to restore the data at a later stage. Conceptually I'm wanting this: >> SELECT * from some_table >> WHERE id ; >> These both work by are really slow: >> SELECT t1.* >> FROM some_table t1 >> WHERE t1.id IN (SELECT unnest(hits) from lists WHERE id = 2); >> SELECT t1.* >> FROM some_table t1 >> JOIN lists l ON t1.id = any(l.hits) >> WHERE l.id = 2; >> Is there an efficient way to do this, or is this a dead end? > You could create a GIN index on lists.hits and then do > > SELECT t1.* > FROM some_table t1 > JOIN lists l ON array[t1.id] <@ l.hits > WHERE l.id = 2; > > How efficient that will be remains to be determined though; > if the l.id condition will eliminate a lot of matches it > could still be kind of slow. > > (ISTR some talk of teaching the planner to convert =ANY(array) > conditions to this form automatically when there's a suitable > index, but for now you'd have to write it out like this.) > > regards, tom lane -- Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-sql