Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1Xe6Eg-0003Ka-B4 for pgsql-bugs@arkaria.postgresql.org; Tue, 14 Oct 2014 17:50:14 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1Xe6Ef-0000El-4Q for pgsql-bugs@arkaria.postgresql.org; Tue, 14 Oct 2014 17:50:13 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1XdyAE-0002oB-VR for pgsql-bugs@postgresql.org; Tue, 14 Oct 2014 09:13:07 +0000 Received: from wrigleys.postgresql.org ([204.145.124.246]) by magus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1XdyAB-00059p-GK for pgsql-bugs@postgresql.org; Tue, 14 Oct 2014 09:13:05 +0000 MIME-Version: 1.0 Subject: BUG #11671: UNACCENT call with constant arg evaluated for each row To: pgsql-bugs@postgresql.org From: t.chaumeny@gmail.com Date: Tue, 14 Oct 2014 09:12:21 +0000 Message-ID: <20141014091221.25461.16786@wrigleys.postgresql.org> X-Pg-Spam-Score: -0.3 (/) Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 8bit List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org The following bug has been logged on the website: Bug reference: 11671 Logged by: Thomas Email address: t.chaumeny@gmail.com PostgreSQL version: 9.3.5 Operating system: OS X 10.9.4 Description: Hi, I noticed that filtering on a constant condition using UNACCENT lead to significantly slower queries than expected : 1 - First query without UNACCENT: EXPLAIN ANALYZE SELECT first_name, last_name FROM user WHERE first_name = 'a'; QUERY PLAN ------------------------------------------------------------------------------------------------------------ Seq Scan on user (cost=0.00..2509.61 rows=1 width=32) (actual time=19.814..19.814 rows=0 loops=1) Filter: ((first_name)::text = 'a'::text) Rows Removed by Filter: 31409 Total runtime: 19.870 ms (4 rows) 2 - Second with UNACCENT (>4 times slower): mydb=# EXPLAIN ANALYZE SELECT first_name, last_name FROM user WHERE first_name = UNACCENT('a'); QUERY PLAN ------------------------------------------------------------------------------------------------------------ Seq Scan on user (cost=0.00..2588.14 rows=1 width=32) (actual time=88.913..88.913 rows=0 loops=1) Filter: ((first_name)::text = unaccent('a'::text)) Rows Removed by Filter: 31409 Total runtime: 88.969 ms (4 rows) Time: 89,767 ms 3 - Third with "SELECT UNACCENT(...)" — which seems to force constant evaluation : mydb=# EXPLAIN ANALYZE SELECT first_name, last_name FROM user WHERE first_name = (SELECT UNACCENT('a')); QUERY PLAN ------------------------------------------------------------------------------------------------------------ Seq Scan on user (cost=0.01..2509.62 rows=1 width=32) (actual time=16.875..16.875 rows=0 loops=1) Filter: ((first_name)::text = $0) Rows Removed by Filter: 31409 InitPlan 1 (returns $0) -> Result (cost=0.00..0.01 rows=1 width=0) (actual time=0.024..0.025 rows=1 loops=1) Total runtime: 16.995 ms (6 rows) Since UNACCENT function is defined as STABLE, I would expect its result to be evaluated only once in the second query. -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs