From pstm.spain@gmail.com Tue Jul 3 09:29:29 2012 Received: from makus.postgresql.org (makus.postgresql.org [98.129.198.125]) by mail.postgresql.org (Postfix) with ESMTP id 7FFBA1845A07 for ; Tue, 3 Jul 2012 06:29:29 -0300 (ADT) Received: from mail-bk0-f46.google.com ([209.85.214.46]) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1SlzQF-0002pz-PO for pgsql-performance@postgresql.org; Tue, 03 Jul 2012 09:29:29 +0000 Received: by bkwj10 with SMTP id j10so1400712bkw.19 for ; Tue, 03 Jul 2012 02:29:12 -0700 (PDT) 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 :content-type; bh=YPMY0tFo8DnT1Z6I170Wk7CuJuciN6OQgYpEkfFlZDY=; b=T7t05jLrmVneDRFsLXcXYiucHXxxYWky3ZFHnjVkg1s57BDcIbjTwIX0lcMvBzqsJh iUt+pQkzHW7wuvmiUoT3Hsbb1EVXd1YKvacZkzL1sFXfeODjxuc7TO6zpkUzkix468zi GzYTmGh+P78w5oNuSFWcRPYz1vCm/v/4W+pz4KN8Ayxj6X49AxuNIfva1yG2E4xYfaV0 VpbiFFP5NSRm/11yiB+mmAv5J75wPnMUBAckUdrJgfu1DyLfRcExHHTFH4Xrym8r+tVM SAbU9k16OABnE7m/cTjxyGPoqN0G43tZJGGZQmEPBpRl8imH8pBnl7UjW78NtzNMhW8B 1Y+A== Received: by 10.204.128.201 with SMTP id l9mr8967720bks.68.1341307752828; Tue, 03 Jul 2012 02:29:12 -0700 (PDT) Received: from athlon.ww1.0pc.eu (66.Red-88-29-2.staticIP.rima-tde.net. [88.29.2.66]) by mx.google.com with ESMTPS id h18sm16694674bkh.8.2012.07.03.02.29.11 (version=SSLv3 cipher=OTHER); Tue, 03 Jul 2012 02:29:12 -0700 (PDT) Message-ID: <4FF2BB65.7080903@gmail.com> Date: Tue, 03 Jul 2012 11:29:09 +0200 From: PV User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:12.0) Gecko/20120430 Thunderbird/12.0.1 MIME-Version: 1.0 To: pgsql-performance@postgresql.org Subject: static virtual columns as result? Content-Type: multipart/alternative; boundary="------------070102040101040209080507" X-Pg-Spam-Score: -2.6 (--) X-Archive-Number: 201207/4 X-Sequence-Number: 47185 This is a multi-part message in MIME format. --------------070102040101040209080507 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Hello, I "have" this two tables: Table Cat: id|A|B --+-+- 1|3|5 2|5|8 3|6|9 Table Pr: id|Catid| --+- 1|3 2|2 3|1 I need replace "Catid" column for corresponding values A and B (Table Cat) in Table Pr. Virtual table like this: Table Pr: id|Catid|A|B --+-+-+ 1|3|6|9 2|2|5|8 3|1|3|5 Something like this, but that works,... SELECT * FROM pr WHERE pr.a /*> 1 AND*/*//* pr.b < 10; With subqueries is too slow: SELECT * FROM "Pr" AS p, (SELECT "id" AS cid FROM "Cat" WHERE "lft" > 1 AND "rgt" < 10) AS c WHERE p."Cat"=c."cid" AND (...)) ORDER BY "Catid" ASC OFFSET 0 LIMIT 40 Any suggestion? --------------070102040101040209080507 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Hello,

I "have" this two tables:


Table Cat:
id|A|B
--+-+-
1|3|5
2|5|8
3|6|9

Table Pr:
id|Catid|
--+-
1|3 
2|2 
3|1


I need replace "Catid" column for corresponding values A and B (Table Cat) in Table Pr.

Virtual table like this:
Table Pr:
id|Catid|A|B
--+-+-+
1|3|6|9 
2|2|5|8 
3|1|3|5


Something like this, but that works,...

SELECT * FROM pr WHERE pr.a > 1 AND pr.b < 10;

