Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mJ05h-0000iJ-8P for pgsql-performance@arkaria.postgresql.org; Wed, 25 Aug 2021 21:05:45 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1mJ059-0004Ss-Dk for pgsql-performance@arkaria.postgresql.org; Wed, 25 Aug 2021 21:05:11 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mJ058-0004Sk-Qi for pgsql-performance@lists.postgresql.org; Wed, 25 Aug 2021 21:05:10 +0000 Received: from mail-io1-xd2a.google.com ([2607:f8b0:4864:20::d2a]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1mJ051-000262-VX for pgsql-performance@lists.postgresql.org; Wed, 25 Aug 2021 21:05:09 +0000 Received: by mail-io1-xd2a.google.com with SMTP id f6so881849iox.0 for ; Wed, 25 Aug 2021 14:05:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=telsasoft-com.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=RXL8hxQ2VBqdG8+S7O9Saei5JK1D4cddfhujrZ6Stq0=; b=ENOGRbwryWoCO1zX2ksKAHRS7LnHEaWRbxb0iaaRw6kvK49mPI6PeUubVbiw1nH4cZ YuUn+9ln5WmOOTFpc6tyPKqyrgvSEMwV94mJA985Eeo7JXVE+IWDSUuhU1II80keY1Gh DaihXzPRcXO2Cn5xTxAoqthZ70RoQCzRyiESA8f3ZcuPdufFhTfSDIuFDUGkhxSE/jzZ cDhQfgZHXYh7Djyh5Mb9u7X+GM6DpYUkxNryJv2hVfDW+xviNgo498QRMmylegLXuvnY 31hdxz1oYyrnNsSRvXqsjqqFMtQdmflfM+/4WqIM2fkWYYO1eVS9x2AN9PK2jQGSM4n7 unVA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=RXL8hxQ2VBqdG8+S7O9Saei5JK1D4cddfhujrZ6Stq0=; b=tXmIxGiHAuwEw9NOO6HyxOePFSaPDSQXA+qoS2BPAWDv9CiBz4vazZXLnQjbzFpfaJ c/5iixKXKGOhSArCvdg+dGq8FcAn2XsflyVtO7xEehh5zmQsHuAF+60acSYVu8XrtsMH vp8QhTFfGnjF5Wbx7P2SDFvfGzXZs8JQCGm6n36IDdwH1fxq3LAcv80GQpJp1v1jmmD6 sWhQ+gpbvIPs5e7OsTM3I/EpUStfYlLLh3E3PypeAGkKe1DT3oEkQBchv06bFkV9S80i H0Ykp5R8jhzvo40W9KHcfB+2oA5M7ThqcllDY2nMcG7V4dpv9phkHhU69TLrxHtnXwD/ LrYA== X-Gm-Message-State: AOAM532EnIyQlC+lfugvmD9chPdwf/1ySSa0zD1s1d/FilhDzwUgzVPm Z4dER1tguwtvkBQATdg9HrOB3zoRmnTttg== X-Google-Smtp-Source: ABdhPJx8P7+61EodSWMPCL1AMxb2atpcwY6n/qhNXgqN4QuuNg20E2TYvEu+9CrNq2u0zj1yxgMVqA== X-Received: by 2002:a02:ce37:: with SMTP id v23mr351923jar.81.1629925502913; Wed, 25 Aug 2021 14:05:02 -0700 (PDT) Received: from pryzbyj.telsasoft (charmander.telsasoft.com. [50.244.222.1]) by smtp.gmail.com with ESMTPSA id s18sm550503ilp.83.2021.08.25.14.05.02 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 25 Aug 2021 14:05:02 -0700 (PDT) Received: by pryzbyj.telsasoft (Postfix, from userid 1000) id 13480800689; Wed, 25 Aug 2021 16:05:01 -0500 (CDT) Date: Wed, 25 Aug 2021 16:05:00 -0500 From: Justin Pryzby To: Jack Christensen Cc: pgsql-performance@lists.postgresql.org Subject: Re: Using regexp from table has unpredictable poor performance Message-ID: <20210825210500.GF10479@telsasoft.com> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.9.4 (2018-02-28) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Wed, Aug 25, 2021 at 11:47:43AM -0500, Jack Christensen wrote: > I have items that need to be categorized by user defined matching rules. > Trusted users can create rules that include regular expressions. I've > reduced the problem to this example. > I use the following query to find matches: > > select r.id, i.id > from items i > join matching_rules r on i.name ~ r.name_matches; > > When there are few rules the query runs quickly. But as the number of rules > increases the runtime often increases at a greater than linear rate. Maybe it's because the REs are cached by RE_compile_and_cache(), but if you loop over the REs in the inner loop, then the caching is ineffecive. Maybe you can force it to join with REs on the outer loop by writing it as: | rules LEFT JOIN items WHERE rules.id IS NOT NULL, ..to improve performance, or at least test that theory. -- Justin