我有一个 PostgreSQL 表。select *
很慢,但又select id
好又快。我认为可能是行的大小非常大并且需要一段时间来运输,或者可能是其他一些因素。
我需要所有字段(或几乎所有字段),因此仅选择一个子集不是一个快速解决方案。选择我想要的字段仍然很慢。
这是我的表架构减去名称:
integer | not null default nextval('core_page_id_seq'::regclass)
character varying(255) | not null
character varying(64) | not null
text | default '{}'::text
character varying(255) |
integer | not null default 0
text | default '{}'::text
text |
timestamp with time zone |
integer |
timestamp with time zone |
integer |
Run Code Online (Sandbox Code Playgroud)
文本字段的大小可以是任意大小。但是,在最坏的情况下,不会超过几千字节。
postgresql performance size disk-space postgresql-performance
我想从Postgres 文档中询问这个片段关于varchar(n)
类型的含义:
短字符串(最多 126 个字节)的存储要求是 1 个字节加上实际字符串,其中包括字符情况下的空格填充。较长的字符串有 4 个字节的开销而不是 1 个字节。
假设我有一个varchar(255)
字段。现在,以下声明:
我们一再看到尝试索引值超过最大大小的列失败。Postgres 10 有这样的错误信息:
Run Code Online (Sandbox Code Playgroud)ERROR: index row size xxxx exceeds maximum 2712 for index "foo_idx" HINT: Values larger than 1/3 of a buffer page cannot be indexed. Consider a function index of an MD5 hash of the value, or use full text indexing.
例子:
等等。
现在,a_horse_with_no_name 演示了一个具有更大text
值(10000 个字符)的案例,它似乎仍然适用UNIQUE
于 Postgres 9.6 中的索引。引用他的测试用例:
create table tbl (col text);
create unique index on tbl (col);
insert into tbl
values (rpad(md5(random()::text), 10000, md5(random()::text)));
select length(val) …
Run Code Online (Sandbox Code Playgroud) 我们在 Postgres 9.2.10 数据库中有一个大约有 20 列的表。为了在某些SELECT
查询上获得更好的性能,我们计划在数据类型为 的列上添加索引timestamp
。由于索引也会降低插入的性能,我们做了以下性能测试:
我们在表中插入了 500 万条记录。那是最大值。我们期望在生产中的记录数。然后我们测量了在时间戳列上插入有索引和没有索引的 10000 条记录的时间。这是我们每天预期的最大插入次数,峰值每秒不超过 5 次插入。
结果如下:
至少对于本次测试,该指数仅略微降低了性能。对于我们的要求,我没有看到添加索引的问题。
但这只是实验室环境中的一项测试,在生产数据库上运行时是否还有其他陷阱?我们是否会遇到INSERT
在特定情况下突然需要超过 5 秒的情况?
postgresql ×4
index ×2
performance ×2
size ×2
disk-space ×1
index-tuning ×1
limits ×1
varchar ×1