Getting Random Record from Query

Reply

Join Date: Jul 2008
Posts: 20
Reputation: dragonflyuk is an unknown quantity at this point 
Solved Threads: 0
dragonflyuk dragonflyuk is offline Offline
Newbie Poster

Getting Random Record from Query

 
0
  #1
Dec 9th, 2008
I've got quite a slow query, that I need to get a random record from, however this is proving painfully slow, can anyobdy suggest ways to speed up the following query.

SELECT * FROM people WHERE names like "%bert%" or names like "%john smith%".......... or names like "%eric%" ORDER BY RAND() LIMIT 0,1

bear in mind their could be 50+ names in the list hence the ............

The query does infact work, but is that slow that the PHP code that runs it times out waiting for the answer.
Dragonfly Services - Same Day Courier
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 11
Reputation: indianrock9 is an unknown quantity at this point 
Solved Threads: 0
indianrock9 indianrock9 is offline Offline
Newbie Poster

Re: Getting Random Record from Query

 
0
  #2
Dec 10th, 2008
Hi there,
One common mistake a newbie makes is using *. From your query, I can see that you are using '*' to get all the fields. Do you really need all the fields in your query? To speed it up, you can remove * & in fact, specify which field(s) you want to retrieve. It will boost your query speed a lot.

So instead of using *, try something like this:
  1. SELECT field1, field2,....field10 FROM people WHERE names LIKE "%bert%" OR names LIKE "%john smith%".......... OR names LIKE "%eric%" ORDER BY RAND() LIMIT 0,1

That's just an example. You may modify it to suit your need. Let us know if this helps.
Last edited by indianrock9; Dec 10th, 2008 at 1:29 am.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 20
Reputation: dragonflyuk is an unknown quantity at this point 
Solved Threads: 0
dragonflyuk dragonflyuk is offline Offline
Newbie Poster

Re: Getting Random Record from Query

 
0
  #3
Dec 10th, 2008
I actually only get 2 fields, but I'd paraphrased my query to shorten it.
Dragonfly Services - Same Day Courier
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 20
Reputation: dragonflyuk is an unknown quantity at this point 
Solved Threads: 0
dragonflyuk dragonflyuk is offline Offline
Newbie Poster

Re: Getting Random Record from Query

 
0
  #4
Dec 10th, 2008
I have found it is considerably quicker to do the query without the rand, then get the record count in php and pick a random record. Don't know if that helps anybody infuture.
Dragonfly Services - Same Day Courier
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 822
Reputation: verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough 
Solved Threads: 73
verruckt24's Avatar
verruckt24 verruckt24 is offline Offline
Practically a Posting Shark

Re: Getting Random Record from Query

 
0
  #5
Dec 10th, 2008
Can you detail a bit on what you exactly need to do rather than showing us what to do it through, so we can show you a completely different way, if there exists, to do the same thing efficiently. Also use EXPLAIN in front of your query for MySQL to explain how your query is performing then analyze the output so you might get clues what actually is slowing your query down.
Get up every morning and take a look at the Forbes' list of richest people. If your name doesn't appear.... GET TO WORK !!!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC