title: Import and process text file within MySQL
body: <p>I'm working on a research project that requires me to process large csv files (~2-5 GB) with 500,000+ records.  These files contain information on government contracts (from <a href="http://www.usaspending.gov" rel="nofollow">USASpending.gov</a>).  So far, I've been using PHP or Python scripts to attack the files row-by-row, parse them, and then insert the information into the relevant tables.  The parsing is moderately complex.  For each record, the script checks to see if the entity named is already in the database (using a combination of string and regex matching); if it is not, it first adds the entity to a table of entities and then proceeds to parse the rest of the record and inserts the information into the appropriate tables.  The list of entities is over 100,000.</p>
  
  <p>Here are the basic functions (part of a class) that try to match each record with any existing entities:</p>
  
  <pre><code>private function _getOrg($data)
  {
      // if name of organization is null, skip it
      if($data[44] == '') return null;
  
      // use each of the possible names to check if organization exists
      $names = array($data[44],$data[45],$data[46],$data[47]);
  
      // cycle through the names
      foreach($names as $name) {
          // check to see if there is actually an entry here
          if($name != '') {
              if(($org_id = $this-&gt;_parseOrg($name)) != null) {
                  $this-&gt;update_org_meta($org_id,$data); // updates some information of existing entity based on record
                  return $org_id;
              }
          }
      }
  
      return $this-&gt;_addOrg($data);
  }
  
  private function _parseOrg($name)
  {
      // check to see if it matches any org names
      // db class function, performs simple "like" match
      $this-&gt;db-&gt;where('org_name',$name,'like');
  
      $result = $this-&gt;db-&gt;get('orgs');
  
      if(mysql_num_rows($result) == 1) {
          $row = mysql_fetch_object($result);
          return $row-&gt;org_id;
      }
  
      // check to see if matches any org aliases
      $this-&gt;db-&gt;where('org_alias_name',$name,'like');
  
      $result = $this-&gt;db-&gt;get('orgs_aliases');
  
      if(mysql_num_rows($result) == 1) {
          $row = mysql_fetch_object($result);
          return $row-&gt;org_id;
      }
      return null; // no matches, have to add new entity
   }
  </code></pre>
  
  <p>The _addOrg function inserts the new entity's information into the db, where hopefully it will match subsequent records.</p>
  
  <p>Here's the problem: I can only get these scripts to parse about 10,000 records / hour, which, given the size, means a few solid days for each file.  The way my db is structured requires a several different tables to be updated for each record because I'm compiling multiple external datasets.  So, each record updates two tables, and each new entity updates three tables.  I'm worried that this adds too much lag time between MySQL server and my script.</p>
  
  <p>Here's my question: is there a way to import the text file into a temporary MySQL table and then use internal MySQL functions (or PHP/Python wrapper) to <strong>speed up</strong> the processing?</p>
  
  <p>I'm running this on my Mac OS 10.6 with local MySQL server.</p>