2

I would like to select a random line in my database. I saw this solution on a website:

SELECT column FROM table
ORDER BY RAND()
LIMIT 1

This SQL query run but someone said me that it was a non performant query. Is there another solution ?

Thx

zerkms
  • 230,357
  • 57
  • 408
  • 498
Jensen
  • 121
  • 1
  • 1
  • 6

2 Answers2

3

It is. You have to count rows number with

SELECT COUNT(*) FROM `table`;

After this with php function mt_rand() get the random from 1 to $count and get it with query:

SELECT `column` FROM `table` LIMIT $rand, 1
zerkms
  • 230,357
  • 57
  • 408
  • 498
0

There's a lot more discussion of this subject, including performance implications and strategies for different DBMSs, in this question.

Community
  • 1
  • 1
Ken Redler
  • 22,899
  • 7
  • 55
  • 68
  • All the answers from that topic can be separated into 2 categories. 1. **slow** with using of RAND() (or DBMS-specific synonym) or 2. the calculating some random `id` in the range [1, id] and finding value that >= of that. and this solution gives us irregular randomize coverage (Ex: 1, 2, 3, 4, 5, 6, 7, 8, 9, 1000000000000. In this case in 99.99% of cases you will get the latest row). This is **not true uniform random** – zerkms May 27 '10 at 03:40
  • I think this is a matter of TANSTAAFL. Approaches like TABLESAMPLE clearly trade randomness for speed (and are vendor-specific). If you want to go down the rabbit-hole of seeking true randomness, you're adding a theoretical math problem to an implementation challenge. You could add a trigger that populates a column with a pseudo-random number on insert, "pre-slicing" your large table into manageable shards, then select a row, rand() limit 1, from one shard. So, pre-calculating some of the "fake" randomness. I agree that all these approaches involve compromise between purity and practicality. – Ken Redler May 27 '10 at 04:06