相关疑难解决方法(0)

如何使用`WHERE field IS NULL`索引查询?

我有一个包含大量插入内容的表,将其中一个字段 ( uploaded_at) 设置为NULL. 然后周期性任务选择所有元组WHERE uploaded_at IS NULL,处理它们并更新,设置uploaded_at为当前日期。

我应该如何索引表?

我知道我应该使用部分索引,例如:

CREATE INDEX foo ON table (uploaded_at) WHERE uploaded_at IS NULL
Run Code Online (Sandbox Code Playgroud)

或者像那样。我有点困惑,但如果在始终为NULL. 或者如果使用 b 树索引是正确的。Hash 看起来是一个更好的主意,但它已经过时并且不能通过流式热备复制进行复制。任何建议将不胜感激。

我对以下索引进行了一些试验:

"foo_part" btree (uploaded_at) WHERE uploaded_at IS NULL
"foo_part_id" btree (id) WHERE uploaded_at IS NULL
Run Code Online (Sandbox Code Playgroud)

并且查询平面似乎总是选择foo_part索引。索引的explain analyse结果也稍好一些foo_part

Index Scan using foo_part on t1  (cost=0.28..297.25 rows=4433 width=16) (actual time=0.025..3.649 rows=4351 loops=1)
   Index Cond: (uploaded_at IS NULL)
 Total runtime: 4.060 ms
Run Code Online (Sandbox Code Playgroud)

对比 …

postgresql index null index-tuning

15
推荐指数
1
解决办法
1万
查看次数

Why does this LEFT JOIN perform so much worse than LEFT JOIN LATERAL?

I have the following tables (taken from the Sakila database):

  • film: film_id is pkey
  • actor: actor_id is pkey
  • film_actor: film_id and actor_id are fkeys to film/actor

I am selecting a particular film. For this film, I also want all actors participating in that film. I have two queries for this: one with a LEFT JOIN and one with a LEFT JOIN LATERAL.

