简体   繁体   English

如何在InnoDB中回收auto_increment ID

[英]How to NOT recycle auto_increment IDs in InnoDB

I recently read that because of how InnoDB recalculates the AUTO_INCREMENT value when the server restarts, any records on the high end of the ID list may have their IDs reused. 我最近读到 ,由于InnoDB在服务器重新启动时重新计算AUTO_INCREMENT值,ID列表高端的任何记录都可能重用其ID。

Normally, this isn't a problem, because when a user is deleted everything associated with the ID is deleted from other tables too. 通常,这不是问题,因为当删除用户时,与ID相关联的所有内容也会从其他表中删除。

But I'm deliberately leaving their forum posts orphaned, labelled as "Posted by =User #123= ", so that past conversations are retained. 但我故意将他们的论坛帖子孤立,标记为“发布者=用户#123 = ”,以便保留过去的对话。 Clearly, should an ID be reused, this will be a problem. 显然,如果ID被重用,这将是一个问题。

I've never had this issue before because there were always enough new users to make it unlikely for an ID to be reused in this way. 我之前从未遇到过这个问题,因为总有足够的新用户使得ID不太可能以这种方式重用。 However on my new project signups are rare and inactive user deletions frequent (especially since the "Open Alpha" accounts only last for three days as a preview), and such ID reuse has happened three for three now. 然而,在我的新项目中,注册是罕见且不活跃的用户删除频繁(特别是因为“Open Alpha”帐户仅作为预览持续三天),并且此类ID重用现在已经发生了三次。

I have "fixed" the issue by saving the correct value for AUTO_INCREMENT elsewhere and using that instead of relying on the internal value. 我通过在其他地方保存AUTO_INCREMENT的正确值并使用它而不依赖于内部值来“修复”了该问题。 Is there an actual way to have InnoDB remember the actual last value? 是否有一种实际的方法让InnoDB记住实际的最后一个值?

The 5.5 docs suggests storing the auto-increment value elsewhere as you already have. 5.5文档建议将自动增量值存储在您已有的其他位置。

An alternative solution would be to emulate a SEQUENCE so you don't use auto-increment in the actual table itself. 另一种解决方案是模拟SEQUENCE,这样就不会在实际表本身中使用自动增量。 This has been discussed on SO before and again . 这已经在SO讨论过 The MYSQL Performance blog mentions it . MYSQL Performance博客提到了它

Yet another MySQL data screwing that other RDBMS don't have... 另一个MySQL数据搞砸了其他RDBMS没有...

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

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