我有这个 SQL:
CREATE TABLE test(id SERIAL PRIMARY KEY, data JSONB);
INSERT INTO test(data) VALUES
('{"parent":null,"children":[2,3]}'),
('{"parent":1, "children":[4,5]}'),
('{"parent":1, "children":[]}'),
('{"parent":2, "children":[]}'),
('{"parent":2, "children":[]}');
Run Code Online (Sandbox Code Playgroud)
那会给:
id | data
----+--------------------------------------
1 | {"parent": null, "children": [2, 3]}
2 | {"parent": 1, "children": [4, 5]}
3 | {"parent": 1, "children": []}
4 | {"parent": 2, "children": []}
5 | {"parent": 2, "children": []}
Run Code Online (Sandbox Code Playgroud)
当进行正常的一对多时,它会显示如下内容:
SELECT *
FROM test x1
LEFT JOIN test x2
ON x1.id = (x2.data->>'parent')::INT;
id | data | id | …Run Code Online (Sandbox Code Playgroud) 我station_logs在 PostgreSQL 9.6 数据库中有一个表:
Column | Type |
---------------+-----------------------------+
id | bigint | bigserial
station_id | integer | not null
submitted_at | timestamp without time zone |
level_sensor | double precision |
Indexes:
"station_logs_pkey" PRIMARY KEY, btree (id)
"uniq_sid_sat" UNIQUE CONSTRAINT, btree (station_id, submitted_at)
Run Code Online (Sandbox Code Playgroud)
我试图level_sensor根据submitted_at, 对于每个station_id. 大约有 400 个唯一station_id值,每个station_id.
创建索引之前:
EXPLAIN ANALYZE
SELECT DISTINCT ON(station_id) station_id, submitted_at, level_sensor
FROM station_logs ORDER BY station_id, submitted_at DESC;
Run Code Online (Sandbox Code Playgroud)
唯一(成本=4347852.14..4450301.72行=89宽度=20)(实际时间=22202.080..27619.167行=98循环=1) -> Sort …
postgresql performance greatest-n-per-group postgresql-9.6 query-performance
是jsonb_column @> '{"key":value}'::jsonb运营商等于jsonb_column->'key' = value?在将使用的结果、性能和索引方面?
我正在使用ArchLinux和PostgreSQL9.4.4 并在配置文件中启用了日志记录:
$ sudo egrep -v "^[[:blank:]]*($|#|//|/\*| \*|\*/)" /var/lib/postgres/data/postgresql.conf
max_connections = 1024 # (change requires restart)
shared_buffers = 128MB # min 128kB
dynamic_shared_memory_type = posix # the default is the first option
logging_collector = on # Enable capturing of stderr and csvlog
log_directory = '/tmp' # directory where log files are written,
log_filename = 'postgresql-%Y-%m-%d_%H%M%S.log' # log file name pattern,
log_file_mode = 0644 # creation mode for log files,
log_error_verbosity = verbose # terse, default, or …Run Code Online (Sandbox Code Playgroud) 我们可以为 JSONB 数据类型的键/值创建索引吗?
例如,对于这些架构:
CREATE TABLE x (
id BIGSERIAL,
data JSONB
);
CREATE TABLE y (
id BIGSERIAL,
data JSONB
);
Run Code Online (Sandbox Code Playgroud)
慢查询:
SELECT *
FROM x
LEFT JOIN y
ON (y.data->>'x_id')::BIGINT = x.id
Run Code Online (Sandbox Code Playgroud)
如何为该索引创建y.data->>'x_id'可用于此类查询的索引?
postgresql performance index postgresql-9.4 postgresql-performance
我们有使用Golang、PostgreSQL和sqlx(适配器、连接池)的Web 应用程序,每个请求需要 1 到 8 个查询,有时 1 个事务需要 5-8 个选择和 5-8 个插入查询。
我们已经将 PostgreSQL 设置max_connections为 1024,然后是 4096,但是它开始交换,所以我们将它们减少到 64(这是交换前的限制)。
我们的 RAM 是 2GB,我们将 PostgreSQL 配置working_mem为 16MB、temp_buffers8MB、shared_buffers800MB、effective_cache_size 1536MB(这个配置根本不交换)。
每天 00:00 到 02:35 每秒大约有 18 个请求,每秒大约有 8 个错误,此时每小时只有 10 个事务成功,其他事务失败,错误消息如下:
除了缓存只读页面(因为页面每分钟可以更新10次左右)或升级机器之外,如何克服这个问题?
如何创建INSERT INTO语句,使用SELECT语句?我需要它,因为我想从表中删除 10k 条记录,但是如果有一天需要恢复其中的某些记录,我想轻松地做到这一点。我不想备份整个表,因为我只需要恢复一些已删除的行。
备份 cassandra 或 scyladb 以便我们可以轻松在开发环境中恢复它的正确(建议)方法是什么?
为什么我的 PostgreSQL 数据库文件夹中有以 .1 .2 .3 结尾的文件?
-rw------- 1 postgres postgres 1073741824 Oct 11 09:32 412rw95.2
-rw------- 1 postgres postgres 67100672 Oct 11 09:32 41295.3
-rw------- 1 postgres postgres 1073741824 Oct 11 09:07 41296
-rw------- 1 postgres postgres 1073741824 Oct 11 07:27 41296.1
Run Code Online (Sandbox Code Playgroud) postgresql ×6
performance ×2
backup ×1
cassandra ×1
index ×1
join ×1
json ×1
log ×1
scalability ×1
scylladb ×1