I am checking over 160,000 records in my database every 24 hours to see if there were changes in the incoming data coming from API and my existing data in the database. If there were I am updating those, otherwise I'm doing nothing. Checking 189 days takes 10 seconds and 160,000 about 2.5 hours.
What can I improve in my code to speed this up?
$listings = Array('23169','23567','23114','5487'); //Rooms to check (189 days in each room)
$count_listings = count(listings); //count the rooms
$continue = TRUE;
$x = 0;
while ($continue == TRUE && $x < $count_listings) {
foreach ($listings as $list) {
//Generates a link from the looped @list
$url = $this->generate_link($list);
$results_page = $this->http_get_contents($url, $this->generate_proxy()); //JSON output
$calendar_month = json_decode($results_page, true); //Into Array
$data = Array();
foreach ($calendar_month['calendar_months'] as $month) {
foreach ($month['days'] as $day) {
$data[] = $day; //Getting each day
}
}
//Super_unique removes duplicate days
foreach ($this->super_unique($data) as $day_info) {
$data_listing = Array(
'list_id' => $list,
'list_price' => $day_info['price']['local_price'],
'list_currency' => $day_info['price']['local_currency'],
'list_date' => $day_info['date'],
'list_available' => $this->check_list_available((int)$day_info['available'])
);
//First get incoming result and search in the database
$check = $this->db->select('list_price, list_available')
->from('airbnb_lists_price')
->where("(list_id = '{$data_listing['list_id']}' and list_date = '{$data_listing['list_date']}')")
->get();
//If the record exists
if ($check->num_rows() > 0) {
foreach ($check->result() as $row) {
//If the record info was changed update
if ($row->list_price != $data_listing['list_price'] || $row->list_available != $data_listing['list_available']) {
$this->db->where("(list_id = '{$data_listing['list_id']}' and list_date = '{$data_listing['list_date']}')");
$this->db->update('airbnb_lists_price', $data_listing);
}
else {
//do nothing
}
}
}
}
$x++;
}
if ($x > $count_listings) {
$continue = FALSE;
}
}
$this->http_get_contents
is likely to be not very fast. \$\endgroup\$