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 1nFuk1-00022N-TG for pgsql-performance@arkaria.postgresql.org; Fri, 04 Feb 2022 09:18:53 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1nFuk0-0005Lx-JM for pgsql-performance@arkaria.postgresql.org; Fri, 04 Feb 2022 09:18:52 +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 1nFuk0-0005HQ-46 for pgsql-performance@lists.postgresql.org; Fri, 04 Feb 2022 09:18:52 +0000 Received: from mail-wm1-x333.google.com ([2a00:1450:4864:20::333]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1nFujv-0006tF-5o for pgsql-performance@lists.postgresql.org; Fri, 04 Feb 2022 09:18:50 +0000 Received: by mail-wm1-x333.google.com with SMTP id n12-20020a05600c3b8c00b0034eb13edb8eso4960922wms.0 for ; Fri, 04 Feb 2022 01:18:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cybertec-at.20210112.gappssmtp.com; s=20210112; h=message-id:subject:from:to:date:in-reply-to:references:user-agent :mime-version:content-transfer-encoding; bh=FM0k1t8nBIuOLUAqw8v1UlpzF4WvPE9SqAVz3ClV+7I=; b=vFo/80hjvwtyPtuvzXk5Cov6ZTEB4lh9j3IISUao3YeDJSjOhIfD/L/irITwOlSKPs /0/zzSkjlDpGBA3BHqZG5pnZ39/Vhiw0HMEMdnjMW71BdHgvgJ6pR/3DQSuVGxG94AZL Sem19p0G3elP2KEB1ngOkAfVdFlof5nlYFUHIbK/blvwejuJnZqJ/xGnnE8oHRUm3Hbk fy2F0pZWxcsuHp/0x4OJTswlX6JFCVcOeIpxiUZQ9H+fLTTnzAmjYAchytohaRGmyOvS LDIQspKBzZd50MM/waY9e+46eNcdSiB03mNlaHaI08V1Gx8JgdBPh0+98Za2aufjfemL rORw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:subject:from:to:date:in-reply-to :references:user-agent:mime-version:content-transfer-encoding; bh=FM0k1t8nBIuOLUAqw8v1UlpzF4WvPE9SqAVz3ClV+7I=; b=o3+k1RkQxzcQQUVjzIC1eB0YfZupmDhAO9nirLCyVqoh1XBt6+3ANLVza6txH+lU9d uMialHS9JX8NaHFH44UATMAK+HSYB316dWnA4ldinmvw/V8kQQj1K84Z5sUxbna/TpL4 lDDzDmdJPJ98R7gou/LArhvUIN3NPUIfvuNSMo+7K8gWxCbMuzxRnV2q39VJn42GP730 p4cyUOU/ZiI24xi1hKmONJdhc8SxSTQ2Y9r7pTvO39N4XwZh/CEcfchwyKu2+bf7kZFL 1LK4MQmLGma/7D6urFYw1hNgzpgYCs+4Ee3bUzIAU39gDVd+bN5cqlYbfLJme+hEOpi0 1PxQ== X-Gm-Message-State: AOAM531tc5TcFw8DTFCqL9lgKuaPKg097RaCM/eTGAUbKq64THV87cY6 jMfuCVdvzbqrz+TU1vv1tBqWFw== X-Google-Smtp-Source: ABdhPJyx0c9242VuPZUeQVnfnQLLnbW8qblPHjmZ7o5wDGmkIYrVYLoKzQJZmswvJtuFgjWigWfFtA== X-Received: by 2002:a05:600c:20d3:: with SMTP id y19mr1429066wmm.1.1643966324554; Fri, 04 Feb 2022 01:18:44 -0800 (PST) Received: from localhost.localdomain ([2001:871:5e:1a8c:4743:4146:831e:cbd9]) by smtp.gmail.com with ESMTPSA id l24sm10478583wms.24.2022.02.04.01.18.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Feb 2022 01:18:44 -0800 (PST) Message-ID: <64f14cb61b82fc8bd2d03963d03b7da252b1ba59.camel@cybertec.at> Subject: Re: Terribly slow query with very good plan? From: Laurenz Albe To: Les , pgsql-performance@lists.postgresql.org Date: Fri, 04 Feb 2022 10:18:43 +0100 In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" User-Agent: Evolution 3.40.4 (3.40.4-2.fc34) MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Fri, 2022-02-04 at 10:11 +0100, Les wrote: > EXPLAIN (ANALYZE, COSTS, VERBOSE, BUFFERS) > > select id, title, >  (exists (select f2.id from media.oo_file f2 where f2.relpath like f.relpath || '%')) as has_file > from media.oo_folder f where f.parent_id is null > > QUERY PLAN                                                                                                                                                    | > --------------------------------------------------------------------------------------------------------------------------------------------------------------+ > Index Scan using oo_folder_idx_parent on media.oo_folder f  (cost=0.42..488.02 rows=20 width=26) (actual time=713.419..25414.969 rows=45 loops=1)             | >   Output: f.id, f.title, (SubPlan 1)                                                                                                                          | >   Index Cond: (f.parent_id IS NULL)                                                                                                                           | >   Buffers: shared hit=7014170                                                                                                                                 | >   SubPlan 1                                                                                                                                                   | >     ->  Index Only Scan using oo_file_idx_relpath on media.oo_file f2  (cost=0.55..108499.27 rows=5381 width=0) (actual time=564.756..564.756 rows=0 loops=45)| >           Filter: (f2.relpath ~~ (f.relpath || '%'::text))                                                                                                    | >           Rows Removed by Filter: 792025                                                                                                                      | >           Heap Fetches: 768960                                                                                                                                | >           Buffers: shared hit=7014130                                                                                                                         | > Planning Time: 0.361 ms                                                                                                                                       | > Execution Time: 25415.088 ms                                                                                                                                  | > > It also returns 45 rows, but in 25 seconds which is unacceptable. You should create an index that supports LIKE; for example CREATE INDEX ON media.oo_file (relpath COLLATE "C"); Yours, Laurenz Albe -- Cybertec | https://www.cybertec-postgresql.com