Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1YcyZG-0001VW-4m for pgsql-performance@arkaria.postgresql.org; Tue, 31 Mar 2015 15:59:06 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1YcyZF-00060P-Jd for pgsql-performance@arkaria.postgresql.org; Tue, 31 Mar 2015 15:59:05 +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 1YcyZE-00060H-Es for pgsql-performance@postgresql.org; Tue, 31 Mar 2015 15:59:04 +0000 Received: from mail-wi0-f169.google.com ([209.85.212.169]) by magus.postgresql.org with esmtps (TLS1.2:RSA_AES_256_CBC_SHA1:256) (Exim 4.80) (envelope-from ) id 1YcyZA-0008VK-A2 for pgsql-performance@postgresql.org; Tue, 31 Mar 2015 15:59:03 +0000 Received: by wixo5 with SMTP id o5so19193201wix.1 for ; Tue, 31 Mar 2015 08:58:58 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:references:in-reply-to:subject:date :message-id:mime-version:content-type:content-transfer-encoding :thread-index:content-language; bh=hKJo7h7sg6Qbjd0a0uGh0ditPSZM+6o00IibK2cZMjU=; b=SKh2AuL3BeynXHEoMQcqtHluICc1FiQBdcD157poFdx/9BLWoztrZJDUcH3B+dN6zR 0P5UJg2s6z2yw/eeFTAMBE25K7mB+RLoWUxDNoAu3FFTFJvG2hazPmUSBrD3Zvxxu9Cj AYTnYtCU/Sx5CZ46eptJvkQEuzZlzreoW0m9c1RMMa//85EzTKWdPmvBWGjG4ZsYGkbI V2AHKAYVvJNWpEW47FspSv2yWFuVJYWrn38bi6PD+C8Jy/pm5x3IkPmGH9mvQ9pp4X+j usJ/kjlSwT3Jf9bEJv6DmWZj8SU2dkhFugbDngKCwY7UDteYKfK+DsQf/lH0kXarAiXE hvFw== X-Gm-Message-State: ALoCoQm1wK3GG4ZNFqoG3c/qNBYusLVH/1po/AS8yVgKq81aPUKKhNwBDds3GbN3Ic7pA4H022Jy X-Received: by 10.180.78.135 with SMTP id b7mr73741wix.65.1427817538726; Tue, 31 Mar 2015 08:58:58 -0700 (PDT) Received: from RI02980T440p (jh.rocket-internet.de. [217.110.53.72]) by mx.google.com with ESMTPSA id md2sm21501835wic.19.2015.03.31.08.58.57 (version=TLSv1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 31 Mar 2015 08:58:57 -0700 (PDT) From: "Kevin Viraud" To: "'Tom Lane'" Cc: References: <00a001d06b90$2d1bbe80$87533b80$@rocket-internet.de> <16234.1427810310@sss.pgh.pa.us> In-Reply-To: <16234.1427810310@sss.pgh.pa.us> Subject: Re: Weird CASE WHEN behaviour causing query to be suddenly very slow Date: Tue, 31 Mar 2015 17:58:57 +0200 Message-ID: <00d301d06bcb$997cf220$cc76d660$@rocket-internet.de> MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable X-Mailer: Microsoft Outlook 15.0 Thread-Index: AQECQJ6lxjgMF7czE/gsntwULz0zEwHZauBnnsQ1RSA= Content-Language: de X-Pg-Spam-Score: -2.6 (--) 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 Touche ! Thanks a lot. Looking more at the data yes it goes very often to ELSE Clause. And therefore reaching the MAX_CACHED_RES.=20 In there anyway to increase that value ? Basically, I have several tables containing millions of rows and let say 5 columns. Those five columns, depending of their combination give me a 6th value.=20=20 We have complex patterns to match and using simple LIKE / EQUAL and so on wouldn't be enough. This can be applied to N number of table so we refactored this process into a function that we can use in the SELECT statement, by giving only the 5 values each time. I wouldn't mind using a table and mapping it through a join if it were for my own use.=20 But the final query has to be readable and usable for almost-non-initiated SQL user... So using a function with encapsulated case when seemed to be a good idea and so far worked nicely.=20 But we might consider changing it if we have no other choice... Regards, Kevin -----Original Message----- From: Tom Lane [mailto:tgl@sss.pgh.pa.us]=20 Sent: Dienstag, 31. M=E4rz 2015 15:59 To: Kevin Viraud Cc: pgsql-performance@postgresql.org Subject: Re: [PERFORM] Weird CASE WHEN behaviour causing query to be suddenly very slow "Kevin Viraud" writes: > I have an issue with a rather large CASE WHEN and I cannot figure out=20 > why it is so slow... Do all the arms of the CASE usually fail, leaving you at the ELSE? I suspect what's happening is that you're running into the MAX_CACHED_RES limit in src/backend/utils/adt/regexp.c, so that instead of just compiling each regexp once and then re-using 'em, the regexps are constantly falling out of cache and then having to be recompiled. They'd have to be used in a nearly perfect round robin in order for the behavior to have such a big cliff as you describe, though. In this CASE structure, that suggests that you're nearly always testing every regexp because they're all failing. I have to think there's probably a better way to do whatever you're trying to do, but there's not enough info here about your underlying goal to suggest a better approach. At the very least, if you need a many-armed CASE, it behooves you to make sure the common cases appear early. regards, tom lane --=20 Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-performance