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

Kevin Meredith's questions

Martin Hope
Kevin Meredith
Asked: 2024-07-18 09:52:24 +0800 CST

部分索引对不涉及部分匹配的 INSERT 的性能影响

  • 5

鉴于:

postgres=# create table testing(a int primary key, b int not null);
CREATE TABLE
postgres=# create index on testing (b) where b = 0;
CREATE INDEX
postgres=# \d testing
              Table "public.testing"
 Column |  Type   | Collation | Nullable | Default 
--------+---------+-----------+----------+---------
 a      | integer |           | not null | 
 b      | integer |           | not null | 
Indexes:
    "testing_pkey" PRIMARY KEY, btree (a)
    "testing_b_idx" btree (b) WHERE b = 0

当在!=INSERT处添加新行时,我是否需要支付更新索引的惩罚?b0

理想情况下,我想加速值为b=的查询,而不会增加在!=1处插入新行的延迟。b0

postgresql
  • 1 个回答
  • 28 Views
Martin Hope
Kevin Meredith
Asked: 2023-12-16 22:19:51 +0800 CST

了解 AWS RDS 多可用区同步复制

  • 6

多可用区 AWS RDS文档指出:

在此输入图像描述

  1. 什么是“同步块级复制”?
  2. 鉴于这种复制是在磁盘级别,而不是数据库级别,这种形式是否与数据库复制一样可靠?
replication
  • 1 个回答
  • 21 Views
Martin Hope
Kevin Meredith
Asked: 2023-11-04 21:43:04 +0800 CST

在 COMMIT 和 CHAIN 上保持锁定?

  • 5

我正在编写一个处理作业的应用程序。一个作业做两件非事务性的事情:A 部分和 B 部分。两者都是幂等的。

一个职位有三种状态:

  • 已创建
  • A 部分_完成
  • B 部分_完成

逻辑看起来像这样:

BEGIN;
let jobId = randomUUID
INSERT INTO jobs (id, status) VALUES (jobId, 'Created');
COMMIT;

BEGIN;
do partA in application code (make HTTP request)
UPDATE jobs SET status = 'PartA_Done' WHERE id=?
COMMIT;

BEGIN;
do partB in application code (make HTTP request)
UPDATE jobs SET status = 'PartB_Done' WHERE id=?
COMMIT;

PartA 可能会成功,但无法更新jobs表。重试 cron 作业将重试完成PartB_Done。

如果我要添加多个 cron 作业(即同时运行),则存在 cron 作业执行重复工作的风险。特别是,两个 cron 作业都可以重复执行PartA,PartB或者都可以重复执行同一个作业。我想避免这种情况。

有什么方法可以让我执行, 同时在我添加的新行上的后续事务中COMMIT保持锁定?FOR UPDATEjobs

我认为这COMMIT AND CHAIN会起作用,但我不知道该怎么做。

postgresql
  • 1 个回答
  • 32 Views
Martin Hope
Kevin Meredith
Asked: 2021-02-26 19:59:10 +0800 CST

Postresql 客户端和版本不匹配

  • 1

鉴于:

$which psql
/Library/PostgreSQL/12/bin/psql
$which createdb
/Library/PostgreSQL/12/bin/createdb

最近我创建了一个数据库:

$createdb -U postgres postgres
Password: 

但是,我不明白为什么服务器版本因客户端而异。

$psql -U postgres
Password for user postgres: 
psql (12.1, server 9.6.2)
Type "help" for help.

如何在 12.1 上创建服务器?

postgresql macos
  • 1 个回答
  • 60 Views
Martin Hope
Kevin Meredith
Asked: 2021-02-11 19:51:46 +0800 CST

了解插入...返回

  • 3

鉴于:

$psql -U postgres
Password for user postgres: 
psql (12.1, server 9.6.2)

postgres=# \d foo
                Table "public.foo"
 Column |  Type   | Collation | Nullable | Default 
--------+---------+-----------+----------+---------
 a      | integer |           |          | 

然后我跑了:

postgres=# select count(*) from foo;
 count 
-------
     0
(1 row)

postgres=# insert into foo (a) values (1) returning (select count(*) from foo);
 count 
-------
     0
(1 row)

INSERT 0 1

为什么它返回0而不是1,即因为我插入了一行?

postgresql
  • 1 个回答
  • 691 Views
Martin Hope
Kevin Meredith
Asked: 2020-10-14 04:17:18 +0800 CST

了解带演员表的索引

  • 0

鉴于:

postgres=# \d b
                Table "public.b"
 Column | Type | Collation | Nullable | Default 
--------+------+-----------+----------+---------
 a      | uuid |           | not null | 
Indexes:
    "b_a_idx" btree (a)

postgres=# \d c
                Table "public.c"
 Column | Type | Collation | Nullable | Default 
--------+------+-----------+----------+---------
 a      | text |           | not null | 
