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 1jd6u7-0005SK-GF for pgsql-performance@arkaria.postgresql.org; Mon, 25 May 2020 06:48:07 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jd6u6-0004Un-3L for pgsql-performance@arkaria.postgresql.org; Mon, 25 May 2020 06:48:06 +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 1jd6u5-0004Ue-S8 for pgsql-performance@lists.postgresql.org; Mon, 25 May 2020 06:48:05 +0000 Received: from mail-ed1-x544.google.com ([2a00:1450:4864:20::544]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1jd6u2-000447-Ew for pgsql-performance@postgresql.org; Mon, 25 May 2020 06:48:04 +0000 Received: by mail-ed1-x544.google.com with SMTP id be9so14116878edb.2 for ; Sun, 24 May 2020 23:48:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cybertec-at.20150623.gappssmtp.com; s=20150623; h=message-id:subject:from:to:date:in-reply-to:references:user-agent :mime-version:content-transfer-encoding; bh=gFGQQI8FsGInIiF/2hlcbhLxn2076As0lAUkT2EGNnM=; b=GildqIKSAky0bLoHc/EQiWPdB0c0dw6tmfysogHGuRzMqkIfN8x2N6Eyej75pN2RgR d/e926T2QuM9jcCieJr/KDDXnNwberEuwCr0NtFyJIDyXGNZj7tBwRGOTFixubHfXDAe J/GbcDCW+kmkykSXQB9MVqWAtWA8ZP5wT9Xv9n73hKOnbdISeahRXla74wIe69+Ogs19 fMtkMHigGm++ZikK8Q5bbKxUn/KxejGmLBKdKy5YvtjyxPcPzDVfD8JODYoK+6Wr4vSN P3LKQmDHS7FI9Dl7MaVa3wwkGWelIc3QaDR9rf2mPhTP17Jg1AWcN8frO/M5myygbiLu XsXg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:message-id:subject:from:to:date:in-reply-to :references:user-agent:mime-version:content-transfer-encoding; bh=gFGQQI8FsGInIiF/2hlcbhLxn2076As0lAUkT2EGNnM=; b=ZK82euFl+TZfjv88XikYChfidBhpVsiuMJiGPtwmz2Yd0YjMSZr0DnmwnaKZVZmyQx LCQ49C8vbVmuNT5AOt+oZ5QKYso3ag8f3BLZ78KOcmK8IZQ+icZ3rkyH1awXEPbJXsC7 jCnemrDMjqEM9tXQFGcy5S0hdIYA5rvY0DYHn8XoSlpCfILkWzOq197+BPlSTBOD0B+K Mwvb3v6lktyH0pJCDD3I2zIjbKs3jjxVti9Q0yTp+yc3m/ltuGQBqGkSkm09RXb8UxwW 44UoLJfBNUiShRRZOYHqIIQ4sLLuZ4ROr5G2Iteozc5Fd5HwRqD1e+mwPF6SJB6gsjHY gz/w== X-Gm-Message-State: AOAM533UASi7w9oLtU6hF/G9Z5LdCvsOsIrOIGSat0WSqspMLOkoeLFV l+dQLF0ggglDPPU9ZmSWH+KiCQ== X-Google-Smtp-Source: ABdhPJxLEw6TgrQCwsPZK1iPwoCAJRQ5QehEWg5jKwFtSWz5PkOeNpVQ+E+m19RuCMyTWB5B0n0bTQ== X-Received: by 2002:a50:fd0c:: with SMTP id i12mr13471311eds.48.1590389280710; Sun, 24 May 2020 23:48:00 -0700 (PDT) Received: from localhost.localdomain (217-149-175-226.nat.highway.telekom.at. [217.149.175.226]) by smtp.gmail.com with ESMTPSA id be12sm14767502edb.11.2020.05.24.23.47.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 24 May 2020 23:48:00 -0700 (PDT) Message-ID: <5f26f28471cd4d0e9cdcf98d93238c29fb344e8b.camel@cybertec.at> Subject: Re: Request to help on Query improvement suggestion. From: Laurenz Albe To: devchef2020 d , pgsql-admin@postgresql.org, pgsql-general@postgresql.org, pgsql-performance@postgresql.org Date: Mon, 25 May 2020 08:47:59 +0200 In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" User-Agent: Evolution 3.32.5 (3.32.5-1.fc30) MIME-Version: 1.0 Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Fri, 2020-05-22 at 16:15 +0530, devchef2020 d wrote: > PostgreSQL : 9.5.15 > Created Indexes on column parental_path: > ================================= > > CREATE INDEX cable_pair_parental_path_idx > ON SCHEMA.TABLE_NAME > USING btree > (md5(parental_path) COLLATE pg_catalog."default"); > > CREATE INDEX cable_pair_parental_path_idx_fulltext > ON SCHEMA.TABLE_NAME > USING gist > (parental_path COLLATE pg_catalog."default"); > SELECT seq_no + 1 FROM SCHEMA.TABLE_NAME WHERE (parental_path LIKE '%,sheath--' || cable_seq_id || ',%' OR parental_path LIKE 'sheath--' || cable_seq_id || ',%' OR parental_path LIKE '%,sheath--' || > cable_seq_id OR parental_path = 'sheath--' || cable_seq_id) ORDER BY seq_no DESC LIMIT 1; > > Explain Plan: > ============= > > Limit (cost=108111.60..108111.61 rows=1 width=4) (actual time=4597.605..4597.605 rows=0 loops=1) > Output: ((seq_no + 1)), seq_no > Buffers: shared hit=2967 read=69606 dirtied=1 > -> Sort (cost=108111.60..108113.09 rows=595 width=4) (actual time=4597.603..4597.603 rows=0 loops=1) > Output: ((seq_no + 1)), seq_no > Sort Key: TABLE_NAME.seq_no DESC > Sort Method: quicksort Memory: 25kB > Buffers: shared hit=2967 read=69606 dirtied=1 > -> Seq Scan on SCHEMA.TABLE_NAME (cost=0.00..108108.63 rows=595 width=4) (actual time=4597.595..4597.595 rows=0 loops=1) > Output: (seq_no + 1), seq_no > Filter: ((TABLE_NAME.parental_path ~~ '%,sheath--64690,%'::text) OR (TABLE_NAME.parental_path ~~ 'sheath--64690,%'::text) OR (TABLE_NAME.parental_path ~~ '%,sheath--64690'::text) OR > (TABLE_NAME.parental_path = 'sheath--64690'::text)) > Rows Removed by Filter: 1930188 > Buffers: shared hit=2967 read=69606 dirtied=1 An index on an expression can only be used if the expression is exactly the same as on one side of an operator in a WHERE condition. So your only chance with that query is to hope for a bitmap OR with an index on "parental path". Two things to try: 1) CREATE INDEX ON table_name (parental_path text_pattern_ops); 2) CREATE EXTENSION pg_trgm; CREATE INDEX ON table_name USING GIN (parental_path gin_trgm_ops); Yours, Laurenz Albe -- Cybertec | https://www.cybertec-postgresql.com