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 / 问题 / 179495
Accepted
Ian Timothy
Ian Timothy
Asked: 2017-07-11 10:00:35 +0800 CST2017-07-11 10:00:35 +0800 CST 2017-07-11 10:00:35 +0800 CST

在 WHERE 子句中使用 NULLIF() 更改计划器

  • 772

在 9.6.3 中,在 WHERE 子句中添加 NULLIF() 会将计划从位图堆扫描更改为索引扫描,并将执行时间从大约 0.3 毫秒更改为 43,000 毫秒。

这是一个相关的问题:https ://stackoverflow.com/questions/21062148/how-to-query-postgres-on-optional-params

这是快速查询和计划:

SELECT
        c.name_first, c.name_last, c.postal_code,
        v.tag as plate_number, v.tag_state as plate_state, v.year, v.make, v.model
FROM customers_vehicles AS v
        JOIN customers AS c ON (v.customer_id = c.id)
WHERE ('XXXXXX'::text is null OR v.tag='XXXXXX');
┌───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│                                                                  QUERY PLAN                                                                   │
├───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┤
│ Nested Loop  (cost=824.67..451863.32 rows=43830 width=228) (actual time=0.202..0.203 rows=1 loops=1)                                          │
│   ->  Bitmap Heap Scan on customers_vehicles v  (cost=824.12..121946.77 rows=43830 width=164) (actual time=0.104..0.104 rows=1 loops=1)       │
│         Recheck Cond: (tag = 'XXXXXX'::text)                                                                                                  │
│         Heap Blocks: exact=1                                                                                                                  │
│         ->  Bitmap Index Scan on customers_vehicles_tag_idx  (cost=0.00..813.16 rows=43830 width=0) (actual time=0.090..0.090 rows=1 loops=1) │
│               Index Cond: (tag = 'XXXXXX'::text)                                                                                              │
│   ->  Index Scan using customers_pkey on customers c  (cost=0.56..7.52 rows=1 width=128) (actual time=0.093..0.094 rows=1 loops=1)            │
│         Index Cond: (id = v.customer_id)                                                                                                      │
│ Planning time: 0.268 ms                                                                                                                       │
│ Execution time: 0.256 ms                                                                                                                      │
└───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘

这是缓慢的查询和计划:

SELECT
        c.name_first, c.name_last, c.postal_code,
        v.tag as plate_number, v.tag_state AS plate_state, v.year, v.make, v.model
FROM customers_vehicles AS v
        JOIN customers as c on (v.customer_id = c.id)
WHERE (nullif('XXXXXX'::text,'') IS NULL OR v.tag='XXXXXX');
┌──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│                                                                                    QUERY PLAN                                                                                    │
├──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┤
│ Nested Loop  (cost=1.12..7509681.67 rows=87441 width=228) (actual time=5462.111..42209.513 rows=1 loops=1)                                                                       │
│   ->  Index Scan using customers_vehicles_customer_id_idx on customers_vehicles v  (cost=0.56..6921180.68 rows=87441 width=164) (actual time=5462.023..42209.423 rows=1 loops=1) │
│         Filter: ((NULLIF('XXXXXX'::text, ''::text) IS NULL) OR (tag = 'XXXXXX'::text))                                                                                             │
│         Rows Removed by Filter: 8766005                                                                                                                                          │
│   ->  Index Scan using customers_pkey on customers c  (cost=0.56..6.72 rows=1 width=128) (actual time=0.080..0.081 rows=1 loops=1)                                               │
│         Index Cond: (id = v.customer_id)                                                                                                                                         │
│ Planning time: 0.209 ms                                                                                                                                                          │
│ Execution time: 42209.549 ms                                                                                                                                                     │
└──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘

唯一的区别在于 WHERE 子句中的慢查询:'XXXXXX' IS NULL更改为nullif('XXXXXX','') IS NULL. 如果第一个条件包含 ,规划器似乎无法优化它NULLIF()。有没有办法强迫它这样做?

postgresql
  • 1 1 个回答
  • 764 Views

1 个回答

  • Voted
  1. Best Answer
    John Eisbrener
    2017-07-11T10:05:13+08:002017-07-11T10:05:13+08:00

    尝试将谓词更改为以下内容:

    WHERE ('XXXXXX'::text is null OR v.tag='XXXXXX' OR 'XXXXXX' = '');

    因为从逻辑的角度来看,这实际上是该NULLIF语句正在做的事情。我假设'XXXXXX'在这种情况下是一列,而不仅仅是一个随机字符串,X对吗?

    • 0

相关问题

  • 我可以在使用数据库后激活 PITR 吗?

  • 运行时间偏移延迟复制的最佳实践

  • 存储过程可以防止 SQL 注入吗?

  • PostgreSQL 中 UniProt 的生物序列

  • 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