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-242165

Pragy Agarwal's questions

Martin Hope
Pragy Agarwal
Asked: 2022-07-31 12:34:59 +0800 CST

如何将当前行传递给 Postgres 中的 Generated Column 函数?

  • 4

Postgres 12+ 支持生成的列:https ://www.postgresql.org/docs/current/ddl-generated-columns.html

从文档来看,语法似乎是有限的——它强制人们显式地命名生成列所依赖的列。

CREATE TABLE people (
    ...,
    height_cm numeric,
    height_in numeric GENERATED ALWAYS AS (height_cm / 2.54) STORED
);

有没有办法将整行传递给生成函数?就像是

CREATE TABLE people (
    ...,
    height_cm numeric,
    height_in numeric GENERATED ALWAYS AS generator_function(current_row) STORED
);
postgresql functions
  • 1 个回答
  • 175 Views
Martin Hope
Pragy Agarwal
Asked: 2021-11-24 06:25:24 +0800 CST

使用触发器代替 count(*) 来计算相关实体的缺点是什么?

  • 2
  1. 使用以下方法有什么缺点?
  2. 可以采取哪些措施来防止有人错误地更新计数?
  3. 以下方法是反模式吗?如果是,有什么更好的方法存在?

假设:

应用程序是一个公共 wiki/论坛(如 stackexchange)

  1. 该应用程序读取量很大。选择将是最终用户提出的 99% 的查询
  2. 最终用户不执行批量插入。它们可能仅用于开发/维护工作
  3. 需要同时写入表
  4. 计数估计已经足够好了。确切的数量将是最重要的

方法

考虑两个实体,Students每个实体Courses都有对应的表。我们还有一个student_courses存储多对多映射 b/w 学生和课程的表。

create table students (
  id bigserial primary key,
  name text
);

create table courses (
  id bigserial primary key,
  content text
);

create table student_courses (
  student_id bigint not null references students,
  course_id bigint not null references courses,
  primary key (student_id, course_id)
);
create index on student_courses (course_id);

要找到给定学生的课程数量,我们可以执行

select count(1)
from student_courses
where student_id = 123;

如果这些计数查询很频繁(假设您希望始终显示学生姓名和课程计数),优化的一种方法是在两个实体中维护一个计数变量,然后放置插入和删除触发器。

alter table students add column course_count bigint default 0;
alter table courses add column student_count bigint default 0;

create function increment_student_course_count() returns trigger as
$$begin
  update students
    set course_count := course_count + 1
    where student_id = new.student_id;
  update courses
    set student_count := student_count + 1
    where course_id = new.course_id;
  return new;
end;$$

create trigger after_insert_update_counts
after insert
on student_courses
for each row execute procedure increment_student_course_count();


create function decrement_student_course_count() returns trigger as
$$begin
  update students
    set course_count := course_count - 1
    where student_id = new.student_id;
  update courses
    set student_count := student_count - 1
    where course_id = new.course_id;
  return new;
end;$$

create trigger after_delete_update_counts
after delete
on student_courses
for each row execute procedure decrement_student_course_count();

一旦到位,查找学生的课程数量很简单

select course_count
from students where student_id = 123;
database-design postgresql
  • 2 个回答
  • 333 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