查询SQLite表中的免费ID的最快方法是什么?

时间:2022-11-12 23:03:10

I heard that SQLite has special processing for ID columns and is stored with a special type, "PRIMARY NUMERIC KEY".

我听说SQLite对ID​​列进行了特殊处理,并以特殊类型“PRIMARY NUMERIC KEY”存储。

So what's the fastest query I can write to fetch a free ID? Assuming that all ID's before that one are occupied. Is MAX(id) the best I can get?

那么我可以写什么来获取免费ID的最快查询?假设在那之前的所有ID都被占用了。 MAX(id)是我能得到的最好的吗?

1 个解决方案

#1


3  

If you need to preallocate IDs, create a table that stores last allocated ID for every table. Preallocate an ID from it by incrementing the appropriate value and committing the change, then use it as needed, or forget.

如果需要预先分配ID,请创建一个表,用于存储每个表的最后分配的ID。通过递增适当的值并提交更改来预先分配ID,然后根据需要使用它,或者忘记。

This is a bit like sequences work in Oracle and Postgres.

这有点像Oracle和Postgres中的序列工作。

#1


3  

If you need to preallocate IDs, create a table that stores last allocated ID for every table. Preallocate an ID from it by incrementing the appropriate value and committing the change, then use it as needed, or forget.

如果需要预先分配ID,请创建一个表,用于存储每个表的最后分配的ID。通过递增适当的值并提交更改来预先分配ID,然后根据需要使用它,或者忘记。

This is a bit like sequences work in Oracle and Postgres.

这有点像Oracle和Postgres中的序列工作。