postgres:id自动增量,每天有数百万行。这不好吗?

时间:2022-09-23 15:48:50

in my database i save million of rows per day in a table and i delete all the rows after 2 weeks because i don't need them anymore.

在我的数据库中,我每天在表中节省数百万行,并且我在2周后删除所有行,因为我不再需要它们了。

i was wondering if it's good or bad to add a primary key id auto increment or i will hit too high numbers.

我想知道添加主键ID自动增量是否好或者我会打出太高的数字。

the table contains only inserts anytime an user issues a command with columns like the id of the user, the timestamp and the command. so to add a primary key i guess i necessarily need to add an auto-increment that will soon become very very high.

只要用户发出包含用户id,时间戳和命令等列的命令,该表就只包含插入。所以要添加一个主键我想我必须添加一个很快会变得非常高的自动增量。

should i avoid to add it considering the amount of inserts?

考虑到插入量,我应该避免添加吗?

2 个解决方案

#1


3  

It's not bad, unless it causes performance problems and you have proof that the auto increment is the source of those problems, which is unlikely.

它并不坏,除非它导致性能问题并且您已证明自动增量是这些问题的根源,这是不太可能的。

Frankly, 1 million rows per day is peanuts. You'll be fine. If you expect your scenario to continue for more than 3 years, you should use serial8 (64-bit) instead of serial (32-bit).

坦率地说,每天100万行是花生。你会没事的。如果您希望您的方案持续超过3年,则应使用serial8(64位)而不是串行(32位)。

#2


3  

The "auto-incrementing" functionality in Postgres is provided by the serial data types. These are described in the documentation.

Postgres中的“自动递增”功能由串行数据类型提供。这些在文档中描述。

If you are inserting millions of rows per day, then I would advise you to use bigserial. You won't have to worry about overflow or resetting the value.

如果你每天插入数百万行,那么我建议你使用bigserial。您不必担心溢出或重置值。

#1


3  

It's not bad, unless it causes performance problems and you have proof that the auto increment is the source of those problems, which is unlikely.

它并不坏,除非它导致性能问题并且您已证明自动增量是这些问题的根源,这是不太可能的。

Frankly, 1 million rows per day is peanuts. You'll be fine. If you expect your scenario to continue for more than 3 years, you should use serial8 (64-bit) instead of serial (32-bit).

坦率地说,每天100万行是花生。你会没事的。如果您希望您的方案持续超过3年,则应使用serial8(64位)而不是串行(32位)。

#2


3  

The "auto-incrementing" functionality in Postgres is provided by the serial data types. These are described in the documentation.

Postgres中的“自动递增”功能由串行数据类型提供。这些在文档中描述。

If you are inserting millions of rows per day, then I would advise you to use bigserial. You won't have to worry about overflow or resetting the value.

如果你每天插入数百万行,那么我建议你使用bigserial。您不必担心溢出或重置值。