The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Special Forums > Web Programming, Web 2.0 and Mashups
.
google unix.com



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

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-02-2009
worchyld worchyld is offline
Registered User
  
 

Join Date: Mar 2009
Posts: 27
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
  #2 (permalink)  
Old 06-10-2009
figaro figaro is offline
Registered User
  
 

Join Date: Jan 2007
Posts: 267
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
  #3 (permalink)  
Old 06-10-2009
worchyld worchyld is offline
Registered User
  
 

Join Date: Mar 2009
Posts: 27
Many thanks, I have resolved the issue by another means.

Thanks again!
Sponsored Links
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 07:09 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0