Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1Z3CgA-0006ku-UC for pgsql-performance@arkaria.postgresql.org; Fri, 12 Jun 2015 00:18:39 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1Z3CgA-0001Yj-DT for pgsql-performance@arkaria.postgresql.org; Fri, 12 Jun 2015 00:18:38 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1Z3Cg8-0001Wh-KE for pgsql-performance@postgresql.org; Fri, 12 Jun 2015 00:18:36 +0000 Received: from mail-wi0-x234.google.com ([2a00:1450:400c:c05::234]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84) (envelope-from ) id 1Z3Cg3-00013v-FI for pgsql-performance@postgresql.org; Fri, 12 Jun 2015 00:18:35 +0000 Received: by wifx6 with SMTP id x6so1624414wif.0 for ; Thu, 11 Jun 2015 17:18:30 -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:content-transfer-encoding; bh=lvbOzo0upXjfKzdihNh0MmdpsnaiYs8tEzm33migUKA=; b=cIwL+VRU1qdQ2ivEyFn6P77P1i8Zz6X6jlDrl7qYCllKeIla+RYRiYPMlF5jhguzt4 gdIsS6o+iAIOvy8bzadmIhlgCActy01hpZJfZ4uKQRlVecuZh2sRmC7REA4M8TJtoHPa Mn6DU00IyEnz2Pu40FSZXFteD/KDZMo9Cxz741bIPIVLaVkBjoUS/yLeFVRoqk2Mp7Cw IxUAuxdB3Yajz3/CvDH7PX0mf5ofD1EdO8AvP9cSG4tEV2m0bBeFcUkeWrC5aZefSVwk H5Ja4T6C7/hUFPCekGkk+Z95oGVSXYJlNyehg5UFtNF+xXBJoaYRJD+GpYqersYde3nO vILA== X-Received: by 10.194.97.196 with SMTP id ec4mr21493309wjb.3.1434068310766; Thu, 11 Jun 2015 17:18:30 -0700 (PDT) Received: from [192.168.0.22] (chello062178238165.4.12.vie.surfer.at. [62.178.238.165]) by mx.google.com with ESMTPSA id vy5sm3320729wjc.33.2015.06.11.17.18.30 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 11 Jun 2015 17:18:30 -0700 (PDT) Message-ID: <557A2555.7090807@gmail.com> Date: Fri, 12 Jun 2015 02:18:29 +0200 From: Sasa Vilic User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.7.0 MIME-Version: 1.0 To: pgsql-performance@postgresql.org Subject: Slow query: Postgres chooses nested loop over hash join, whery by hash join is much faster, wrong number of rows estimated Content-Type: text/plain; charset=utf-8; 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-performance Precedence: bulk Sender: pgsql-performance-owner@postgresql.org Hi, I have a query that takes ridiculously long to complete (over 500ms) but if I disable nested loop it does it really fast (24.5ms) Here are links for * first request (everything enabled): http://explain.depesz.com/s/Q1M * second request (nested loop disabled): http://explain.depesz.com/s/9ZY I have also noticed, that setting set join_collapse_limit = 1; produces similar results as when nested loops are disabled. Autovacuumm is running, and I did manually performed both: analyze and vacuumm analyze. No effect. I tried increasing statistics for columns (slot, path_id, key) to 5000 for table data. No effect. I tried increasing statistics for columns (id, parent, key) to 5000 for table path. No effect. I can see, that postgres is doing wrong estimation on request count, but I can't figure it out why. Table path is used to represent tree-like structure. == QUERY == SELECT p1.value as request_type, p2.value as app_id, p3.value as app_ip, p3.id as id, data.*, server.name FROM data INNER JOIN path p3 ON data.path_id = p3.id INNER JOIN server on data.server_id = server.id INNER JOIN path p2 on p2.id = p3.parent INNER JOIN path p1 on p1.id = p2.parent WHERE data.slot between '2015-02-18 00:00:00' and '2015-02-19 00:00:00' AND p1.key = 'request_type' AND p2.key = 'app_id' AND p3.key = 'app_ip' ; == TABLES == Table "public.path" Column | Type | Modifiers | Storage | Description --------+-----------------------+---------------------------------------------------+----------+------------- id | integer | not null default nextval('path_id_seq'::regclass) | plain | parent | integer | | plain | key | character varying(25) | not null | extended | value | character varying(50) | not null | extended | Indexes: "path_pkey" PRIMARY KEY, btree (id) "path_unique" UNIQUE CONSTRAINT, btree (parent, key, value) Foreign-key constraints: "path.fg.parent->path(id)" FOREIGN KEY (parent) REFERENCES path(id) Referenced by: TABLE "data" CONSTRAINT "data_fkey_path" FOREIGN KEY (path_id) REFERENCES path(id) TABLE "path" CONSTRAINT "path.fg.parent->path(id)" FOREIGN KEY (parent) REFERENCES path(id) Has OIDs: no Table "public.data" Column | Type | Modifiers | Storage | Description -----------+--------------------------------+-----------+----------+------------- slot | timestamp(0) without time zone | not null | plain | server_id | integer | not null | plain | path_id | integer | not null | plain | key | character varying(50) | not null | extended | value | real | not null | plain | Indexes: "data_pkey" PRIMARY KEY, btree (slot, server_id, path_id, key) Foreign-key constraints: "data_fkey_path" FOREIGN KEY (path_id) REFERENCES path(id) Has OIDs: no svilic=> select count(*) from path; count ------- 603 svilic=> select count(*) from path p1 inner join path p2 on p1.id = p2.parent inner join path p3 on p2.id = p3.parent where p1.parent is null; count ------- 463 svilic=> select count(*) from server; count ------- 37 svilic=> select count(*) from data; count ---------- 23495552 svilic=> select version(); version ------------------------------------------------------------------------------------------------------------- PostgreSQL 9.1.17 on x86_64-unknown-linux-gnu, compiled by gcc (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3, 64-bit == SERVER CONFIGURATION == shared_buffers = 512MB work_mem = 8MB (I have tried changing it to 32, 128 and 512, no effect) maintenance_work_mem = 64MB checkpoint_segments = 100 random_page_cost = 4.0 effective_cache_size = 3072MB == HARDWARE CONFIGURATION == cpu: Intel(R) Core(TM) i3-2100 CPU @ 3.10GHz (4 cores) mem: 8GB system is using regular disks, (no raid and no ssd) -- Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-performance