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
    • 最新
    • 标签
主页 / user-225269

Robert Elliot's questions

Martin Hope
Robert Elliot
Asked: 2022-09-25 04:52:54 +0800 CST

对于每一行,使用自动生成的 ID 在另一个表中创建一个链接行

  • 2

在 Postgres 中,我有一个现有的表:

things

thing_id 事物名称 目的地ID
10 第 10 件事 无效的
15 第 15 件事 无效的

对于该表中的每一行,我想将行添加到两个新的相关表中,其中一个具有自动生成的 ID。然后我想更新旧表中的行以指向相关的新行。

期望的结果:

things

thing_id 事物名称 目的地ID
10 第 10 件事 1
15 第 15 件事 2

parent_destination

目的地ID
1
2

child_destination

目的地ID 目的地名称
1 [目的地]东西10
2 [目的地]东西15

我在这里尝试过这样做:https ://www.db-fiddle.com/f/6iyvCT7BYXPHPi2N2HvNor/1 但我不知道如何从result1.

postgresql insert
  • 2 个回答
  • 44 Views
Martin Hope
Robert Elliot
Asked: 2022-09-18 03:40:49 +0800 CST

如何优化 Postgresql 中的递归查询?

  • 2

我有一个递归查询需要很长时间 - 30+ 毫秒,其中手动提取相同数据的单个查询需要 < 0.12 毫秒。所以我们说的时间是 250 倍。

我有以下数据库结构,允许组成员身份的 DAG(此处为 db-fiddle):

create table subjects
(
    subject_id bigint not null
        constraint pk_subjects
            primary key
);

create table subject_group_members
(
    subject_group_id bigint not null
        constraint fk_subject_group_members_subject_group_id_subjects_subject_id
            references subjects(subject_id)
            on delete cascade,
    subject_id       bigint not null
        constraint fk_subject_group_members_subject_id_subjects_subject_id
            references subjects(subject_id)
            on delete cascade,
    constraint pk_subject_group_members
        primary key (subject_group_id, subject_id)
);

create index idx_subject_group_members_subject_id
    on subject_group_members (subject_id);

create index idx_subject_group_members_subject_group_id
    on subject_group_members (subject_group_id);

数据可能如下所示:

subject_group_id 主题ID
1 2
1 3
1 4
2 5
3 5

我想知道 5 所属的所有组(1 通过继承,2 和 3 直接,不是 4 或任何其他主题 ID)。

此查询按预期工作:

with recursive flat_members(subject_group_id, subject_id) as (
      select subject_group_id, subject_id
      from subject_group_members gm
      union
      select
          flat_members.subject_group_id as subject_group_id,
          subject_group_members.subject_id as subject_id
      from subject_group_members
      join flat_members on flat_members.subject_id = subject_group_members.subject_group_id
  )
  select * from flat_members where subject_id = 5

但是使用真实数据运行,我得到了这个查询计划:

CTE Scan on flat_members  (cost=36759729.47..59962757.76 rows=5156229 width=16) (actual time=26.526..55.166 rows=3 loops=1)
  Filter: (subject_id = 30459)
  Rows Removed by Filter: 48984
  CTE flat_members
    ->  Recursive Union  (cost=0.00..36759729.47 rows=1031245702 width=16) (actual time=0.022..47.638 rows=48987 loops=1)
          ->  Seq Scan on subject_group_members gm  (cost=0.00..745.82 rows=48382 width=16) (actual time=0.019..4.286 rows=48382 loops=1)
          ->  Merge Join  (cost=63629.74..1613406.96 rows=103119732 width=16) (actual time=10.897..11.038 rows=320 loops=2)
                Merge Cond: (subject_group_members.subject_group_id = flat_members_1.subject_id)
                ->  Index Scan using idx_subject_group_members_subject_group_id on subject_group_members  (cost=0.29..1651.02 rows=48382 width=16) (actual time=0.009..1.987 rows=24192 loops=2)
                ->  Materialize  (cost=63629.45..66048.55 rows=483820 width=16) (actual time=4.124..6.592 rows=24668 loops=2)
                      ->  Sort  (cost=63629.45..64839.00 rows=483820 width=16) (actual time=4.120..5.034 rows=24494 loops=2)
                            Sort Key: flat_members_1.subject_id
                            Sort Method: quicksort  Memory: 53kB
                            ->  WorkTable Scan on flat_members flat_members_1  (cost=0.00..9676.40 rows=483820 width=16) (actual time=0.001..0.916 rows=24494 loops=2)
Planning Time: 0.296 ms
Execution Time: 56.735 ms

现在,如果我手动执行,查询select subject_group_id from subject_group_members where subject_id = 30459并跟踪树,则有 4 个查询,每个查询大约需要 0.02 毫秒。

有没有一种方法可以使递归查询接近手动进行递归的速度?

postgresql query-performance
  • 1 个回答
  • 93 Views
Martin Hope
Robert Elliot
Asked: 2021-03-06 05:24:43 +0800 CST

如何在同一个表的列之间定义一对多约束?

  • 1

我正在 PostgreSQL 中创建一个 IDs 表来表示多个外部 ID 之间的关系 - 称它们为a,b和c.

我碰巧知道外国ID之间有一对多的关系:

a 1..* b b 1..* c

a我想将它们表示为一个表(见下文),但在& b、 & 和 之间b强制执行一对多不变量c。

那可能吗?

示例:以下插入是非法的:

PK 一个 b C 原因
p15 a1 b11 c111 c111 已经存在
p16 a1 b21 c213 a2 的 b21 已存在
p17 a2 b11 c214 a1 的 b11 已存在

给定以下合法表格:

PK 一个 b C
p1 a1 b11 c111
p2 a1 b11 c112
p3 a1 b12 c121
p4 a1 b12 c122
p5 a2 b21 c211
p6 a2 b21 c212
p7 a2 b22 c221
p8 a2 b22 c222
p9 无效的 b31 c311
p10 a3 无效的 c312
p11 a3 b31 无效的
p12 无效的 无效的 c314
p13 无效的 b31 无效的
p14 a3 无效的 无效的
postgresql constraint
  • 1 个回答
  • 148 Views

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