在 SQLite 中使用带有自动增量键的更新插入时如何避免消耗键 ID?

How to avoid consume key-id when using upsert with autoincrement key in SQLite?

我有一个 table 如下所示:

id是PK和自增

+-----+--------+--------+
| id  |  kind  |  count |
+-----+--------+--------+

当我使用 upsert 查询时,100 个请求同时 api。

INSERT INTO Views (kind, count)
VALUES(1, 1)
ON CONFLICT(kind)
DO UPDATE SET count = count + 1

我会得到:

+-----+--------+--------+
| id  |  kind  |  count |
+-----+--------+--------+
| 100 | kind-1 |   100  |
+-----+--------+--------+

可以这样操作 sqlite_sequence table:

UPDATE sqlite_sequence SET seq=(SELECT MAX(id) FROM Views) WHERE name="Views"

这将 "reset" 序列到上次使用的 id

然而,这根本没有用。在批量 UPSERT 期间更新 sqlite_sequence 肯定会破坏性能,并且在操作后这样做不会避免按键序列中的 "holes"。

另一种选择是全部重写id,但你真的需要这样吗?