I want to have a PHP code to draw some ids one by one without replacement randomly from a table in MySQL database.
Now I implement it like this:
There is a table called box
CREATE TABLE box
(
id INT UNSIGNED AUTO_INCREMENT,
PRIMARY KEY (id)
);
Say it contains 15 records,
box
id:
=====
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
=====
The ids that have been drawn will be recorded and excluded in next draw.
For example,
at the start, the SQL for drawing will be
SELECT id FROM box RAND() LIMIT 1;`.
Say the result is 4.
Then I have drawn 4, the next SQL for drawing will be
SELECT id FROM box WHERE id != 4 ORDER BY RAND() LIMIT 1;
Say the result is 6 this times.
That is, I have drawn 4 and 6, so the next SQL for drawing will be
SELECT id FROM box WHERE id != 4 AND id != 6 ORDER BY RAND() LIMIT 1;"
The problem is that:
If the table ‘box’ contains 2000 records and I have drawn 1000+ ids, the next SQL for drawing will be very long. I wonder would a longer SQL statment make the program or the process in MySQL run slower? If yes, is there a solution/design/algorithm to solve this problem? The records in the table may increase so I cannot make a random sequence on ids before the drawing.
More about the problem:
the PHP code will be called by multiple users, so each user has some different ids that have been drawn by him/her.
Actually, in my current design, the ids that have been drawn by a user are stored in PHP session variable. When a user wants to draw, the ids stored in session variable will be accessed for the drawing SQL as above.
Most of the queries written will take approximately the same time as your query.
By the way the conditions in your query are equivalent to a NOT IN and it may easier for you to apply a NOT IN
Now as to whether the increased size of the query will slow down MySQL the answer is no, not really.
An article about why it won’t slow it down is here (remember your query is equivalent to a not in)