-1

Possible Duplicate:
quick selection of a random row from a large table in mysql
Selecting random rows with MySQL

I am currently making a module to pull random business records from the mysql database and display the record as a 'featured business' inside the module. It all works fine, but im not 100% if this is the best way of doing it, as I hear its not overly fast ?. At present it is pretty rapid, and I should think the max amount of businesses I will get is approx 100-200, so is it ok to use the method below, or will things be a lot slower with that amount of records ?.

Many thanks :-)

<?php
$result = mysql_query("SELECT * FROM `zgjzb_chronoforms_data_submitbusiness` ORDER BY     RAND() LIMIT 0,4;");
$row = mysql_fetch_array($result);
echo $row['businessname'];
?>
Community
  • 1
  • 1
Iain Simpson
  • 8,011
  • 13
  • 47
  • 66

2 Answers2

2

See this question: MySQL select 10 random rows from 600K rows fast

There are "better" ways of doing what you want.

At 100-200 businesses, this probably won't make much of a difference. Even in a couple thousand. The reason why your current query is considered "slow" is that MySQL is creating a temporary table in memory to process the query.

So you should be fine with what you have for now (well, for a while).

P.S. the mysql_* functions are now outdated. You should use either mysqli or PDO. Here is the PHP Manual on the differences.

Community
  • 1
  • 1
cegfault
  • 6,442
  • 3
  • 27
  • 49
0

You will be totally fine with that many records. Even if you are in the thousands you will be fine. Sql queries almost always impress me with their speed, and for queries with so few records, you'll be fine.

ajon
  • 7,868
  • 11
  • 48
  • 86