Actually ... I finished the test, and I may have answered my question. I thought I posted this information here if it were useful to someone else. (If I did something wrong, please let me know!)
It is amazing...
Unlike everything I read, I created a TestData table with 1 million rows and ran the following query:
SELECT * FROM TestData WHERE number = 41 ORDER BY RAND () LIMIT 8
... and he returned the rows on average 0.0070 seconds. I really don't understand why RAND () has such a bad reputation. I think this is useful to me, at least in this particular situation.
I have three columns in my table:
id [BIGINT (20)] | text field [tinytext] | number [BIGINT (20)]
Primary key by identifier, index by number.
I think MySQL is smart enough to know that it should only apply RAND () to the 20 rows that return "WHERE number = 41"? (I specifically added only 20 lines, which had a value of 41 for the "number".)
An alternative subquery method returns results with an average time of about 0.0080 seconds, which is slower than a method without a subquery.
Subquery Method: SELECT * FROM (SELECT * FROM TestData WHERE number = 41) as t ORDER BY RAND () LIMIT 8
source share