Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1ty8yo-002z7e-V1 for pgsql-general@arkaria.postgresql.org; Fri, 28 Mar 2025 12:38:35 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1ty8yn-009PIP-D4 for pgsql-general@arkaria.postgresql.org; Fri, 28 Mar 2025 12:38:33 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1ty8ym-009PF0-Qu for pgsql-general@lists.postgresql.org; Fri, 28 Mar 2025 12:38:32 +0000 Received: from smtp.evolu-s.it ([77.81.232.174] helo=mail.evolu-s.it) by makus.postgresql.org with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1ty8yk-001ceF-06 for pgsql-general@postgresql.org; Fri, 28 Mar 2025 12:38:31 +0000 dkim-signature: v=1; a=rsa-sha256; d=evolu-s.it; s=dkim; c=relaxed/relaxed; q=dns/txt; h=From:Subject:Date:Message-ID:To:MIME-Version:Content-Type; bh=3jHBb7LqOhUdrj/DAZZ6l/w6bQ1dqIcmydLP2CgXDRI=; b=djJkOS6e2z8EqKzIkGTMx3myIYqXZa7/tXMztvbiYHP9M8LbJMSlXgJZGqrzbR7B5O5oFqCU8lQRSPOrzKSXLtAxhRujqD8vKvpMj4LAIhqHRla6PNdsclLMs0LkD4jeUaY2Ts6cd3ufVc/T89QxS1f4P8/TA53NcesNHO33fHo= Received: from [192.168.1.103] (res-129401d.ppp.twt.it [83.217.179.105]) by mail.evolu-s.it with ESMTPSA (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128) ; Fri, 28 Mar 2025 13:38:27 +0100 Content-Type: multipart/alternative; boundary="------------t387KcTYc1B9oYVJyesVQ3kT" Message-ID: Date: Fri, 28 Mar 2025 13:38:26 +0100 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Content-Language: en-US, it To: PostgreSQL mailing lists From: Moreno Andreo Subject: BTREE index: field ordering List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk This is a multi-part message in MIME format. --------------t387KcTYc1B9oYVJyesVQ3kT Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Hi, Postgres 16.4 (planning to go on 17.4) I'm creating some indexes based on some slow query reported by logs. These queries involve a WHERE with more than 5 fields, that are matching by =, <>, LIKE and IN() I read that equality fields must be first, then the others. Is it correct? Based on this query SELECT COUNT(id) AS total                 FROM nx.tbl1                 WHERE             (date_order >= '2025-03-21')             AND (date_order <= '2025-03-29')             AND (flag = TRUE)             AND ((               -- (flag = TRUE)               -- AND (((tipo <> 'C') AND (tipo <> 'V') AND (tipo <> 'F')) OR (tipo IS NULL) OR (tipo = ''))               (((op <> 'C') OR (op IS NULL)) OR (tipo = 'F'))               AND (s_state IN ('ENQ','WFR','BLQ','BLR','WFA','FRW','FRO','0000','0001'))               AND (tiporic IS NOT NULL)               AND (tiporic NOT LIKE '%cart%')             ) OR (               (tiporic LIKE '%cart%') AND (S_state <> 'CON') AND (s_state <> '0002') AND ((op <> 'C') OR (op IS NULL))             ))  AND (priv IS NULL OR priv = false OR (priv = true and idpriv = 'TEST'))); Should the following index be correct? CREATE INDEX IF NOT EXISTS tbl1_count_idx on nx.tbl1 USING BTREE(flag, tipo, op, priv, idpriv, date_order, s_state, tiporic); Would it be better to create a separate GIN/GIST index for the field matched with LIKE? Thanks in advance, Moreno --------------t387KcTYc1B9oYVJyesVQ3kT Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: 8bit
Hi,
Postgres 16.4 (planning to go on 17.4)
I'm creating some indexes based on some slow query reported by logs.
These queries involve a WHERE with more than 5 fields, that are matching by =, <>, LIKE and IN()
I read that equality fields must be first, then the others.
Is it correct?

Based on this query

SELECT COUNT(id) AS total
                FROM nx.tbl1
                WHERE
            (date_order >= '2025-03-21')
            AND (date_order <= '2025-03-29')
            AND (flag = TRUE)
            AND ((
              -- (flag = TRUE)
              -- AND (((tipo <> 'C') AND (tipo <> 'V') AND (tipo <> 'F')) OR (tipo IS NULL) OR (tipo = ''))
              (((op <> 'C') OR (op IS NULL)) OR (tipo = 'F'))
              AND (s_state IN ('ENQ','WFR','BLQ','BLR','WFA','FRW','FRO','0000','0001'))
              AND (tiporic IS NOT NULL)
              AND (tiporic NOT LIKE '%cart%')
            ) OR (
              (tiporic LIKE '%cart%') AND (S_state <> 'CON') AND (s_state <> '0002') AND ((op <> 'C') OR (op IS NULL))
            ))  AND (priv IS NULL OR priv = false OR (priv = true and idpriv = 'TEST')));

Should the following index be correct?

CREATE INDEX IF NOT EXISTS tbl1_count_idx on nx.tbl1 USING BTREE(flag, tipo, op, priv, idpriv, date_order, s_state, tiporic);

Would it be better to create a separate GIN/GIST index for the field matched with LIKE?

Thanks in advance,
Moreno
--------------t387KcTYc1B9oYVJyesVQ3kT--