Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id E4C01B5DBEE for ; Tue, 25 Oct 2011 13:22:55 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.29]) (amavisd-maia, port 10024) with ESMTP id 56740-01 for ; Tue, 25 Oct 2011 16:22:49 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.8.0-rc2 Received: from ns210619.ovh.net (ns210619.ovh.net [188.165.206.83]) by mail.postgresql.org (Postfix) with ESMTP id AF865B5DC6A for ; Tue, 25 Oct 2011 13:22:49 -0300 (ADT) Received: from localhost (localhost.localdomain [127.0.0.1]) by ns210619.ovh.net (Postfix) with ESMTP id 265483D1E429 for ; Tue, 25 Oct 2011 18:24:46 +0200 (CEST) X-Virus-Scanned: Debian amavisd-new at ns210619.ovh.net Received: from ns210619.ovh.net ([127.0.0.1]) by localhost (ns210619.ovh.net [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 4Z8QZSe7AE6d for ; Tue, 25 Oct 2011 18:24:41 +0200 (CEST) Received: from [192.168.1.4] (119.87.220.87.dynamic.jazztel.es [87.220.87.119]) by ns210619.ovh.net (Postfix) with ESMTPA id 202773D1E421 for ; Tue, 25 Oct 2011 18:24:41 +0200 (CEST) Message-ID: <4EA6E252.6030002@linos.es> Date: Tue, 25 Oct 2011 18:22:42 +0200 From: Linos User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:7.0.1) Gecko/20111001 Thunderbird/7.0.1 MIME-Version: 1.0 To: pgsql-performance@postgresql.org Subject: CTE vs Subquery Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.9 tagged_above=-10 required=5 tests=BAYES_00=-1.9 X-Spam-Level: X-Archive-Number: 201110/209 X-Sequence-Number: 45431 Hi all, i am having any problems with performance of queries that uses CTE, can the join on a CTE use the index of the original table?, suppose two simple tables: CREATE TABLE employee ( emp_id integer NOT NULL, name character varying NOT NULL, CONSTRAINT employee_pkey PRIMARY KEY (emp_id ) ); CREATE TABLE employee_telephone ( emp_id integer NOT NULL, phone_type character varying NOT NULL, phone_number character varying NOT NULL, CONSTRAINT employee_telephone_pkey PRIMARY KEY (emp_id , phone_type ), CONSTRAINT employee_telephone_emp_id_fkey FOREIGN KEY (emp_id) REFERENCES employee (emp_id) ); and this two queries, i know this particular case don't need either a CTE or subquery it is only an example: WITH phones AS (SELECT emp_id, phone_number ORDER BY emp_id, phone_type) SELECT emp.emp_id, emp.name, array_to_string(array_agg(phones.phone_number)) AS phones FROM employee AS emp JOIN phones ON phones.emp_id = emp.emp_id VS SELECT emp.emp_id, emp.name, array_to_string(array_agg(phones.phone_number)) AS phones FROM employee AS emp JOIN (SELECT emp_id, phone_number ORDER BY emp_id, phone_type) AS phones ON phones.emp_id = emp.emp_id Why the CTE it is slower in many cases? does the CTE don't use the index for the join and the subquery do? if the CTE it is usually slower where should be used instead of a subquery other than recursive CTE's? Regards, Miguel Angel.