Indexes:
    "c_a_idx" btree (a)

postgres=# explain b;
ERROR:  syntax error at or near "b"
LINE 1: explain b;
                ^
postgres=# analyze b; 
ANALYZE
postgres=# analyze c;
ANALYZE

为什么下面的查询不使用b.a's 索引?

postgres=# explain (select * from b join c on (b.a :: text) = c.a);
                                   QUERY PLAN                                    
---------------------------------------------------------------------------------
 Merge Join  (cost=129.05..455.25 rows=12580 width=48)
   Merge Cond: (c.a = ((b.a)::text))
   ->  Index Only Scan using c_a_idx on c  (cost=0.15..64.55 rows=1360 width=32)
   ->  Sort  (cost=128.89..133.52 rows=1850 width=16)
         Sort Key: ((b.a)::text)
         ->  Seq Scan on b  (cost=0.00..28.50 rows=1850 width=16)
(6 rows)
postgresql index
  • 1 个回答
  • 121 Views
Martin Hope
Kevin Meredith
Asked: 2020-09-21 19:18:34 +0800 CST

了解 JOIN 语法

  • 0

鉴于:

postgres=# \d foo
                Table "public.foo"
 Column |  Type   | Collation | Nullable | Default 
--------+---------+-----------+----------+---------
 a      | integer |           | not null | 
 b      | text    |           | not null | 
Indexes:
    "foo_pkey" PRIMARY KEY, btree (a)

postgres=# \d bar
                Table "public.bar"
 Column |  Type   | Collation | Nullable | Default 
--------+---------+-----------+----------+---------
 a      | integer |           | not null | 
 b      | text    |           | not null | 
Indexes:
    "bar_pkey" PRIMARY KEY, btree (a)

postgres=# select * from foo;
 a |  b  
---+-----
 1 | one
(1 row)

postgres=# select * from bar;
 a |  b  
---+-----
 2 | two
(1 row)

然后我会join使用以下 JOIN 语法:

postgres=# select * from foo, bar;
 a |  b  | a |  b  
---+-----+---+-----
 1 | one | 2 | two
(1 row)

然后,我将其与full outer join:

postgres=# select * from foo full outer join bar using (a);
 a |  b  |  b  
---+-----+-----
 1 | one | 
 2 |     | two
(2 rows)

和cross join:

postgres=# select * from foo cross join bar;
 a |  b  | a |  b  
---+-----+---+-----
 1 | one | 2 | two
(1 row)

from a, b, c意志产生 a总是真的cross join吗?

postgresql join
  • 1 个回答
  • 55 Views
Martin Hope
Kevin Meredith
Asked: 2020-03-18 11:26:19 +0800 CST

了解综合指数的解释计划

  • 1

在 PostgreSQL12.1上,给出:

postgres=> create table abc(a int not null, b text not null, c boolean not null);
CREATE TABLE

postgres=> create index on abc (a, b, c);
CREATE INDEX

然后我准备了一个只过滤 的查询a,即索引中的第一个元素。

postgres=> prepare only_a(int) as select 42 from abc where a = 1;
PREPARE

postgres=> explain execute only_a(100);
                                 QUERY PLAN                                 
----------------------------------------------------------------------------
 Bitmap Heap Scan on abc  (cost=4.20..13.67 rows=6 width=4)
   Recheck Cond: (a = 1)
   ->  Bitmap Index Scan on abc_a_b_c_idx  (cost=0.00..4.20 rows=6 width=0)
         Index Cond: (a = 1)
(4 rows)

根据上述内容,它可以使用索引来过滤a。

然后,我准备了一个过滤a和的查询c,即不使用b。

postgres=> prepare a_and_c(int) as select 42 from abc where a = 1 and c = true;
PREPARE

然后,我跑了explain execute,

postgres=> explain execute a_and_c(100);
                                 QUERY PLAN                                 
----------------------------------------------------------------------------
 Bitmap Heap Scan on abc  (cost=4.21..11.32 rows=3 width=4)
   Recheck Cond: (a = 1)
   Filter: c
   ->  Bitmap Index Scan on abc_a_b_c_idx  (cost=0.00..4.21 rows=3 width=0)
         Index Cond: ((a = 1) AND (c = true))
(5 rows)

我的理解是,这意味着Index Cond: ((a = 1) AND (c = true))尽管没有参与.abc_a_b_c_idxacbwhere

如果可以,c既然b不使用怎么能使用?

postgresql
  • 2 个回答
  • 129 Views
Martin Hope
Kevin Meredith
Asked: 2019-09-22 06:46:11 +0800 CST

丢失更新理解

  • 5

https://habr.com/en/company/postgrespro/blog/467437/ 给出了以下丢失更新的示例:

