Solution: In No Particular Orderâ¦
The sort-by-random technique is an example of a query thatâs bound to perform a table scan and an expensive manual sort. When you design solutions in SQL, you should be on the lookout for inefficient queries like this. Instead of searching fruitlessly for a way to optimize an unoptimizable query, rethink your approach. You can use one of the alternative techniques shown in the following sections to query a random row from a query result set.
Choose a Random Key Value Between MIN and MAX
One technique that avoids sorting the table is to choose a random value between the least primary key value and the greatest primary key value.
â | âSELECTâ MIN(bug_id), MAX(bug_id) â ... |
Get SQL Antipatterns, Volume 1 now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.