id management for database tables with temporary entries
What is the best way to handle database table ids, if entries of that table are expected to be created and deleted frequently? Basically, most entries are temporary over a timespan of a few days. However, entries might stay in the table for an arbitrary amount of time as well.
Using an integer or long id with Auto Increment might be problematic after a certain time, when the maximum key value is reached. Considering a large user base, where most users will probably create a dozen or more entries each day, this might happen sooner than expected...
Therefore the question. Is there some mechanism with MySQL or another database to get looping ids or any other solution to handle this problem with good performance?
Btw. this is in context of a Ruby on Rails application. I was thinking about having nested ids, i.e. make an entry unique on user_id and user_entr开发者_高级运维y_id. But I don't know, how well this approach is supported by rails.
Edit: I see I should have done some calculation on how likely it is that the id range will run out anytime soon. I guess I don't have to worry about that problem anytime soon. Still happy, if people can report some experiences related to that problems. If anybody ever had problems with that....
Yep, you'd want to create a composite key as D.N. has stated, here's another post which explains how that works/why you'd want it. (This post is in SQL-Server but MySQL is much the same.
Why use multiple columns as primary keys (composite primary key)
RoR supports multiple primary-key columns. This is very common database practice. (you do need the extension though)
I would look at using a UUID http://www.rubyflow.com/items/5717-uuid-primary-keys-with-activerecord-in-rails-3
精彩评论