简体   繁体   English

我可以在Heroku上的5 MB PostgreSQL中存储多少条记录?

[英]How many records can I store in 5 MB of PostgreSQL on Heroku?

I'm going to store records in a single table with 2 fields: 我要将记录存储在一个包含2个字段的表中:

  • id -> 4 characters id - > 4个字符

  • password_hash -> 64 characters password_hash - > 64个字符

How many records like the one above will I be able to store in a 5mb PostgreSQL on Heroku? 我可以在Heroku上的5mb PostgreSQL中存储多少个像上面那样的记录?

PS: given a single table with x columns and a length of y - how can I calculate the space it will take in a database? PS:给定一个包含x列且长度为y的表 - 如何计算数据库中的空间?

Disk space occupied 磁盘空间占用

Calculating the space on disk is not trivial. 计算磁盘空间并非易事。 You have to take into account: 你必须考虑到:

  • The overhead per table (small, basically the entries in the system catalog, may not affect you on Heroku). 每个表的开销(小,基本上是系统目录中的条目,可能不会影响你在Heroku上)。

  • The overhead per row (HeapTupleHeader) and per data page (PageHeaderData). 每行开销 (HeapTupleHeader)和每个数据页面(PageHeaderData)。 Details about page layout in the manual . 手册中有关页面布局的详细信息。

  • Space lost to data type alignment . 数据类型对齐丢失的空间。

  • Space for a NULL bitmap . NULL位图的空间。 Effectively free for tables of 8 columns or less, irrelevant for your case. 有效免费使用8列或更少的表格,与您的情况无关。

  • Dead rows after UPDATE / DELETE . UPDATE / DELETE后的死行

  • Size of index(es) . 索引的大小。 You'll have a primary key, right? 你会有一把主键,对吧? Index size is similar to that of a table with just the indexed columns and less overhead. 索引大小类似于仅包含索引列且开销较少的表的索引大小。

  • The actual space requirement of the data, depending on the respective data types . 数据的实际空间要求,具体取决于各自的数据类型 Details for character types (incl. fixed length types) in the manual : 手册中字符类型(包括固定长度类型)的详细信息

    The storage requirement for a short string (up to 126 bytes) is 1 byte plus the actual string, which includes the space padding in the case of character . 短字符串(最多126个字节)的存储要求是1个字节加上实际字符串,其中包括character空间填充。 Longer strings have 4 bytes of overhead instead of 1 较长的字符串有4个字节的开销而不是1个字节

    More details for all types in the system catalog pg_type . 有关系统目录pg_type所有类型的更多详细信息。

  • The database encoding in particular for character types. 数据库编码特别适用于字符类型。 UTF-8 uses up to four bytes to store one character (But 7-Bit-ASCII characters always occupy just one byte, even in UTF-8.) UTF-8使用最多四个字节来存储一个字符(但是7位ASCII字符总是只占用一个字节,即使在UTF-8中也是如此。)

  • Other small things that may affect your case, like TOAST - which should not affect you with 64 character strings. 其他可能影响你案件的小事,比如TOAST--这对64字符串不会影响你。

Calculate with test case 用测试用例计算

A simple method to find an estimate is to create a test table, fill it with dummy data and measure with database object size functions: : 查找估计值的一种简单方法是创建测试表,用虚拟数据填充它并使用数据库对象大小函数进行测量:

SELECT pg_size_pretty(pg_relation_size('tbl'));

Including indexes: 包括索引:

SELECT pg_size_pretty(pg_total_relation_size('tbl'));

A quick test shows the following results: 快速测试显示以下结果:

CREATE TABLE test(a text, b text);
INSERT INTO test -- quick fake of matching rows
SELECT chr((g/1000 +32)) || to_char(g%1000, 'FM000')
     , repeat (chr(g%120 + 32), 64)
FROM   generate_series(1,50000) g;

SELECT pg_size_pretty(pg_relation_size('test'));       -- 5640 kB
SELECT pg_size_pretty(pg_total_relation_size('test')); -- 5648 kB

After adding a primary key: 添加主键后:

ALTER TABLE test ADD CONSTRAINT test_pkey PRIMARY KEY(a);

SELECT pg_size_pretty(pg_total_relation_size('test')); -- 6760 kB

So, I'd expect a maximum of around 44k rows without and around 36k rows with primary key. 所以,我预计最多大约44,000行没有,大约36k行有主键。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM