| | |
Getting Random Record from Query
Please support our MySQL advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Jul 2008
Posts: 20
Reputation:
Solved Threads: 0
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.
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
•
•
Join Date: Dec 2008
Posts: 11
Reputation:
Solved Threads: 0
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:
That's just an example. You may modify it to suit your need. Let us know if this helps.
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:
MySQL Syntax (Toggle Plain Text)
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.
•
•
Join Date: Jul 2008
Posts: 20
Reputation:
Solved Threads: 0
I actually only get 2 fields, but I'd paraphrased my query to shorten it.
Dragonfly Services - Same Day Courier
•
•
Join Date: Jul 2008
Posts: 20
Reputation:
Solved Threads: 0
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
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 !!!
![]() |
Similar Threads
- having problem with random access files (C++)
- Problem with Access Random Record Generator. (ASP.NET)
- Random Comment Selection (PHP)
- Need urgent help. Problem with SQL and win server. (Windows NT / 2000 / XP)
- mock credit card with mysql & filewriter INSERT into mysql (Java)
- verify page not finding random records (ASP)
- Generate User ID (PHP)
- need your help with database access (Visual Basic 4 / 5 / 6)
- Fast retrieving of records (MS SQL)
- Seeting up and calling variables stored in sessions (PHP)
Other Threads in the MySQL Forum
- Previous Thread: Getting results from using 2 tables
- Next Thread: Help table partition design mysql
| Thread Tools | Search this Thread |
agplv3 alfresco amazon api artisticlicense aws bizspark breathalyzer camparingtocolumns changingprices cmg communityjournalism contentmanagement contractors copyright court crm database design developer development distinct dui ec2 email enterprise eudora facebook form foss gartner gnu government gpl greenit groklaw groupware hiring hyperic images innerjoins insert ip joebrockmeier join journalism keyword keywords kickfire laptop law legal license licensing linux maintenance managing mariadb matchingcolumns metron microsoft microsoftexchange mindtouch montywidenius mozilla multiple music mysql mysqlcolumnupdating mysqldatetimeordermax() mysqlindex mysqlinternalqueries mysqlquery mysqlsearch news open-xchange opendatabasealliance opengovernment opensource oracle penelope php priceupdating query referencedesign reorderingcolumns resultset saas select sharepoint simpledb sourcecode spotify sql sugarcrm syntax techsupport thunderbird transparency virtualization





