English 中文(简体)
• 如何在MySQL中执行交易安全随机顺序
原标题:How to implement a transaction-safe, random sequence in MySQL

因此,我很想让用户产生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预产的代码上拥有巨大的指数)。

最佳回答

如果你已经有一个储存表,我只增加一个用户栏,然后处理这一询问:

UPDATE repository SET user_id = ? WHERE user_id IS NULL LIMIT N;

此后,你可以再次选择记录。 这有两个不同的不利之处:

  • you need an index on user_id
  • you can t use the codes in your table for anything else but binding it to users.
问题回答

暂无回答




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签