因此,我很想让用户产生random<>em> >甲型数字代码,如A6BU31、38QV3B、R6RK7T。 目前,它们由6个果园组成,而我和奥没有使用(因此我们有34个^6的可能性)。 这些代码随后印成并用于其他内容。
I must now ensure that many users can "reserve" up to 100 codes per request, so user A might want to get 50 codes, user B wants to generate 10 and so on. These codes must be unique across all users, so user A and user B may not both receive the code ABC123.
我目前的做法(使用PHP和MySQL)是为此设立两个非行表格:
- One (the "repository") contains a large list of pre-generated codes (since the possibility of collisions will increase over time and I do not want to go the try-insert-if-fails-try-another-code approach). The repository contains just the codes and an auto-incremented ID (so I can sort them, see below).
- The other table holds the reserved keys (i.e. code + owning user).
Whenever a user wants to reserve N keys, I planned to do the following
BEGIN;
INSERT INTO revered_codes (code,user_id)
SELECT code FROM repository WHERE 1 ORDER BY id LIMIT N;
DELETE FROM repository WHERE 1 ORDER BY id LIMIT N;
COMMIT;
http://em>should work,但我并不肯定。 似乎像我建造一个WTF解决方案一样。
After insertion I must select the just reserved codes to display them to the user. And that s the tricky part, since I don t really know how to identify the just reserved codes after my transaction is done. I could of course add just another column to my reserved_codes
table, holding some kind of random token, but this seems even more WTFy.
我赞成的解决办法是,有随机的编号顺序,以便我能够仅履行<条码>INSERT在<条码>保留_代码代码>表中的业务。
因此,我SQL是如何做到这一独特、随机和交易-安全顺序的? 一种想法是,在<编码> 保留-编码编码>表格上定期进行自动计算,并从该数字栏中得出随机编码价值,但我想知道,是否有更好的办法。
<><>UPDATE: 我想提一下的是,保留代码表要小一些,因为我后来不得不再次找到更新这些代码的单一编码(<条码>保留——代码代码>)。 有两个不同的属性。 使保留表缓慢增长(而不是在大约1mio预产的代码上拥有巨大的指数)。