With subqueries is too slow:
SELECT * FROM "Pr" AS p, (SELECT "id" AS cid FROM "Cat" WHERE "lft" > 1 AND "rgt" < 10) AS c WHERE p."Cat"=c."cid" AND (...)) ORDER BY "Catid" ASC OFFSET 0 LIMIT 40


Any suggestion?

--------------070102040101040209080507-- From Kevin.Grittner@wicourts.gov Tue Jul 3 13:45:14 2012 Received: from makus.postgresql.org (makus.postgresql.org [98.129.198.125]) by mail.postgresql.org (Postfix) with ESMTP id 356181845A07 for ; Tue, 3 Jul 2012 10:45:14 -0300 (ADT) Received: from gwmta.wicourts.gov ([165.219.244.99] helo=gw.wicourts.gov) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1Sm3Pj-0007XE-QA for pgsql-performance@postgresql.org; Tue, 03 Jul 2012 13:45:14 +0000 Received: from Courts-MTA by gw.wicourts.gov with Novell_GroupWise; Tue, 03 Jul 2012 08:44:59 -0500 Message-Id: <4FF2B1020200002500048D3F@gw.wicourts.gov> X-Mailer: Novell GroupWise Internet Agent 8.0.2 Date: Tue, 03 Jul 2012 08:44:50 -0500 From: "Kevin Grittner" To: , Subject: Re: static virtual columns as result? Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Content-Disposition: inline X-Pg-Spam-Score: -4.2 (----) X-Archive-Number: 201207/5 X-Sequence-Number: 47186 PV wrote: > Any suggestion? You provided too little information to suggest much beyond using JOIN instead of a subquery. Something like: SELECT pr.id, pr.catid, cat.a, cat.b FROM pr join cat ON (cat.id = pr.catid) WHERE "lft" > 1 AND "rgt" < 10 AND (...) ORDER BY cat.id OFFSET 0 LIMIT 40; We can provide more specific suggestions if you follow the advice here: http://wiki.postgresql.org/wiki/SlowQueryQuestions And please format your queries to make them more readable -- something like I did above. -Kevin From pstm.spain@gmail.com Tue Jul 3 15:44:01 2012 Received: from magus.postgresql.org (magus.postgresql.org [87.238.57.229]) by mail.postgresql.org (Postfix) with ESMTP id 9CA331FFFD22 for ; Tue, 3 Jul 2012 12:44:01 -0300 (ADT) Received: from mail-bk0-f46.google.com ([209.85.214.46]) by magus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1Sm5Gf-0006J9-Uo for pgsql-performance@postgresql.org; Tue, 03 Jul 2012 15:44:01 +0000 Received: by mail-bk0-f46.google.com with SMTP id j10so1794729bkw.19 for ; Tue, 03 Jul 2012 08:43:57 -0700 (PDT) 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:cc:subject :references:in-reply-to:content-type:content-transfer-encoding; bh=pssv6u5sbuyBsnOufU4aVVLbfb5Vncsh3Rf87cWmLr4=; b=T7VlATQFShM3cprOwk+FCdoWT4cPoWE31IDv8zaeTYxv3Z+Pfdzd2UoEP2WOQXR+ig g3W5v1n276yHh96k8BSNps6GftjfnWXXcCew6VuQwsQO6bTrvDuiEBXhkxr8O+0trYAc gbp4KcLe3u0xPn3neRHoFNAYlwnAw3Rf6pjYPuX/NTwC+h6efoc7fto9yA6vsnhEvaU8 KrYoog020GQCIzFWUBN0o752mNAtQTjvFO8ab7XCciARALZqLQz1w+Qdcf8msYaNXX+I tmqxXvchSvKcjQB0LWxM48t6jue6LoX9dJTqbtm/qRDE7U4XNMdAjH7/7urfJXGpG1p7 TtyA== Received: by 10.204.152.4 with SMTP id e4mr4318719bkw.2.1341330237281; Tue, 03 Jul 2012 08:43:57 -0700 (PDT) Received: from athlon.ww1.0pc.eu (66.Red-88-29-2.staticIP.rima-tde.net. [88.29.2.66]) by mx.google.com with ESMTPS id h18sm17606668bkh.8.2012.07.03.08.43.55 (version=SSLv3 cipher=OTHER); Tue, 03 Jul 2012 08:43:56 -0700 (PDT) Message-ID: <4FF3133A.6010801@gmail.com> Date: Tue, 03 Jul 2012 17:43:54 +0200 From: PV User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:12.0) Gecko/20120430 Thunderbird/12.0.1 MIME-Version: 1.0 To: Kevin Grittner CC: pgsql-performance@postgresql.org Subject: Re: static virtual columns as result? References: <4FF2B1020200002500048D3F@gw.wicourts.gov> In-Reply-To: <4FF2B1020200002500048D3F@gw.wicourts.gov> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 8bit X-Pg-Spam-Score: -2.7 (--) X-Archive-Number: 201207/6 X-Sequence-Number: 47187 El 03/07/12 15:44, Kevin Grittner escribió: > You provided too little information to suggest much beyond using JOIN > instead of a subquery. Something like: I think that adding new columns to Product , lft and rgt with index should be fast. But does not seem a good design. Tables: ######################################### ######################################### -- Table: "Category" CREATE TABLE "Category" ( id serial NOT NULL, ... lft integer, rgt integer, ... path ltree, description text NOT NULL, idxfti tsvector, ... CONSTRAINT "Category_pkey" PRIMARY KEY (id ), ) WITH (OIDS=FALSE); ALTER TABLE "Category" OWNER TO root; CREATE INDEX "Category_idxfti_idx" ON "Category" USING gist (idxfti ); CREATE INDEX "Category_lftrgt_idx" ON "Category" USING btree (lft , rgt ); CREATE TRIGGER categorytsvectorupdate BEFORE INSERT OR UPDATE ON "Category" FOR EACH ROW EXECUTE PROCEDURE tsearch2('idxfti', 'description'); #################################### -- Table: "Product" CREATE TABLE "Product" ( id serial NOT NULL, ... description text NOT NULL, "Category" integer NOT NULL, ... creationtime integer NOT NULL, ... idxfti tsvector, ... CONSTRAINT product_pkey PRIMARY KEY (id ), CONSTRAINT product_creationtime_check CHECK (creationtime >= 0), ) WITH ( OIDS=FALSE ); CREATE INDEX "Product_Category_idx" ON "Product" USING btree ("Category" ); CREATE INDEX "Product_creationtime" ON "Product" USING btree (creationtime ); CREATE INDEX "Product_idxfti_idx" ON "Product" USING gist (idxfti ); CREATE TRIGGER producttsvectorupdate BEFORE INSERT OR UPDATE ON "Product" FOR EACH ROW EXECUTE PROCEDURE tsearch2('idxfti','description'); ################################# ######################################### Query ######################################### EXPLAIN ANALYZE SELECT * FROM "Product" AS p JOIN "Category" ON ("Category".id = p."Category") WHERE "lft" BETWEEN 1 AND 792 ORDER BY creationtime ASC OFFSET 0 LIMIT 40 "Limit (cost=2582.87..2582.97 rows=40 width=1688) (actual time=4306.209..4306.328 rows=40 loops=1)" " -> Sort (cost=2582.87..2584.40 rows=615 width=1688) (actual time=4306.205..4306.246 rows=40 loops=1)" " Sort Key: p.creationtime" " Sort Method: top-N heapsort Memory: 69kB" " -> Nested Loop (cost=31.21..2563.43 rows=615 width=1688) (actual time=0.256..3257.310 rows=122543 loops=1)" " -> Index Scan using "Category_lftrgt_idx" on "Category" (cost=0.00..12.29 rows=2 width=954) (actual time=0.102..18.598 rows=402 loops=1)" " Index Cond: ((lft >= 1) AND (lft <= 792))" " -> Bitmap Heap Scan on "Product" p (cost=31.21..1270.93 rows=371 width=734) (actual time=0.561..6.125 rows=305 loops=402)" " Recheck Cond: ("Category" = "Category".id)" " -> Bitmap Index Scan on "Product_Category_idx" (cost=0.00..31.12 rows=371 width=0) (actual time=0.350..0.350 rows=337 loops=402)" " Index Cond: ("Category" = "Category".id)" "Total runtime: 4306.706 ms" ######################################### EXPLAIN ANALYZE SELECT * FROM "Product" AS p WHERE (p."idxfti" @@ to_tsquery('simple', 'vpc')) ORDER BY creationtime ASC OFFSET 0 LIMIT 40 "Limit (cost=471.29..471.39 rows=40 width=734) (actual time=262.854..262.971 rows=40 loops=1)" " -> Sort (cost=471.29..471.57 rows=113 width=734) (actual time=262.850..262.890 rows=40 loops=1)" " Sort Key: creationtime" " Sort Method: top-N heapsort Memory: 68kB" " -> Bitmap Heap Scan on "Product" p (cost=49.62..467.72 rows=113 width=734) (actual time=258.502..262.322 rows=130 loops=1)" " Recheck Cond: (idxfti @@ '''vpc'''::tsquery)" " -> Bitmap Index Scan on "Product_idxfti_idx" (cost=0.00..49.60 rows=113 width=0) (actual time=258.340..258.340 rows=178 loops=1)" " Index Cond: (idxfti @@ '''vpc'''::tsquery)" "Total runtime: 263.177 ms" ######################################### And here is a big problem: EXPLAIN ANALYZE SELECT * FROM "Product" AS p JOIN "Category" ON ("Category".id = p."Category") WHERE "lft" BETWEEN 1 AND 792 AND (p."idxfti" @@ to_tsquery('simple', 'vpc')) ORDER BY creationtime ASC OFFSET 0 LIMIT 40 "Limit (cost=180.09..180.09 rows=1 width=1688) (actual time=26652.316..26652.424 rows=40 loops=1)" " -> Sort (cost=180.09..180.09 rows=1 width=1688) (actual time=26652.312..26652.350 rows=40 loops=1)" " Sort Key: p.creationtime" " Sort Method: top-N heapsort Memory: 96kB" " -> Nested Loop (cost=85.27..180.08 rows=1 width=1688) (actual time=12981.612..26651.594 rows=130 loops=1)" " -> Bitmap Heap Scan on "Category" (cost=4.27..10.03 rows=2 width=954) (actual time=0.215..1.580 rows=402 loops=1)" " Recheck Cond: ((lft >= 1) AND (lft <= 792))" " -> Bitmap Index Scan on "Category_lftrgt_idx" (cost=0.00..4.27 rows=2 width=0) (actual time=0.193..0.193 rows=402 loops=1)" " Index Cond: ((lft >= 1) AND (lft <= 792))" " -> Bitmap Heap Scan on "Product" p (cost=81.00..85.01 rows=1 width=734) (actual time=66.276..66.280 rows=0 loops=402)" " Recheck Cond: (("Category" = "Category".id) AND (idxfti @@ '''vpc'''::tsquery))" " -> BitmapAnd (cost=81.00..81.00 rows=1 width=0) (actual time=66.263..66.263 rows=0 loops=402)" " -> Bitmap Index Scan on "Product_Category_idx" (cost=0.00..31.12 rows=371 width=0) (actual time=0.188..0.188 rows=337 loops=402)" " Index Cond: ("Category" = "Category".id)" " -> Bitmap Index Scan on "Product_idxfti_idx" (cost=0.00..49.60 rows=113 width=0) (actual time=70.557..70.557 rows=178 loops=376)" " Index Cond: (idxfti @@ '''vpc'''::tsquery)" "Total runtime: 26652.772 ms" ######################################### Similar query: EXPLAIN ANALYZE SELECT *FROM "Product" AS p, (SELECT "id" AS cid FROM "Category" WHERE "lft" BETWEEN 1 AND 792) AS c WHERE p."Category"=c."cid" AND (p."idxfti" @@ to_tsquery('simple', 'vpc')) ORDER BY creationtime ASC OFFSET 0 LIMIT 40 "Limit (cost=180.09..180.09 rows=1 width=738) (actual time=23530.598..23530.730 rows=40 loops=1)" " -> Sort (cost=180.09..180.09 rows=1 width=738) (actual time=23530.593..23530.632 rows=40 loops=1)" " Sort Key: p.creationtime" " Sort Method: top-N heapsort Memory: 68kB" " -> Nested Loop (cost=85.27..180.08 rows=1 width=738) (actual time=10523.533..23530.043 rows=130 loops=1)" " -> Bitmap Heap Scan on "Category" (cost=4.27..10.03 rows=2 width=4) (actual time=0.270..1.688 rows=402 loops=1)" " Recheck Cond: ((lft >= 1) AND (lft <= 792))" " -> Bitmap Index Scan on "Category_lftrgt_idx" (cost=0.00..4.27 rows=2 width=0) (actual time=0.246..0.246 rows=402 loops=1)" " Index Cond: ((lft >= 1) AND (lft <= 792))" " -> Bitmap Heap Scan on "Product" p (cost=81.00..85.01 rows=1 width=734) (actual time=58.512..58.516 rows=0 loops=402)" " Recheck Cond: (("Category" = "Category".id) AND (idxfti @@ '''vpc'''::tsquery))" " -> BitmapAnd (cost=81.00..81.00 rows=1 width=0) (actual time=58.503..58.503 rows=0 loops=402)" " -> Bitmap Index Scan on "Product_Category_idx" (cost=0.00..31.12 rows=371 width=0) (actual time=0.213..0.213 rows=337 loops=402)" " Index Cond: ("Category" = "Category".id)" " -> Bitmap Index Scan on "Product_idxfti_idx" (cost=0.00..49.60 rows=113 width=0) (actual time=62.246..62.246 rows=178 loops=376)" " Index Cond: (idxfti @@ '''vpc'''::tsquery)" "Total runtime: 23531.079 ms" From pstm.spain@gmail.com Wed Jul 4 10:36:06 2012 Received: from magus.postgresql.org (magus.postgresql.org [87.238.57.229]) by mail.postgresql.org (Postfix) with ESMTP id B39AF7FB732 for ; Wed, 4 Jul 2012 07:36:06 -0300 (ADT) Received: from mail-bk0-f46.google.com ([209.85.214.46]) by magus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1SmMwH-0002Oa-BT for pgsql-performance@postgresql.org; Wed, 04 Jul 2012 10:36:05 +0000 Received: by bkwj10 with SMTP id j10so2480187bkw.19 for ; Wed, 04 Jul 2012 03:35:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=message-id:date:from:user-agent:mime-version:cc:subject:references :in-reply-to:content-type:content-transfer-encoding; bh=nPwzZmukkG0Hy9LXsZxkIBrrtn6+BBvhyfK6c1KVI+Q=; b=VFeVNOHC7ksnXqYiNGQzuKIxWUZD3UO1JFAf32Ld7vPQQH7dXn1v3Ips1y96afNjad EadBeWLGQ8NJn3CmTfRcjkBpem8P+WwPg54ic+h5FZe9JY6Kbd5O4AADKvpmu/YAU25s bm/KEoBV/x79x7XNjHfMaCIg/k7EKAqiPOi+iR1yOaObWjJ+d9gIQ83BufzAhWZskl3B IVzt+F6UL6DvE8vB3FVCfTBIdd6FINR32Vms4Av1VsWqhu3koi3A8ILLJm5LMgkSAhyf GHChqUbaIU4L/rCOKGcAMeoWwyBUphOuG/627IQaRlwt/TqdVhFQLKK/ZslpjHwiZ3To PuBQ== Received: by 10.204.148.83 with SMTP id o19mr11266156bkv.96.1341398152228; Wed, 04 Jul 2012 03:35:52 -0700 (PDT) Received: from athlon.ww1.0pc.eu (237.Red-88-28-127.staticIP.rima-tde.net. [88.28.127.237]) by mx.google.com with ESMTPS id u8sm18710665bks.0.2012.07.04.03.35.49 (version=SSLv3 cipher=OTHER); Wed, 04 Jul 2012 03:35:51 -0700 (PDT) Message-ID: <4FF41C83.6040206@gmail.com> Date: Wed, 04 Jul 2012 12:35:47 +0200 From: PV User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:13.0) Gecko/20120605 Thunderbird/13.0 MIME-Version: 1.0 CC: pgsql-performance@postgresql.org Subject: Re: static virtual columns as result? References: <4FF2B1020200002500048D3F@gw.wicourts.gov> In-Reply-To: <4FF2B1020200002500048D3F@gw.wicourts.gov> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: -1.4 (-) X-Archive-Number: 201207/15 X-Sequence-Number: 47196 SOLVED: 1) Try remove old functions tsearch, ltree,..., "old" database format 2) Vacuum or rebuild database One could solve the problem. Regards