![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| Web Programming, Web 2.0 and Mashups Discuss Web Programming and Web Server Administration, including LAMP, Apache, MySQL, Flash, HTML, SEO, Mashups and other Web APIs and topics. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Carsten's Random Ramblings on MySQL, PHP, programming and stuff | iBot | Solaris BigAdmin RSS | 0 | 02-04-2009 02:30 PM |
| Mac OS X 10.5: Time Machine doesn't back up to AirPort Extreme AirPort Disks | iBot | OS X Support RSS | 0 | 11-17-2008 12:10 PM |
| AirPort Quick Assist | iBot | OS X Support RSS | 0 | 11-14-2008 04:50 PM |
| Mac OS X 10.5.5: Can't turn AirPort on after turning it off and restarting | iBot | OS X Support RSS | 0 | 10-27-2008 11:30 AM |
| Connecting Solaris to Airport network | giax | SUN Solaris | 1 | 07-17-2007 02:05 AM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
||||
|
MySQL: Random offers for every airport
I need to write a MySQL query where it loops through every airport and generates 10 random offers (my offers table is 990,000+ rows)
The code I have below works for 1 airport, but as soon as it gets bigger it slows RIGHT down. This is using PHP/MySQL At the moment it takes well over a minute to run which is way too long -- I need it much, much quicker. Can anyone help? Code:
/**
* This code takes over a minute to run. How do I make it quicker?
*/
// Get airports
$sql0 = 'SELECT airportcode FROM airports ORDER BY airportcode ASC';
$query = $this->db->query($sql0);
// Loop through airports
foreach ($query->result() as $row)
{
// Get a random offer so long as the airport matches
$sql1 = 'SELECT T.id, T.DepAirportCode
FROM offers T
JOIN (
SELECT FLOOR( MAX( id ) * RAND( ) ) AS id
FROM offers
) AS x ON T.id >= x.id
WHERE
T.DepAirportCode="'.$row->airportcode.'"
LIMIT
10';
// Loop through random offers
$query1 = $this->db->query($sql1);
foreach ($query1->result() as $row)
{
print $row->id;
print $row->DepAirportCode;
} // next
} // next
|
|
||||
|
You need to combine the two queries into 1 query and so that at least the WHERE clause looks as follows:
WHERE T.DepAirportCode=airports.airportcode Have a read of this to select a random row: SQL to Select a random row from a database table |
|
||||
|
Many thanks, I have resolved the issue by another means.
Thanks again! |
| Sponsored Links | ||
|
|