select film.film_id, film.title, a.actors
from   film
left join
  (         
       select     film_actor.film_id, array_agg(first_name) as actors
       from       actor
       inner …
Run Code Online (Sandbox Code Playgroud)

postgresql performance join execution-plan postgresql-10 postgresql-performance

14
推荐指数
1
解决办法
4050
查看次数

强制约束“两个表”

我在 SQL 中对电气原理图建模时遇到了一些麻烦。我想捕获的结构是

  part ??????????? pin
   ?                ?
part_inst ?????? pin_inst
Run Code Online (Sandbox Code Playgroud)

其中“inst”是“instance”的缩写。

例如,我可能part将 LM358 运算放大器pin用作 1OUT、1IN-、1IN+、GND、2IN+、2IN-、2OUT 和 V CC。然后我可能会将这部分放在原理图上,创建 apart_inst和 8 pin_insts。

忽略数据字段,我对模式的最初尝试是

create table parts (
    part_id bigserial primary key
);
create table pins (
    pin_id bigserial primary key,
    part_id bigint not null references parts
);
create table part_insts (
    part_inst_id bigserial primary key,
    part_id bigint not null references parts
);
create table pin_insts (
    pin_inst_id bigserial primary key,
    part_inst_id bigint …
Run Code Online (Sandbox Code Playgroud)

postgresql foreign-key database-design referential-integrity polymorphic-associations

13
推荐指数
1
解决办法
5105
查看次数

PostgreSQL 中的覆盖索引是否有助于 JOIN 列?

我有很多看起来像这样的表格:

CREATE TABLE table1(id INTEGER PRIMARY KEY, t1c1 INTEGER, t1c2 INTEGER);
CREATE TABLE table2(id INTEGER PRIMARY KEY, t1 INTEGER REFERENCES table1(id), t2c1 INTEGER);
Run Code Online (Sandbox Code Playgroud)

我做了很多连接,我试图过滤连接表以从第一个表中获取内容,如下所示:

SELECT t1c1
FROM table1
JOIN table2 ON table2.t1 = table1.id
WHERE t2c1 = 42;
Run Code Online (Sandbox Code Playgroud)

当我为表编写索引时,我会查看 WHERE 子句中使用的列并构建索引以满足它们。所以对于这个查询,我最终会写一个这样的索引:

CREATE INDEX ON table2 (t2c1);
Run Code Online (Sandbox Code Playgroud)

并且这个索引至少有资格在该查询中使用。

我的问题是,如果我写这样的索引:

CREATE INDEX ON table2 (t2c1, t1);
Run Code Online (Sandbox Code Playgroud)

索引会不会作为覆盖索引来帮助上面查询中的JOIN?我应该改变我的索引编写策略来覆盖外键列吗?

postgresql index optimization

13
推荐指数
1
解决办法
2万
查看次数

如何避免 3 个表之间的循环依赖(循环引用)?

我有3张桌子:

  • 人们
  • 邮政
  • 喜欢

当我设计 ER 模型时,它具有循环依赖关系:

         1:N
人 --------< 帖子

         1:N
发帖 ----------< 点赞

         1:N
人们 --------< 喜欢

逻辑是:

  • 1个人可以有很多帖子。

  • 1个帖子有很多赞。

  • 1个人可以点赞多个帖子(创建的人不能点赞自己的帖子)。

我怎样才能消除这种循环设计?还是我的数据库设计错了?

mysql postgresql database-design design-pattern

11
推荐指数
2
解决办法
9330
查看次数

由于行估计非常不准确,全文搜索速度缓慢

针对此数据库的全文查询(存储 RT(请求跟踪器)票证)似乎需要很长时间才能执行。附件表(包含全文数据)大约为 15GB。

数据库模式如下,大约有 200 万行:

rt4=# \d+ 附件
                                                    表“public.attachments”
     专栏 | 类型 | 修饰符 | 存储 | 描述
-----------------+------------------------------------------+-- -------------------------------------------------- -------+----------+-------------
 身份证 | 整数 | not null default nextval('attachments_id_seq'::regclass) | 平原 |
 交易ID | 整数 | 不为空| 平原 |
 家长 | 整数 | 非空默认值 0 | 平原 |
 消息ID | 字符变化(160) | | 扩展 |
 主题 | 字符变化(255) | | 扩展 |
 文件名 | 字符变化(255) | | 扩展 |
 内容类型 | 字符变化(80) | | 扩展 |
 内容编码 …

postgresql performance full-text-search execution-plan postgresql-9.1 query-performance

10
推荐指数
1
解决办法
1582
查看次数

CLUSTER 对性能的影响

我正在尝试优化我的 Postgres 9.2 数据库以加快具有日期限制的查询。

我有一个timestamp专栏,但主要是我要求某一天,所以我创建了一个timestamp用于date解析的索引:

CREATE INDEX foo_my_timestamp_idx
ON foo
USING btree
((my_timestamp::date) DESC);
Run Code Online (Sandbox Code Playgroud)

现在,为了提高性能,我CLUSTER foo使用上面的索引表:

CLUSTER foo USING foo_my_timestamp_idx;
Run Code Online (Sandbox Code Playgroud)

根据手册上SQL-CLUSTER,表

根据索引信息进行物理重新排序

我想知道是否会对使用表 PK 的其他查询的性能产生影响(比如说id_foo)。有什么缺点吗?

postgresql performance storage index-tuning postgresql-9.2 postgresql-performance

10
推荐指数
1
解决办法
3483
查看次数

优化大表上的 LATERAL JOIN 查询

我正在使用 Postgres 9.5。我有一个记录来自多个网站的页面点击量的表格。该表包含从 2016 年 1 月 1 日到 2016 年 6 月 30 日的大约 3200 万行。

CREATE TABLE event_pg (
   timestamp_        timestamp without time zone NOT NULL,
   person_id         character(24),
   location_host     varchar(256),
   location_path     varchar(256),
   location_query    varchar(256),
   location_fragment varchar(256)
);
Run Code Online (Sandbox Code Playgroud)

我正在尝试调整一个查询,该查询计算执行给定页面命中序列的人数。该查询旨在回答诸如“有多少人查看了主页,然后访问了帮助站点,然后查看了感谢页面”之类的问题?结果看起来像这样

?????????????????????????????????????????
?  home-page ? help site  ? thankyou    ?
?????????????????????????????????????????
? 10000      ? 9800       ?1500         ?
?????????????????????????????????????????
Run Code Online (Sandbox Code Playgroud)

请注意数字正在减少,这是有道理的,因为查看主页的 10000 人 9800 继续访问了帮助站点,而其中 1500 人继续点击了感谢页面。

3 步序列的 SQL 使用横向连接,如下所示:

SELECT 
  sum(view_homepage) AS view_homepage,
  sum(use_help) AS use_help,
  sum(thank_you) AS thank_you
FROM ( …
Run Code Online (Sandbox Code Playgroud)

postgresql performance optimization greatest-n-per-group postgresql-performance

9
推荐指数
1
解决办法
8114
查看次数

存储数百万行非标准化数据或一些 SQL 魔法?

我的 DBA 经验只是简单的存储 + CMS 样式数据的检索 - 所以这可能是一个愚蠢的问题,我不知道!

我有一个问题,我需要查找或计算特定组大小和特定时间段内特定天数的假期价格。例如:

1 月任何时候 2 人 4 晚的酒店房间多少钱?

例如,我有 5000 家酒店的定价和可用性数据,如下所示:

Hotel ID | Date | Spaces | Price PP
-----------------------------------
     123 | Jan1 | 5      | 100
     123 | Jan2 | 7      | 100
     123 | Jan3 | 5      | 100
     123 | Jan4 | 3      | 100
     123 | Jan5 | 5      | 100
     123 | Jan6 | 7      | 110
     456 | Jan1 | 5      | 120
     456 | Jan2 …
Run Code Online (Sandbox Code Playgroud)

postgresql window-functions datetime denormalization

8
推荐指数
1
解决办法
769
查看次数

RDS 上非常慢的简单 PostgreSQL 查询

我似乎在中型 RDS 盒子(db.m3.medium,3.7gb ram)上的查询速度很慢。

这是一个包含 4,152,928 行的表格。

select sum(some_field) c
from pages
where pages.some_id=123
and pages.first_action_at > '2014-01-01 00:00:00 +1000'
Run Code Online (Sandbox Code Playgroud)

总运行时间:45031 毫秒。
在本地,我有大约 110 万行,同样的查询需要大约 450 毫秒。

这是查询计划,来自解释:

Aggregate  (cost=475640.59..475640.60 rows=1 width=4)
   ->  Seq Scan on pages  (cost=0.00..475266.07 rows=149809 width=4)
         Filter: ((first_action_at > '2014-01-01 00:00:00'::timestamp without time zone) 
                AND (some_id = 447))
Run Code Online (Sandbox Code Playgroud)

这是来自解释分析的回应:

 Aggregate  (cost=475641.74..475641.76 rows=1 width=4) (actual time=42419.717..42419.718 rows=1 loops=1)
   ->  Seq Scan on pages  (cost=0.00..475267.22 rows=149810 width=4) (actual time=0.013..42265.908 rows=141559 loops=1)
    Filter: ((first_action_at > '2014-01-01 00:00:00'::timestamp without time …
Run Code Online (Sandbox Code Playgroud)

postgresql performance index index-tuning postgresql-performance

7
推荐指数
1
解决办法
1万
查看次数