We help IT Professionals succeed at work.

Optimizing a mysql query (random set mt_rand)

seopti
seopti asked
on
666 Views
Last Modified: 2013-12-12
I use a query to get random values from DB, which works fine, but the load on the server is quite large.
Can you think of a way optimizing this query?


<?php
$maxRs = mysql_query("select bizID from this_database WHERE bizState ='$bizState' AND bizLive='1'");
$maxRows = mysql_num_rows($maxRs);
$recordNumber = mt_rand(1 , $maxRows );
$rs = mysql_query("select bizName,bizAddr,bizCity,bizState,bizZip,bizPhone from this_database WHERE bizState ='$bizState' AND bizLive='1' LIMIT $recordNumber,1");
$row = mysql_fetch_array($rs, MYSQL_NUM);
?>

Open in new window

Comment
Watch Question

Cem TürkSenior Software Engineer

Commented:
<?
php


$rs 
=
 mysql_query
(
"select bizName,bizAddr,bizCity,bizState,bizZip,bizPhone from this_database WHERE bizState ='$bizState' AND bizLive='1' ORDER BY rand() LIMIT 1"
);

$row 
=
 mysql_fetch_array
(
$rs
,
 MYSQL_NUM
);



?>

Open in new window

Author

Commented:
Thanks but ORDER BY RAND() is even using 10x MORE server resouces, so this no solution to my problem.

Commented:

<?php
$rs = mysql_query("select bizName, bizAddr, bizCity, bizState, bizZip, bizPhone from this_database WHERE bizState ='$bizState' AND bizLive='1' LIMIT $recordNumber,1");
$max = mysql_num_rows($rs);
$int = mt_rand(1, $max);
echo mysql_result($rs, $int, 'bizName');
echo mysql_result($rs, $int, 'bizAddr');
//etc..
?>

Open in new window

Cem TürkSenior Software Engineer

Commented:
<?
php
$maxRs 
=
 mysql_query
(
"select max(bizID) from this_database WHERE bizState ='$bizState' AND bizLive='1'"
);

$maxID 
=
 mysql_result
(
$maxRs
,0);

$randomID 
=
 mt_rand
(1

 
,
 $maxID 
);

$rs 
=
 mysql_query
(
"select bizName,bizAddr,bizCity,bizState,bizZip,bizPhone from this_database WHERE bizID=" . $randomID
);

$row 
=
 mysql_fetch_array
(
$rs
,
 MYSQL_NUM
);



?>

or


<?
php
$maxRs 
=
 mysql_query
(
"select max(bizID) from this_database WHERE bizState ='$bizState' AND bizLive='1'"
);

$maxID 
=
 mysql_result
(
$maxRs
,0);

$randomID 
=
 mt_rand
(0
 
,
 $maxID 
);

$rs 
=
 mysql_query
(
"select bizName,bizAddr,bizCity,bizState,bizZip,bizPhone from this_database WHERE bizID>" . $randomID
 . " LIMIT 1");

$row 
=
 mysql_fetch_array
(
$rs
,
 MYSQL_NUM
);



?>

Open in new window

Cem TürkSenior Software Engineer

Commented:
here is another approach

<?php
$maxRs = mysql_query("select bizID from this_database WHERE bizState ='$bizState' AND bizLive='1'");
$maxRows = mysql_num_rows($maxRs);
$recordNumber = mt_rand(1 , $maxRows );
$theID=mysql_result($maxRs,$recordNumber-1);
$rs = mysql_query("select bizName,bizAddr,bizCity,bizState,bizZip,bizPhone from this_database WHERE bizID=" . $theID . " LIMIT 1");
$row = mysql_fetch_array($rs, MYSQL_NUM);
?>

Open in new window

Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.