Problem Statement:
Extracting a Simple Random Sample (SRS) from a large MySQL database efficiently can be challenging using the "obvious" approach (SELECT * FROM table ORDER BY RAND() LIMIT n). This method's inefficiency stems from its usage of RAND() for each row and subsequent sorting, resulting in resource-intensive O(n lg n) complexity.
Efficient Solution:
To overcome this hurdle, consider employing a more efficient approach:
SELECT * FROM table WHERE RAND() <= 0.3
This solution outperforms the "obvious" method due to its ability to generate a random number for each row between 0 and 1, then evaluate whether to display that row based on a probability threshold (0.3 in this case).
Explanation:
Additional Considerations:
The above is the detailed content of How Can I Efficiently Extract a Simple Random Sample from a Large MySQL Database?. For more information, please follow other related articles on the PHP Chinese website!