Explain Codes LogoExplain Codes Logo

Sqlite - ORDER BY RAND()

sql
random
performance
datasets
Alex KataevbyAlex Kataev·Aug 14, 2024
TLDR

To randomize row order in SQLite, use ORDER BY RANDOM():

SELECT * FROM table_name ORDER BY RANDOM();

The query will return rows in a random sequence every time it's executed. Like shaking the good old 8-ball, you never know what answer you might get!

Limiting number of random rows

To retrieve a set number of random rows, you can make use of the LIMIT clause. It's just like asking the cosmos for a certain number of random ideas.

SELECT * FROM table_name ORDER BY RANDOM() LIMIT 5;

The above expression fetches any 5 random rows from the table. Useful for pulling out a small random subset from your data.

Fast & random — the optimized way

For larger datasets, ORDER BY RANDOM() may cause a performance hit. Here's a workaround that performs better:

SELECT * FROM table_name WHERE id IN (SELECT id FROM table_name ORDER BY RANDOM() LIMIT 10);

Here, we're fetching 10 random ids first, then we get the full rows for these ids, reducing the number of rows RANDOM() has to work on. It's like the universe giving you a random shortcut!

No magic seeds in SQLite

Unlike MySQL's RAND(seed), SQLite's RANDOM() doesn't support seeding natively. If you want repeated randomness, you'd have to get creative with application-layer seeding or complex SQL substitutions. But remember, trying to control randomness, defeats the whole idea of being random, doesn't it?

Structuring the random in SQLite

SQL syntax — No, SQLite, not Sequel

Ensure to align your solution with SQLite's unique syntax and capabilities:

-- SQLite Syntax - Yes, you spell it as SQL, not sequel SELECT your_columns FROM your_table ORDER BY RANDOM();

SQLite is a different beast with its own tricks, compared to other SQL dialects.

We limit because we care

Limit your results if you're concerned about performance or browser-crashing memory hogs:

-- Because we men of science do not want to crash our browsers, do we? SELECT column FROM your_table ORDER BY RANDOM() LIMIT 5;

Be wise in handling data; you don't want to fetch all stars from the cosmos in one go!

To shuffle or not to shuffle

Avoid randomizing if you need reproducible ordering. Debugging unpredictable randomness feels like chasing a phantom!

Avoid endless pitfalls

Order by SIN(id + seed): A big nope

Methods like ORDER BY SIN(id + seed) are frowned upon. They're unpredictable, non-standard and often lead to non-random results. It's like calling a monkey's scribblings as fine art.

Healthy handling of large datasets

ORDER BY RANDOM() may become orthodox for lager datasets. Here, you might want to explore alternative strategies like different SQL methods (e.g., ROW_NUMBER) or per-generated random indexing for better performance. Remember, there's more than one way to flip a coin!