简体   繁体   English

使用InnoDB或替代方法按组进行MySQL AUTO_INCREMENT

[英]MySQL AUTO_INCREMENT by group using InnoDB or alternatives

I am designing a web application using a MySQL database, and I am stuck on one of the fine points of my database design. 我正在设计一个使用MySQL数据库的Web应用程序,但我始终坚持数据库设计的优点之一。

Here's an example of the type of data I would like to store using the InnoDB engine: 这是我想使用InnoDB引擎存储的数据类型的示例:

user_id   account_id
1         1
1         2
1         3
2         1
2         2
3         1

As is clear from the above, I would like the account_id to auto increment for each individual user. 从上面可以明显看出,我希望account_id为每个用户自动递增。 Here are some problems I have encountered: 这是我遇到的一些问题:

  1. I do know that to do this automatically I could use the MyISAM engine, but after I have done some reading, I have come to the realization that when updates happen, this engine will lock the whole table instead of just one row. 我确实知道要自动执行此操作,可以使用MyISAM引擎,但经过阅读后,我意识到,当更新发生时,该引擎将锁定整个表而不是仅锁定一行。 This is no good to me. 这对我不好。 Also, MyISAM does not enforce referential integrity, which I would highly like to keep. 另外,MyISAM不强制执行参照完整性,我非常希望保留该参照完整性。

  2. I have read other people's solutions to this problem, and they have used a trigger. 我已经阅读了其他人对这个问题的解决方案,并且他们使用了触发器。 I could, of course, use a trigger, but I am not very fond of triggers, as they are more difficult to maintain than just one table. 我当然可以使用触发器,但是我并不喜欢触发器,因为它们比仅一张桌子更难维护。

  3. I could probably use a sequence, but I think that would be ugly to maintain a separate sequence for every user. 我可能可以使用一个序列,但是我认为为每个用户维护一个单独的序列是很丑陋的。

  4. I could simply use a unique AUTO_INCREMENT without any regard to the user_id, which would work very well. 我可以简单地使用唯一的AUTO_INCREMENT而无需考虑user_id,这会很好地工作。 There are two things about this I do not like: 1. This way, my smallint would potentially grow to int or bigint, which would require a lot MORE storage space. 我对此不满意有两件事:1.这样,我的smallint可能会增长为int或bigint,这将需要更多的存储空间。 I know that storage space is cheap, but I would still like to minimize it. 我知道存储空间很便宜,但是我仍然想将其最小化。 2. It would be much cleaner to auto increment by group. 2.按组自动递增会更清洁。

I would like to get some feedback/opinions of those users that have encountered and successfully solved this problem. 我想对已经遇到并成功解决此问题的那些用户提供一些反馈/意见。 Perhaps, there is another solution that I haven't thought of. 也许,还有我没有想到的另一种解决方案。 Perhaps, there is another way to organize my table that I haven't thought of. 也许,还有另一种我没想到的组织表格的方式。

Use a simple AUTO_INCREMENT and ignore the storage: 使用简单的AUTO_INCREMENT并忽略存储:

  • If you scale to a million users, each having 1000 accounts, you will need approx. 如果您要扩展到一百万个用户(每个用户有1000个帐户),则大约需要1000个帐户。 3MB of additional storage. 3MB的额外存储空间。
  • The performance benefit of a single-field primary index (mostly sorted in real storage!) will be worth much more, than that bit of storage 单字段主索引(主要在实际存储中排序)的性能优势将比那一点存储价值更多。

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

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