我正在尝试找到一种将工作日添加到任何日期的方法。一个例子是:
date = '2017-04-28' (It was a Friday)
date + 2 = '2017-05-02' (It skipped Saturday and Sunday)
Run Code Online (Sandbox Code Playgroud)
有没有办法在没有自定义查询的情况下做到这一点?
我需要从一个大表中删除一些行。要删除的行不应在另一个表中,例如:
DELETE FROM LargeTable WHERE id IS NOT IN (SELECT DISTINCT foreign_id from EvenLargerTable)
但是我的服务器无法处理这种生硬的查询,因为 LargeTable 中几乎有一百万条记录,而 EvenLargerTable 中有几百万条记录
我该如何解决?
出于某种原因,Postgres 似乎没有使用我们创建的索引。这是我正在测试的查询:
SELECT "public"."influencers".*
FROM "public"."influencers"
WHERE (ig -> 'id' @> '"4878142508"')
LIMIT 1
Run Code Online (Sandbox Code Playgroud)
运行后EXPLAIN:
-> Seq Scan on influencers (cost=0.00..32800.14 rows=216 width=1110)
Run Code Online (Sandbox Code Playgroud)
这表明(如我所见)没有使用索引。
这是我们创建的数据库和索引:
CREATE TABLE public.influencers
(
id integer NOT NULL DEFAULT nextval('influencers_id_seq'::regclass),
location jsonb,
gender text COLLATE pg_catalog."default",
birthdate timestamp without time zone,
ig jsonb,
contact_info jsonb,
created_at timestamp without time zone DEFAULT now(),
updated_at timestamp without time zone DEFAULT now(),
categories text[] COLLATE pg_catalog."default",
search_field text COLLATE pg_catalog."default",
search_vector tsvector,
ig_updated_at timestamp without time …Run Code Online (Sandbox Code Playgroud) 我们有一个包含大约 50 万行的表。数据库表应该增长到数百万条记录。
这是表的样子:
CREATE TABLE public.influencers
(
id integer NOT NULL DEFAULT nextval('influencers_id_seq'::regclass),
location jsonb,
gender text COLLATE pg_catalog."default",
birthdate timestamp without time zone,
ig jsonb,
contact_info jsonb,
created_at timestamp without time zone DEFAULT now(),
updated_at timestamp without time zone DEFAULT now(),
categories text[] COLLATE pg_catalog."default",
search_field text COLLATE pg_catalog."default",
search_vector tsvector,
ig_updated_at timestamp without time zone,
CONSTRAINT influencers_pkey PRIMARY KEY (id),
CONSTRAINT ig_id_must_exist CHECK (ig ? 'id'::text),
CONSTRAINT ig_username_must_exist CHECK (ig ? 'username'::text)
)
Run Code Online (Sandbox Code Playgroud)
这些是我们需要高效执行的一些查询:
SELECT "public"."influencers".*
FROM …Run Code Online (Sandbox Code Playgroud) 如何在同一个查询中执行更新和插入?我需要更新记录,如果它不存在那么它必须执行插入我试过这个
UPDATE bank SET Address= $2,"PANCard"= $3,"IFSC" = $4 WHERE user_id = $1;
INSERT INTO bank (user_id,bank_details,"PAN",bank_acc,"UAN",tax)
SELECT '$2','$3','$4','$5','$6'
WHERE NOT EXISTS (SELECT * FROM bank WHERE user_id=$1);
Run Code Online (Sandbox Code Playgroud)
但我越来越
{
"name": "error",
"length": 123,
"severity": "ERROR",
"code": "42601",
"file": "postgres.c",
"line": "1274",
"routine": "exec_parse_message"
}
Run Code Online (Sandbox Code Playgroud)
请在这里帮助我
由于频繁的新记录和更新记录导致索引和存储碎片,我面临性能下降和存储使用量增加的问题。
VACUUM 没有多大帮助。
不幸的是,CLUSTER 不是一个选项,因为它会导致停机并且 pg_repack 不适用于 AWS RDS。
我正在寻找 CLUSTER 的 hacky 替代品。在我的本地测试中似乎可以正常工作的一个是:
begin;
create temp table tmp_target as select * from target;
delete from target;
insert into target select * from tmp_target order by field1 asc, field2 desc;
drop table tmp_target;
commit;
Run Code Online (Sandbox Code Playgroud)
ctid看起来的顺序是正确的:
select ctid, field1, field2 from target order by ctid;
Run Code Online (Sandbox Code Playgroud)
问题是:这看起来好吗?是否会锁定target表以SELECT查找导致应用程序停机的查询?有没有办法列出事务中涉及的锁?
在 Ubuntu 16.04 上使用 PostGIS 2.2 全新安装 PostgreSQL 9.5 时,我似乎无法为数据库创建只读用户。这是我到目前为止所做的:
跑了这个,
CREATE ROLE dbowner LOGIN ENCRYPTED PASSWORD 'dbownerpassword';
CREATE DATABASE thedb WITH OWNER dbowner;
GRANT ALL PRIVILEGES ON DATABASE thedb TO dbowner;
CREATE EXTENSION postgis;
CREATE EXTENSION postgis_topology;
GRANT ALL ON ALL TABLES IN SCHEMA public to dbowner;
GRANT ALL ON ALL SEQUENCES IN SCHEMA public to dbowner;
GRANT ALL ON ALL FUNCTIONS IN SCHEMA public to dbowner;
CREATE ROLE readonly LOGIN ENCRYPTED PASSWORD 'ropassword';
GRANT CONNECT ON DATABASE thedb …Run Code Online (Sandbox Code Playgroud) 我编写了一个小程序来从文件导入产品详细信息更新,这比预期的要长得多。(为简洁起见,我将使用精简的示例。)
该程序执行以下操作:
COPYs 修改后的数据到临时表中。这一切都很好,除了UPDATE查询需要约 20 秒的时间来处理约 2000 行的小文件。
临时表如下所示:
CREATE TEMPORARY TABLE tmp_products (
product_id integer,
detail text
);
Run Code Online (Sandbox Code Playgroud)
我的更新查询非常简单:
UPDATE products
SET detail = t.detail
FROM tmp_products t
WHERE t.product_id = products.product_id
Run Code Online (Sandbox Code Playgroud)
为了加快速度,我尝试了以下方法,但收效甚微:
在临时表上创建 BTREE 索引。
CREATE INDEX tmp_products_idx
ON tmp_products
USING BTREE
(product_id);
Run Code Online (Sandbox Code Playgroud)
创建哈希索引:
CREATE INDEX tmp_products_idx
ON tmp_products
USING HASH
(product_id);
Run Code Online (Sandbox Code Playgroud)
这两个索引都没有显着改善更新时间。然后我想也许对表进行聚类会有所帮助,但这意味着我不能使用 HASH 索引。所以我修改了程序中的查询以使用 BTREE 索引,然后使用 CLUSTER/ANALYZE:
CREATE INDEX tmp_products_idx
ON tmp_products
USING BTREE
(product_id);
-- Program inserts data …Run Code Online (Sandbox Code Playgroud) 我有 PostgreSQL 9.2 数据库和一个表:
id integer,
allowed_types character varying(255)`
Run Code Online (Sandbox Code Playgroud)
样本数据如下:
id allowed_types
1 3,4,5,13,14
Run Code Online (Sandbox Code Playgroud)
如何从中删除4和5,allowed_types其中以逗号分隔的 varchar 列表?
删除后,结果应该是allowed_types = 3,13,14。
该表上有许多记录,每个记录allowed_types可以包含不同的数字,用逗号分隔。
我考虑过string_to_array()and array_remove(),但array_remove()还没有在 9.2 版中。
这是一个非常有趣的问题(针对 SQL Server 提出的问题),我想尝试一下,看看它是如何在 PostgreSQL 中完成的。让我们看看其他人是否可以做得更好。拿着这个数据,
CREATE TABLE foo
AS
SELECT pkid::int, numvalue::int, groupid::int
FROM ( VALUES
( 1, -1 , 1 ),
( 2, -2 , 1 ),
( 3, 5 , 1 ),
( 4, -7 , 1 ),
( 5, 1 , 2 )
) AS t(pkid, numvalue, groupid);
Run Code Online (Sandbox Code Playgroud)
我们正在尝试生成这个:
PKID RollingSum GroupID
----------------------------- ## Explanation:
1 0 1 ## 0 - 1 < 0 => 0
2 0 1 ## 0 - 2 < 0 …Run Code Online (Sandbox Code Playgroud) postgresql ×10
json ×2
performance ×2
aggregate ×1
array ×1
btree ×1
clustering ×1
date ×1
gist-index ×1
index ×1
locking ×1
select ×1