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";
LVL 1
lamerhooDJVAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.