例子:
INTERVAL '5 minute',则所需输出为 2018-05-17 22:50:00。INTERVAL '10 minute',则所需输出为 2018-05-17 22:50:00。INTERVAL '1 hour',则所需的输出为 2018-05-17 23:00:00。INTERVAL '1 day',那么期望的输出是 2018-05-18 00:00:00。我刚刚开始学习 Postgres,我遇到的情况是,根据我在表上执行 JOIN 的方式,性能和计划输出似乎真的很奇怪。
这些是与其索引一起使用的表:
create table escola
(
pk_codigo integer not null
constraint pk_escola
primary key,
nome varchar(100),
municipio varchar(150),
uf char(2),
cod_municipio integer,
uf_id integer default 0 not null
constraint fk_escola_uf_id
references tb_uf
)
;
create index idx_escola_uf
on escola (uf)
;
create index idx_escola_uf_id
on escola (uf_id)
;
create index idx_multi_escola_uf_pk
on escola (uf, pk_codigo)
;
create table if not exists candidato
(
pk_numero_inscricao bigint not null
constraint candidato_pk
primary key,
cod_municipio_residencia integer,
municipio_residencia varchar(150),
uf_residencia char(2), …Run Code Online (Sandbox Code Playgroud) 我想将数据从 mysql 插入到 postgresql 中的表中。我想为特定的 id 执行 WHERE NOT EXISTS 语句。
# Source data: MySQL
curr_msql.execute(''' SELECT code, subjectname
FROM test_subj ''')
# Target data: PostgreSQL
for row in curr_msql:
curr_psql.execute(''' INSERT INTO subs (
created, modified,
subjcode, subjname,
is_pe_or_nstp)
VALUES (current_timestamp, current_timestamp,
%s, %s,
%s) ''', (row['code'], row['subjectname'],
False))
Run Code Online (Sandbox Code Playgroud)
我想知道如何将“False”替换为 case 语句。就像是
CASE
WHEN code like '%PE%' or code like '%NSTP%'
THEN True
ELSE False
END
Run Code Online (Sandbox Code Playgroud)
我尝试运行此代码:
for row in curr_msql:
curr_psql.execute(''' INSERT INTO subs (
created, modified,
subjcode, …Run Code Online (Sandbox Code Playgroud) SELECT something FROM table WHERE primary_key = ?
Run Code Online (Sandbox Code Playgroud)
对比
SELECT something FROM table WHERE primary_key = ? AND other_key = ?
Run Code Online (Sandbox Code Playgroud)
假设这是一个包含other_key不会改变结果集的场景。在实践中第二个查询更快吗?或者,如果提供了多个,数据库是否只使用一个最佳密钥?
我有一个 Postgres 物化视图:
Column | Type | Modifiers
---------------------+-------------------+-----------
document_id | character varying |
recorded_date | date |
parcels | jsonb |
Indexes:
"index_my_view_on_document_id" btree (document_id)
"index_my_view_on_recorded_date" btree (recorded_date)
"index_my_view_on_parcels" gin (parcels)
Run Code Online (Sandbox Code Playgroud)
我正在尝试执行一个分页查询,该查询在parcelsjsonb 数组字段上进行过滤,但是每当我添加 LIMIT 时,我的性能就会下降:
无限制:
EXPLAIN ANALYZE SELECT document_id FROM my_view WHERE (parcels @> '[3022890014]') ORDER BY recorded_date DESC;
QUERY PLAN
--------------------------------------------------------------------------------------------------------------------------------------------------------
Sort (cost=24178.50..24194.79 rows=6518 width=21) (actual time=11.272..11.275 rows=22 loops=1)
Sort Key: recorded_date DESC
Sort Method: quicksort Memory: 26kB
-> Bitmap Heap Scan on my_view (cost=78.51..23765.58 …Run Code Online (Sandbox Code Playgroud) 当我在我的表上运行查询时,我不知道这个过程需要多长时间。我正在处理一个包含 1100 万行的表,因此查询通常会运行几分钟 (AWS RDS)。有没有办法获得进度条或更好地了解查询需要多长时间?我通常对表的 1/10 执行查询以了解所需的时间。
编辑:我在笔记本中添加了我的具体情况
我喜欢运行的查询计算几列的移动平均值。下面复制了一个测试查询(仅 1 列,仅适用于年度分数,输出有限):
SELECT year,month, ptotww_m_30spfaf06, temporal_resolution,
SUM(ptotww_m_30spfaf06)
OVER(ORDER BY year ROWS BETWEEN 2 PRECEDING AND CURRENT ROW) as ptotwwma_m_30spfaf06
FROM y2018m05d29_rh_total_demand_postgis_30spfaf06_v01_v01
WHERE temporal_resolution = 'year'
LIMIT 200
Run Code Online (Sandbox Code Playgroud)
该示例已经需要 52 秒才能运行,并且不适用于每月值。1969 年 1 月的每月 10 年移动平均线应该是 1960 年 1 月、1961 年 1 月 ... 1969 年 1 月的平均值。
例如,给出这样的物化视图(Postgres 10.3):
create materialized view my_view as
select * from my_table where sell_date < '2018-03-01';
Run Code Online (Sandbox Code Playgroud)
在sell_date比较值('2018-03-01')有时可以改变,但我想,以避免掉落,每次重新创建物化视图。我想出的唯一想法是使用带有一些元数据值的外部表,例如所需的日期:
create materialized view my_view as
select * from my_table where sell_date <
(select original_sell_date from some_metadata_table);
Run Code Online (Sandbox Code Playgroud)
有没有其他方法可以解决 Postgres 上物化视图的这种限制?
我当前解决方案的问题之一是您可以有两个或多个使用相同值的物化视图,但它们可能需要在某些点使用不同的值。在这种情况下,需要复制元数据表。
我最近在使用 Azure Database for PostgreSQL 时注意到一个问题,我的内存使用量不断增长,达到 100% 时,服务器将停止响应。
这个数据库服务器专门用于开发,所以它有很多短期连接,经常被强行关闭(因为人们重新启动他们的应用程序来修复这里或那里的错误。)
在日志中,在发生这种情况之前,我可以看到两种模式,即自动清理错误:
2018-05-22 11:16:13 UTC-5ae5085b.20-LOG: CreateProcess call failed: No error (error code 1455)
2018-05-22 11:16:13 UTC-5ae5085b.20-LOG: could not fork autovacuum worker process: No error
2018-05-22 11:16:14 UTC-5ae5085b.20-LOG: CreateProcess call failed: A blocking operation was interrupted by a call to WSACancelBlockingCall.
(error code 1455)
Run Code Online (Sandbox Code Playgroud)
似乎是内存使用转储,然后是请求失败的警告:
TopMemoryContext: 143584 total in 6 blocks; 68072 free (43 chunks); 75512 used
TopTransactionContext: 8192 total in 1 blocks; 7960 free (0 chunks); 232 used
CFuncHash: 8192 total in …Run Code Online (Sandbox Code Playgroud) 在来这里打扰你之前,我试图尽可能多地记录我自己的话题,但无论如何我都在这里。
我们想在这个表上实现键集分页:
create table api.subscription (
subscription_id uuid primary key,
token_id uuid not null,
product_id uuid not null references api.product(product_id) deferrable,
spid bigint null,
attributes_snapshot jsonb not null,
created_at timestamp not null,
refreshed_at timestamp,
enriched_at timestamp null,
valid_until timestamp not null,
is_cancelled boolean not null,
has_been_expired boolean not null,
has_quality_data boolean not null
);
Run Code Online (Sandbox Code Playgroud)
为此,我们使用此查询来准备分页元数据:
with book as (
select created_at, subscription_id
from api.subscription
where token_id = $1
and refreshed_at >= $2
and valid_until >= now()
and not is_cancelled
and …Run Code Online (Sandbox Code Playgroud) 我有安装了ubuntu 18.04 服务器的VM 。我已经从 repo安装了postgres 10。
默认安装。我在 postgres.conf 中进行了一些与连接计数相关的更改,并在 pg_hba.conf 中进行了与连接安全相关的更改。
安装目录都是默认的,包括数据目录。
服务器正在运行。我能够创建数据库并从 java 客户端访问它。
但是当我从同一个 VM 启动 psql 时,它会引发以下错误。
Error: Invalid data directory
Run Code Online (Sandbox Code Playgroud)
只是没有任何参数的 psql 也会出现此错误。
人们在尝试更改数据目录时遇到了这个错误,这对我来说并非如此。数据目录是默认目录,它由 postgres 用户拥有。
ubuntu@ubuntu:~$ ls -l /var/lib/postgresql/10/
total 4
drwx------ 20 postgres postgres 4096 Jun 12 02:54 main
Run Code Online (Sandbox Code Playgroud)
知道可能是什么原因或我应该在哪里查找 psql 客户端的详细错误吗?
postgresql ×10
performance ×3
azure ×1
cte ×1
date-math ×1
datetime ×1
index ×1
interval ×1
join ×1
optimization ×1
paging ×1
ubuntu ×1