【问题标题】:Why postgresql use 100% cpu when i put in ANY more then ~15000 parameters为什么当我输入超过 ~15000 个参数时 postgresql 使用 100% cpu
【发布时间】:2019-01-14 13:36:53
【问题描述】:

这是我的查询:select * from dbname where user_name = any(cast(? as text[]))

我的配置文件:

max_connections = 100
shared_buffers = 1GB
effective_cache_size = 3GB
maintenance_work_mem = 256MB
checkpoint_completion_target = 0.7
wal_buffers = 16MB
default_statistics_target = 100
random_page_cost = 4
effective_io_concurrency = 2
work_mem = 300MB
min_wal_size = 512MB
max_wal_size = 2GB

当我放少于 ~15000 时,一切正常。

Postgresq 9.5.

【问题讨论】:

  • 为什么您将 15K 参数放入 ANY 子句中?这些值不应该在某处的表格中吗?我的投票是修复您的数据模型/查询。
  • 我需要知道数据库中已经包含哪些对象,以及我需要在一个查询中添加的其他内容比检查每个对象要快得多。也许您可以建议如何更好更快地做到这一点。
  • @a_horse_with_no_name 很棒的解决方案,它速度更快,支持超过 15,000 个。非常感谢。

标签: sql postgresql hibernate


【解决方案1】:

不确定 Hibernate 是否可行,但您可以尝试加入:

select t.* 
from tablename t 
   join unnest(cast(? as text[])) as x(name) on t.user_name = x.name

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-19
    • 2012-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多