Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1UFBDd-000612-B7 for pgsql-performance@arkaria.postgresql.org; Mon, 11 Mar 2013 22:29:21 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.72) (envelope-from ) id 1UFBDc-0003Da-PO for pgsql-performance@arkaria.postgresql.org; Mon, 11 Mar 2013 22:29:20 +0000 Received: from makus.postgresql.org ([2001:4800:7903:4::125]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1UFBDa-0003C7-Nd for pgsql-performance@postgresql.org; Mon, 11 Mar 2013 22:29:18 +0000 Received: from smtp2.peak6.com ([38.98.137.9]) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1UFBDX-00009m-IS for pgsql-performance@postgresql.org; Mon, 11 Mar 2013 22:29:18 +0000 X-AuditID: 0a02cb20-b7efd6d000002e53-3b-513e5aba43c9 Received: from sswchi5pcas1.peak6.net (sswchi5pcas1.peak6.net [10.100.13.22]) by smtp2.peak6.com (Symantec Messaging Gateway) with SMTP id 76.65.11859.ABA5E315; Mon, 11 Mar 2013 17:29:14 -0500 (CDT) Received: from [10.10.201.64] (10.10.201.64) by sswchi5pcas1.peak6.net (10.100.13.25) with Microsoft SMTP Server (TLS) id 14.2.247.3; Mon, 11 Mar 2013 17:29:13 -0500 Message-ID: <513E5AB9.4010809@optionshouse.com> Date: Mon, 11 Mar 2013 17:29:13 -0500 From: Shaun Thomas Reply-To: Organization: Peak6 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130221 Thunderbird/17.0.3 MIME-Version: 1.0 To: "pgsql-performance@postgresql.org" Subject: Slow query when used in a view Content-Type: text/plain; charset="ISO-8859-1"; format=flowed content-transfer-encoding: 7bit X-Originating-IP: [10.10.201.64] X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFnrIIsWRmVeSWpSXmKPExsXClcIrprsryi7QYMpRSYvrM88xOzB6XNg3 jT2AMaqB0SYxLy+/JLEkVSEltTjZVsm/oCQzP6/YI7+0OFXBJbM4OScxMze1SEkhM8VWyVhJ oSAnMTk1NzWvxFYpsaAgNS9FyY5LAQPYAJVl5imk5iXnp2TmpdsqeQb761pYmFrqGirZxSOD hE6WjO7L7SwFS4QqLvRMZmlg3M3XxcjJISFgIrGh7y8jhC0mceHeerYuRi4OIYETjBLHT59g hHBWMkrse3ubpYuRg4NXQF9iw5QQkAYWAVWJ31cWMYHYbAK6EpP6V7CB2EICKhJ9Z2+AxfkF hCX2fWhiBrFFBcIkXi7ZAVbDKyAocXLmExYQW0TAU2L2/m1gtrCAusTOl29ZQWxmAVuJC3Ou s0DY8hLb385hhpivKrGieSIbxNEKEv+vvGOawCg4C8nYWUjaZyFpX8DIvIpRvLgsMTkj06yg OLekwEivIDUx20wvL7VkEyMoHplOK+xg3NxjdYhRgINRiYd3Aq9doBBrYllxZe4hRgkOZiUR 3lIHoBBvSmJlVWpRfnxRaU5q8SFGH2B4TGSWEk3OB6aKvJJ4Q2NTA1NLU0NTc1NDIxzCSuK8 zDFWgUIC6cCkkZ2aWpBaBDOOiYNTqoEx5/SWPKXP77aLlTzOVAlI4Vr4V3K9zsL+jBsGOYre SS+BiSnlTk20oMbbPO8fQa93/vXxvpW6cuvkDyxWD/4VaL5oet9d5PY5Ivlc20TWOvvlSfKa t7OfVl+8bqPw3jnc6r/n18uSqcHTljf9b+KYZTznSJ+SnIKauMCeL/yXvaMrnps2fVBiKc5I NNRiLipOBADLHwbX9AIAAA== X-Pg-Spam-Score: -1.9 (-) 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 Hey everyone! A developer was complaining about a view he created to abstract an added column in a left join. He was contemplating denormalizing the added value into the parent table and using a trigger to maintain it instead, and I obviously looked into the problem. I noticed the view was incurring a sequence scan on an obvious index condition, but the regular join version was not. Curious, I whipped up this test case: CREATE TABLE foo (id BIGINT, small_label VARCHAR); INSERT INTO foo (id) VALUES (generate_series(1, 10000)); ALTER TABLE foo ADD CONSTRAINT pk_foo_id PRIMARY KEY (id); CREATE TABLE bar (id BIGINT, foo_id BIGINT); INSERT INTO bar (id, foo_id) SELECT a, a%10000 FROM generate_series(1, 100000) a; ALTER TABLE bar ADD CONSTRAINT pk_bar_id PRIMARY KEY (id); CREATE TABLE tiny_foo (small_label VARCHAR NOT NULL PRIMARY KEY); INSERT INTO tiny_foo (small_label) VALUES (('yes', 'we', 'have', 'no', 'bananas')); UPDATE foo SET small_label = 'bananas' WHERE id=750; ANALYZE foo; ANALYZE bar; ANALYZE tiny_foo; CREATE VIEW v_slow_view AS SELECT foo.*, tf.small_label IS NOT NULL AS has_small_label FROM foo LEFT JOIN tiny_foo tf USING (small_label); Now, this is with PostgreSQL 9.1.8, basically default everything in a base Ubuntu install. So, the good query plan using all tables directly: SELECT bar.*, foo.*, tf.small_label IS NOT NULL AS has_small_label FROM bar LEFT JOIN foo ON (foo.id = bar.foo_id) LEFT JOIN tiny_foo tf USING (small_label) WHERE bar.id IN (750, 1750, 2750) ORDER BY bar.id; does this: Index Scan using pk_foo_id on foo (cost=0.00..8.27 rows=1 width=16) Index Cond: (id = bar.foo_id) The bad one using the view: SELECT bar.*, sv.* FROM bar LEFT JOIN v_slow_view sv ON (sv.id = bar.foo_id) WHERE bar.id IN (750, 1750, 2750) ORDER BY bar.id; Mysteriously, does this: Seq Scan on foo (cost=0.00..145.00 rows=10000 width=16) I'm... perplexed. This test case is way too shallow to be affected by join_collapse_limit and its ilk, so I'm not sure what's going on here. I sense an optimization fence, but I can't see where. Thanks in advance! -- Shaun Thomas OptionsHouse | 141 W. Jackson Blvd. | Suite 500 | Chicago IL, 60604 312-676-8870 sthomas@optionshouse.com ______________________________________________ See http://www.peak6.com/email_disclaimer/ for terms and conditions related to this email -- Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-performance