Optimize booking script
<p>I inherited an apartment booking system. 
The system is based on Codeigniter, and runs pretty fast except when doing a booking.<br>
Doing that takes about 20 seconds to add a new booking. </p>

<p>The first part I want to optimize is this function in the model. </p>

<pre><code>function get_booking($apartment_id)  //Get all booked dates for a specific apartment
{
    $where = "apartment = ".$apartment_id."";
    $this-&gt;db-&gt;where($where);

    $query = $this-&gt;db-&gt;get('bookings');
    $daysbooked = 0;
    foreach ($query-&gt;result() as $item)
    {
        $staydates = explode(" ", $item-&gt;dates);
        $staylength = count($staydates);
        // Build a listing of all the days booked under all reservations to be used by the calendar
        // application.
        foreach ($staydates as $bday)
        {
            $booked_days[$daysbooked] = $bday;
            $daysbooked++;
        }

    }
    return $booked_days;

}
</code></pre>

<p>The 'dates' field, stores the booking dates in the database as 'longtext' in the following form:
5-29-2009 5-30-2009 5-31-2009 6-1-2009 </p>

<p>Besides using a different and more efficient database structure, what else could I do to optimize this code? </p>
