[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 253
  • Last Modified:

Help improving my script (making it faster and efficient)

The file in question is about 300mb in length.  The reason it must be done this way is because the database has keys that need to associate with the imported cities.  (EG country codes, state codes).  I'm looking for someone who has a solution to take my existing code and speeding it up :)

--
Please help me improve upon this (make it faster)

mysql_pconnect("localhost", "root", "");
mysql_select_db("globaldate");


$handle = @fopen("Feature.txt", "r");
$i = 0;
echo "<PRE>";
if ($handle) {
   while (!feof($handle)) {
            $buffer = fgets($handle, 4096);
            $phase2[] = explode("\t",$buffer);
            $lookup = @mysql_fetch_array(mysql_query("select * from dt_cities where name = '".$phase2[$i][9]."'"));
            $country = mysql_fetch_array($q = mysql_query("select * from fp_countries where countrycode = '".$phase2[$i][5]."'"));
            $state = mysql_fetch_array(mysql_query("select * from fp_states where statecode = '".$phase2[$i][3]."'"));
            if($lookup['name'] != $phase2[$i][9])
            {
                  $q = mysql_query("insert into dt_cities VALUES('', '".$country['id']."', '".$state['id']."', '".$phase2[$i][9]."')");
                  echo ".";
            }
            ++$i;
   }
   fclose($handle);
}

echo "Done with import of Cities";
0
lamerhooDJV
Asked:
lamerhooDJV
  • 4
1 Solution
 
ixtiCommented:
<?php

mysql_pconnect("localhost", "root", "");
mysql_select_db("globaldate");

$handle = @fopen("Feature.txt", "r");

echo "<PRE>";
if ($handle) {
   while (!feof($handle)) {
          $data = fgets($handle, 4096);
          $data = explode("\t", $data);
          foreach ($data as $k => $v) $data[$k] = mysql_real_escape_string($v);
         
          $country  = @mysql_fetch_row(mysql_query("SELECT `id` FROM `fp_countries` WHERE `countrycode` = '{$data[5]}'"));
          $state    = @mysql_fetch_row(mysql_query("SELECT `id` FROM `fp_states` WHERE `statecode` = '{$data[3]}'"));
         
          $country  = mysql_real_escape_string($country[0]);
          $state    = mysql_real_escape_string($state[0]);
         
          mysql_query("INSERT IGNORE INTO `dt_cities` VALUES('', '{$country}', '{$state}', '{$data[9]}')");
         
          unset($data);
   }
   fclose($handle);
}

echo "Done with import of Cities";

?>
0
 
ixtiCommented:
By the way, it can be done by only one mysql_query...
0
 
ixtiCommented:
And more...
I suggest you to read manual about mysql's "LOAD DATA INFILE" (http://dev.mysql.com/doc/refman/4.1/en/load-data.html) which says:
"The LOAD DATA INFILE statement reads rows from a text file into a table at a very high speed."
0
 
ixtiCommented:
So you can load and parse data from your file to temporary table, then do some operations you nedd and then dcelete that table...
0
 
gschwaCommented:
Pull out the look up for state and country.  Since most likely that's a small amount of data, just load them into a couple of associative arrays and then just look in the arrays rather than hitting the database.  FYI, the function array_map_by_first is defined at the bottom of the script. Also credits for insert ignore go to ixti.

mysql_pconnect("localhost", "root", "");
mysql_select_db("globaldate");

$countries = array_map_by_first(
                 mysql_fetch_array(
                     mysql_query("select countrycode, id from fp_countries")));

$states = array_map_by_first(
              mysql_fetch_array(
                  mysql_query("select statecode, id from fp_states")));

$handle = @fopen("Feature.txt", "r");
$i = 0;
echo "<PRE>";
if ($handle) {
   while (!feof($handle)) {
          $buffer = fgets($handle, 4096);
          $phase2[] = explode("\t",$buffer);
          $city = $phase2[$i][9];
          $country_id = $phase2[$i][5];
          $state_id = $phase2[$i][3];
           
          if (!@$countries[$country_id] || !@$states[$state_id])
          {
               // warn?
          }
          else
          {
               mysql_query("insert ignore into dt_cities VALUES('', '".$countries[$country_id]."', '".$states[$state_id]."', '".$city."')");
               echo ".";
          }
          ++$i;
   }
   fclose($handle);
}

echo "Done with import of Cities";

function array_map_by_first($a)
{
    $n = array();
    foreach ($a as $i)
        $n[array_shift($i)] = count($i) == 1 && isset($i[0]) ? $i[0] : $i;

    return $n;
}
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now