AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / dba / 问题 / 264818
Accepted
Rj_N
Rj_N
Asked: 2020-04-12 16:07:04 +0800 CST2020-04-12 16:07:04 +0800 CST 2020-04-12 16:07:04 +0800 CST

为 WHERE COALESCE() 条件创建索引

  • 772

我正在使用 PostgreSQL V9.6.11

表 DDL:

CREATE TABLE test_c ( 
          insrt_prcs_id bigint NOT NULL, 
          updt_prcs_id bigint, src_sys_id integer NOT NULL,
          load_dttm timestamp(6) with time zone NOT NULL, 
          updt_dttm timestamp(6) without time zone);

我试图为index下面的查询创建一个:

SELECT * 
FROM test_c             
WHERE COALESCE(u_dttm,l_dttm) > '2020-04-10 15:29:44.596311-07'
AND   COALESCE(u_dttm,l_dttm) <= '2020-04-11 15:29:44.596311-07' 

创建index为:

create index idx_test_c  on test_c(COALESCE((updt_dttm, load_dttm)))

但是查询计划器没有扫描索引:

EXPLAIN ANALYZE
SELECT *            
FROM test_c             
WHERE COALESCE(u_dttm,l_dttm) > '2020-04-10 15:29:44.596311-07'
AND   COALESCE(u_dttm,l_dttm) <= '2020-04-11 15:29:44.596311-07' 
Seq Scan on test_c as test_c (cost=0..1857.08 rows=207 width=496) (actual=5.203..5.203 rows=0 loops=1)  
Filter: ((COALESCE((test_c.updt_dttm)::timestamp with time zone, test_c.load_dttm) > '2020-04-10 15:29:44.596311-07'::timestamp with time zone) AND (COALESCE((test_c.updt_dttm)::timestamp with time zone, test_c.load_dttm) <= '2020-04-11 15:29:44.596311-07'::timestamp with time zone))
Rows Removed by Filter: 41304

为什么没有发生索引扫描?

postgresql index
  • 1 1 个回答
  • 3604 Views

1 个回答

  • Voted
  1. Best Answer
    Erwin Brandstetter
    2020-04-12T16:30:26+08:002020-04-12T16:30:26+08:00

    去掉不正确的括号对:

    CREATE INDEX idx_test_c ON test_c(COALESCE(updt_dttm, load_dttm));
    

    db<>在这里摆弄

    你拥有它的方式是有效地索引复合值 (updt_dttm, load_dttm),COALESCE而不是做任何事情。

    添加的表定义揭示了您的第二个问题:

    CREATE TABLE test_c ( 
      insrt_prcs_id bigint NOT NULL
    , updt_prcs_id  bigint
    , src_sys_id    integer NOT NULL
    , load_dttm     timestamp(6) with time zone NOT NULL
    , updt_dttm     timestamp(6) without time zone   -- !!!
    );
    

    为什么要为load_dttm和使用不同的数据类型updt_dttm?解决这个问题,第二个问题就消失了。我建议:

    CREATE TABLE test_c ( 
      -- ...
    , load_dttm     timestamp with time zone NOT NULL
    , updt_dttm     timestamp with time zone  -- !!!
    );
    

    为什么?

    你得到这个错误:

    错误:索引表达式中的函数必须标记为 IMMUTABLE

    ..因为COALESCE必须返回一种数据类型。timestamp with time zone( timestamtz) 是“首选类型”,因此updt_dttm timestamp被强制为timestamptz,它使用的函数取决于timezone当前会话的设置,因此不是 IMMUTABLE。并且索引表达式不能涉及非 IMMUTABLE 函数。

    有关的:

    • 当客户端执行时区逻辑时,timestamptz 是首选吗?
    • 在 Rails 和 PostgreSQL 中完全忽略时区

    您可以通过对时区进行硬编码来使索引与原始(损坏的)表设计一起使用 -'Europe/Vienna'在我的示例中:

    CREATE INDEX ON test_c(COALESCE(updt_dttm AT TIME ZONE 'Europe/Vienna', load_dttm));
    

    只是一个概念证明。希望使用该索引的查询必须使用相同的表达式。如果没有必要,不要去那里。改为修复您的表定义。

    • 4

相关问题

  • 我在索引上放了多少“填充”?

  • PostgreSQL 中 UniProt 的生物序列

  • RDBMS 上的“索引”是什么意思?[关闭]

  • 如何在 MySQL 中创建条件索引?

  • PostgreSQL 9.0 Replication 和 Slony-I 有什么区别?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    连接到 PostgreSQL 服务器:致命:主机没有 pg_hba.conf 条目

    • 12 个回答
  • Marko Smith

    如何让sqlplus的输出出现在一行中?

    • 3 个回答
  • Marko Smith

    选择具有最大日期或最晚日期的日期

    • 3 个回答
  • Marko Smith

    如何列出 PostgreSQL 中的所有模式?

    • 4 个回答
  • Marko Smith

    列出指定表的所有列

    • 5 个回答
  • Marko Smith

    如何在不修改我自己的 tnsnames.ora 的情况下使用 sqlplus 连接到位于另一台主机上的 Oracle 数据库

    • 4 个回答
  • Marko Smith

    你如何mysqldump特定的表?

    • 4 个回答
  • Marko Smith

    使用 psql 列出数据库权限

    • 10 个回答
  • Marko Smith

    如何从 PostgreSQL 中的选择查询中将值插入表中?

    • 4 个回答
  • Marko Smith

    如何使用 psql 列出所有数据库和表?

    • 7 个回答
  • Martin Hope
    Jin 连接到 PostgreSQL 服务器:致命:主机没有 pg_hba.conf 条目 2014-12-02 02:54:58 +0800 CST
  • Martin Hope
    Stéphane 如何列出 PostgreSQL 中的所有模式? 2013-04-16 11:19:16 +0800 CST
  • Martin Hope
    Mike Walsh 为什么事务日志不断增长或空间不足? 2012-12-05 18:11:22 +0800 CST
  • Martin Hope
    Stephane Rolland 列出指定表的所有列 2012-08-14 04:44:44 +0800 CST
  • Martin Hope
    haxney MySQL 能否合理地对数十亿行执行查询? 2012-07-03 11:36:13 +0800 CST
  • Martin Hope
    qazwsx 如何监控大型 .sql 文件的导入进度? 2012-05-03 08:54:41 +0800 CST
  • Martin Hope
    markdorison 你如何mysqldump特定的表? 2011-12-17 12:39:37 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 对 SQL 查询进行计时? 2011-06-04 02:22:54 +0800 CST
  • Martin Hope
    Jonas 如何从 PostgreSQL 中的选择查询中将值插入表中? 2011-05-28 00:33:05 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 列出所有数据库和表? 2011-02-18 00:45:49 +0800 CST

热门标签

sql-server mysql postgresql sql-server-2014 sql-server-2016 oracle sql-server-2008 database-design query-performance sql-server-2017

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve