这是表定义(简化):
CREATE TABLE documents (
document_id int4 NOT NULL GENERATED BY DEFAULT AS IDENTITY,
data_block jsonb NULL
);
Run Code Online (Sandbox Code Playgroud)
示例值:
INSERT INTO documents (document_id, data_block)
VALUES
(878979,
'{"COMMONS": {"DATE": {"value": "2017-03-11"}},
"PAYABLE_INVOICE_LINES": [
{"AMOUNT": {"value": 52408.53}},
{"AMOUNT": {"value": 654.23}}
]}')
, (977656,
'{"COMMONS": {"DATE": {"value": "2018-03-11"}},
"PAYABLE_INVOICE_LINES": [
{"AMOUNT": {"value": 555.10}}
]}');
Run Code Online (Sandbox Code Playgroud)
我想搜索其中一个'PAYABLE_INVOICE_LINES'元素包含'value'大于 1000.00 的所有文档。
我的查询是
select *
from documents d
cross join lateral jsonb_array_elements(d.data_block -> 'PAYABLE_INVOICE_LINES') as pil
where (pil->'AMOUNT'->>'value')::decimal > 1000
Run Code Online (Sandbox Code Playgroud)
但是,由于我想限制为 50 个文档,因此我必须对document_id …
postgresql performance index-tuning json postgresql-10 postgresql-performance
我有一个包含 col1、col2、col3 列的简单表格。都不可为空。
我想删除元组 (col1, col2) 有多个条目的所有行。背景:应添加 (col1, col2) 的唯一约束。
drop table mytable;
create table mytable (
col1 integer not null,
col2 integer not null,
col3 integer not null);
-- rows to delete
insert into mytable values (1, 1, 1);
insert into mytable values (1, 1, 2);
-- rows to keep
insert into mytable values (2, 2, 1);
insert into mytable values (2, 3, 2);
delete from mytable where
(col1, col2) in (
select col1, col2 from mytable
group by …Run Code Online (Sandbox Code Playgroud) 我有一个疑问。我想删除这 3 个不同表中的所有选定行 由于我有很多 INNER 连接,我无法弄清楚。我的目标是删除这些卖家 ID 的所有内容。
SELECT *
FROM orders a
INNER JOIN order_items b ON a.order_id = b.order_id
INNER JOIN order_item_histories c ON c.order_item_id = b.order_item_id
WHERE a.seller_id IN (1, 3)
Run Code Online (Sandbox Code Playgroud)
版本 Postgres 10.3
我试过这个,但我无法成功。
DELETE
FROM
USING orders
USING order_items,
USING order_item_histories
WHERE orders.order_id = order_items.order_id AND order_items.order_item_id = order_item_histories.order_item_id
AND orders.seller_id IN (1, 3)
Run Code Online (Sandbox Code Playgroud) 我有一个表my_tables,它在几个表中被引用为外键。
我想选择my_table其他表中未引用的所有行。
AFAIK,应该可以以通用方式(带有一些内省魔法)来做到这一点。
我使用以下批处理文件在 PostgreSQL 10 中创建一个新数据库。
echo off
cd "C:\Program Files (x86)\PostgreSQL\10\bin"
createdb -h localhost -p 5432 -U postgres myDB
Run Code Online (Sandbox Code Playgroud)
但它要求输入密码。如何防止要求输入密码?我尝试使用此链接中-w解释的选项不要求输入密码提示。但这不起作用。如何防止要求密码?
我有一个表,用于表上的多对多关系users来表示用户之间的跟随关系:
CREATE TABLE users (
id text PRIMARY KEY,
username text NOT NULL
);
CREATE TABLE followers (
userid text,
followid text,
PRIMARY KEY (userid, followid),
CONSTRAINT followers_userid_fk FOREIGN KEY (userid) REFERENCES users (id),
CONSTRAINT followers_followid_fk FOREIGN KEY (followid) REFERENCES users (id)
);
CREATE INDEX followers_followid_idx ON followers (followid);
Run Code Online (Sandbox Code Playgroud)
当我想使用与用户相关的数据创建 JSON 响应时,我有两种情况:
用户数据对象应包含两个用户 ID 数组,一个是他们关注的用户,另一个是关注他们的用户。为了创建这两个字段,我使用了以下SELECT语句。
DECLARE follows RECORD;
SELECT array (select followid FROM followers where userid = Puserid) AS following, …Run Code Online (Sandbox Code Playgroud) 我在数据库中有一个表 t (PostgreSQL 10.4):
\d t;
Table "public.t"
Column | Type | Collation | Nullable | Default
----------+------------------------+-----------+----------+---------
sn | character varying(11) | | |
site | character varying(50) | | |
Indexes:
"site_2018_idx" btree (site), tablespace "indexspace"
"sn_2018_idx" btree (sn), tablespace "indexspace"
Run Code Online (Sandbox Code Playgroud)
我需要为特定站点找到不同的 'sn,我这样做:
SELECT DISTINCT sn FROM t WHERE site='a_b301_1' ORDER BY sn ;
Run Code Online (Sandbox Code Playgroud)
它可以工作,但速度很慢,返回 75 个不同的“sn”值大约需要 8 分钟!有没有办法加快速度?解释分析给出了这个输出:
QUERY PLAN
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Sort (cost=42873094.21..42873103.25 rows=3615 width=12) (actual time=190431.413..190431.417 rows=75 loops=1)
Output: sn
Sort Key: t.sn
Sort Method: quicksort …Run Code Online (Sandbox Code Playgroud) postgresql performance distinct postgresql-10 query-performance
使用PostgreSQL v10.1.2 中的存储过程,哪种方法最快或哪种更好:检查行是否存在然后更新或尝试直接更新,但可能找不到与条件匹配的行?我需要检查很多条件相同的表,表是不是“非规范化”,我进行了几次测试,有时选项 1在其他情况下更快,选项 2 ...
选项1:
IF EXISTS ( SELECT 1
FROM public.table1
WHERE column1 = 'oldvalue' )
THEN
UPDATE public.table1
SET column1 = 'newvalue' , date_update= ....
WHERE column1 = 'oldvalue';
END IF ;
Run Code Online (Sandbox Code Playgroud)
选项 2:
UPDATE public.table1
SET column1 = 'newvalue' , date_update= ...
WHERE column1 = 'oldvalue';
Run Code Online (Sandbox Code Playgroud)
选项 3:
perform FROM public.table1 WHERE column1 = 'oldvalue' ;
if found then
UPDATE public.table1 SET column1='newvalue', date_update = ... WHERE column1 …Run Code Online (Sandbox Code Playgroud) postgresql performance stored-procedures update postgresql-10
是否有一个名为pg_xlog将所有 WAL 日志存储在 PostgreSQL 中的目录?在归档方案下恢复基本备份的一部分需要我将 WAL 复制到DATA_DIR/pg_xlog. 这个目录怎么了?
我有一个包含约 20 亿行数据的表,我想创建另一个包含一些聚合的表。看起来 PostgreSQL 使用临时磁盘空间来执行这些查询。我可以创建表...
CREATE TABLE my_new_table ...
Run Code Online (Sandbox Code Playgroud)
但是当我插入数据时:
INSERT INTO my_new_table SELECT
col_1,
col_2,
col_3,
col_4,
col_5,
col_6,
col_7,
col_8,
col_9,
sum(col_10),
sum(col_11)
FROM
my_table
GROUP BY
1,2,3,4,5,6,7,8,9
Run Code Online (Sandbox Code Playgroud)
PostgreSQL 似乎使用临时文件来存储结果,并且空间不足,例如出现如下错误:
无法写入文件“base/pgsql_tmp/pgsql_tmp31757.25”:设备上没有剩余空间
从 EXPLAIN 的结果来看,我怀疑这是来自某种排序。有办法避免这种情况吗?不会有那么多的输出行,所以不知何故,我觉得好像应该有一种方法可以在输出处做得更到位......但这是一个非常模糊的直觉。
postgresql ×10
postgresql-10 ×10
performance ×5
aws-aurora ×1
command-line ×1
ddl ×1
disk-space ×1
distinct ×1
foreign-key ×1
group-by ×1
index-tuning ×1
json ×1
many-to-many ×1
metadata ×1
replication ×1
update ×1
view ×1
windows ×1