例如,两次交易将使同一账户的金额增加 ₽100(₽ 是俄罗斯卢布的货币符号)。第一个事务读取当前值 (₽1000),然后第二个事务读取相同的值。第一个交易增加了金额(这给出了 ₽1100)并写入了这个值。第二个事务的行为方式相同:它得到相同的 ₽1100 并写入该值。结果,客户损失了₽100

我读了几遍。但我不明白客户是如何失去 P100 的。请解释。

transaction
  • 2 个回答
  • 376 Views
Martin Hope
Kevin Meredith
Asked: 2019-09-07 05:40:15 +0800 CST

带更新的公用表表达式

  • 2

使用 Postgres 10.5,给出以下内容:

postgres=# begin; 
   with updated as (update bippy set id = 100 where id = 1 returning 1)
select id, ts from bippy;
commit;
BEGIN
 id |              ts               
----+-------------------------------
 66 | 2019-07-09 10:42:32.062496-04
 80 | 2019-07-09 10:43:28.068512-04
 80 | 2019-07-09 10:43:28.596341-04
 80 | 2019-07-15 14:42:32.062496-04
  1 | 2019-07-09 10:42:23.142809-04
  1 | 2019-07-09 10:42:25.366664-04
  1 | 2019-07-09 10:42:26.142027-04
  1 | 2019-07-09 10:42:26.702398-04
(8 rows)

COMMIT

为什么上面返回一个idof1而不是100,即根据公用表表达式?

当我立即从表中选择时,我看到更新已经发生。

postgres=# select * from bippy;
              ts               | id  
-------------------------------+-----
 2019-07-09 10:42:32.062496-04 |  66
 2019-07-09 10:43:28.068512-04 |  80
 2019-07-09 10:43:28.596341-04 |  80
 2019-07-15 14:42:32.062496-04 |  80
 2019-07-09 10:42:23.142809-04 | 100
 2019-07-09 10:42:25.366664-04 | 100
 2019-07-09 10:42:26.142027-04 | 100
 2019-07-09 10:42:26.702398-04 | 100
(8 rows)
postgresql
  • 1 个回答
  • 274 Views
Martin Hope
Kevin Meredith
Asked: 2019-07-06 05:33:40 +0800 CST

解析、规划和优化准备好的语句

  • 0

Douglas 和 Douglas 的PostgreSQL(第 2 版)注释:

使用服务器端过程(触发器和函数)来执行常见操作。服务器端过程在您第一次使用时会被解析、计划和优化,而不是每次使用时。

使用准备好的语句时,它是仅在第一次使用时进行解析、计划和优化(类似于引用文本中的触发器和函数),还是每次使用时?

postgresql
  • 1 个回答
  • 38 Views
Martin Hope
Kevin Meredith
Asked: 2019-02-26 05:34:46 +0800 CST

物化视图性能

  • 4

Postgres 的文档说明:

虽然访问存储在物化视图中的数据通常比直接或通过视图访问基础表快得多,但数据并不总是最新的;

https://www.postgresql.org/docs/9.6/rules-materializedviews.html

为什么它“通常快得多”?

postgresql materialized-view
  • 2 个回答
  • 8716 Views
Martin Hope
Kevin Meredith
Asked: 2016-11-16 06:25:37 +0800 CST

使用 Oracle 函数的 IF 语句理解 `null`

  • 1

给定以下 Oracle 函数:

create or replace FUNCTION nullFunction(a NUMBER) RETURN NUMBER IS
  varX NUMBER;
  BEGIN
  select 1 into varX from dual where exists(select 1 from dual where a = 1);
    IF (varX = 1) THEN RETURN 1;
                  ELSE RETURN 0;
    END IF;
  END;

然后,我从 Oracle SQL Developer 的工作表中调用它:

select nullFunction(3) from dual回报(null)。

接下来,我将函数修改为:

create or replace FUNCTION nullFunction(a NUMBER) RETURN NUMBER IS
  varX NUMBER;
  BEGIN
  select 1 into varX from dual where exists(select 1 from dual where a = 1);
    IF (varX is NULL) THEN RETURN 0;
                      ELSE RETURN 1;
    END IF;
  END;   

3但是,当我用-的值调用它时,我收到了相同的输出null。

为什么它返回NULL?作为. 0_ varX_ null_else

oracle
  • 1 个回答
  • 330 Views
Martin Hope
Kevin Meredith
Asked: 2016-10-15 08:13:48 +0800 CST

在函数中使用 `SELECT` 作为 `RETURN` 参数?

  • 0

使用 Oracle,我尝试编写一个函数来演示如何返回SELECT ...查询结果:

 CREATE OR REPLACE FUNCTION f(v1 number) RETURN number IS
     BEGIN
      IF TRUE THEN
         RETURN (select 42 from dual);
       ELSE
         RETURN v1;
       END IF;
     END;

但它不编译。

我怎样才能修复上面IF的RETURN返回42,即结果select 42 from DUAL?

oracle functions
  • 1 个回答
  • 98 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