X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id B1DE75281A for ; Fri, 29 Jul 2005 13:50:07 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 86050-10 for ; Fri, 29 Jul 2005 16:49:54 +0000 (GMT) Received: from mail1.fluidhosting.com (mail1.fluidhosting.com [204.14.90.61]) by svr1.postgresql.org (Postfix) with SMTP id 584A852B1D for ; Fri, 29 Jul 2005 13:49:53 -0300 (ADT) Received: (qmail 8574 invoked by uid 399); 29 Jul 2005 16:49:52 -0000 Received: from 194-185-112-82.f5.ngi.it (HELO ?10.20.20.4?) (194.185.112.82) by mail1.fluidhosting.com with SMTP; 29 Jul 2005 16:49:52 -0000 Message-ID: <42EA5E64.6050209@beccati.com> Date: Fri, 29 Jul 2005 18:50:44 +0200 From: Matteo Beccati User-Agent: Mozilla Thunderbird 1.0.2 (Windows/20050317) X-Accept-Language: en-us, en MIME-Version: 1.0 To: pgsql-performance@postgresql.org Subject: ltree <@ operator selectivity causes very slow plan Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0 tagged_above=0 required=5 tests= X-Spam-Level: X-Archive-Number: 200507/473 X-Sequence-Number: 13714 Hi, I'm happily using ltree since a long time, but I'm recently having troubles because of ltree <@ operator selectivity that is causing very bad planner choices. An example of slow query is: SELECT batch_id, b.t_stamp AS t_stamp, objects, CASE WHEN sent IS NULL THEN gw_batch_sent(b.batch_id) ELSE sent END AS sent FROM gw_users u JOIN gw_batches b USING (u_id) WHERE u.tree <@ '1041' AND b.t_stamp >= 'today'::date - '7 days'::interval AND b.t_stamp < 'today' ORDER BY t_stamp DESC; I've posted the EXPLAIN ANALYZE output here for better readability: http://rafb.net/paste/results/NrCDMs50.html As you may see, disabling nested loops makes the query lightning fast. The problem is caused by the fact that most of the records of gw_users match the "u.tree <@ '1041'" condition: SELECT COUNT(*) FROM gw_users; count ------- 5012 SELECT COUNT(*) FROM gw_users WHERE tree <@ '1041'; count ------- 4684 Is there anything I can do apart from disabling nested loops? Best regards -- Matteo Beccati http://phpadsnew.com http://phppgads.com