Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1Tr2lm-0001xq-FM for pgsql-admin@arkaria.postgresql.org; Fri, 04 Jan 2013 08:36:50 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.72) (envelope-from ) id 1Tr2lk-0007zh-RZ for pgsql-admin@arkaria.postgresql.org; Fri, 04 Jan 2013 08:36:48 +0000 Received: from magus.postgresql.org ([87.238.57.229]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1Tr2lj-0007zb-Ue for pgsql-admin@postgresql.org; Fri, 04 Jan 2013 08:36:48 +0000 Received: from honeysuckle.london.02.net ([87.194.255.144]) by magus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1Tr2lf-00057n-Tp for pgsql-admin@postgresql.org; Fri, 04 Jan 2013 08:36:46 +0000 Received: from lenin.edesix.com (93.97.174.104) by honeysuckle.london.02.net (8.5.140) id 50D1042B00505A27 for pgsql-admin@postgresql.org; Fri, 4 Jan 2013 08:36:42 +0000 Received: from localhost ([127.0.0.1] helo=mail.edesix.com) by lenin.edesix.com with esmtpsa (TLSv1:AES256-SHA:256) (Exim 4.76) (envelope-from ) id 1Tr2ld-0003CD-Ca for pgsql-admin@postgresql.org; Fri, 04 Jan 2013 08:36:41 +0000 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Date: Fri, 04 Jan 2013 08:36:41 +0000 From: robin To: Subject: Re: my query is so slow,It costs 944,319.072 ms,Can someone Help!! In-Reply-To: <50E67019.6060908@countrybright.com.cn> References: <50E66B33.3070707@countrybright.com.cn> <50E67019.6060908@countrybright.com.cn> Message-ID: X-Sender: robin@edesix.com User-Agent: Roundcube Webmail/0.7.2 X-Spam-Score: -21.9 (---------------------) X-Spam-Report: lenin.edesix.com has assigned spam score: -21.9 / 5.0 pts rule name description ---- ---------------------- -------------------------------------------------- -20 ALL_TRUSTED Passed through trusted hosts only via SMTP -1.9 BAYES_00 BODY: Bayes spam probability is 0 to 1% [score: 0.0000] X-Pg-Spam-Score: -1.9 (-) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-admin Precedence: bulk Sender: pgsql-admin-owner@postgresql.org I think I am correct in saying that the first (inner most) join is the problem. All the actual time is spent running this nested loop. It does a single sequential scan of the m_transaction table (takes about 1 second) filtering out all the m_transaction records that match your criteria (28936 records). For each of these records it then does a sequential scan of the ap_attribute table looking for records with isactive='Y'. For each such record it joins it to the m_transaction record if the m_product_id in the m_transaction record matches the m_product_id in the ap_attribute record. That produces 28860 records and takes 938 seconds. I suspect that if you had an index on ap_attribute.m_product_id,ap_attribute.isactive then it might go a lot faster (depending on the actual size of the ap_attribute table). Note that I am not 100% sure that the query planner will immediately use the index after you've created it - you might have to run analyze on the ap_attribute table first. Hope this helps, Robin -- Sent via pgsql-admin mailing list (pgsql-admin@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-admin