title: Help Optimizing MySQL Table (~ 500,000 records) and PHP Code
body: <p>I have a MySQL table that collects player data from various game servers (Urban Terror). The bot that collects the data runs 24/7, and currently the table is up to about 475,000+ records. Because of this, querying this table from PHP has become quite slow. I wonder what I can do on the database side of things to make it as optomized as possible, then I can focus on the application to query the database. The table is as follows:</p>
  
  <pre><code>CREATE TABLE IF NOT EXISTS `people` (
    `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
    `name` varchar(40) NOT NULL,
    `ip` int(4) unsigned NOT NULL,
    `guid` varchar(32) NOT NULL,
    `server` int(4) unsigned NOT NULL,
    `date` int(11) NOT NULL,
    PRIMARY KEY (`id`),
    UNIQUE KEY `Person` (`name`,`ip`,`guid`),
    KEY `server` (`server`),
    KEY `date` (`date`),
    KEY `PlayerName` (`name`)
  ) ENGINE=MyISAM  DEFAULT CHARSET=latin1 COMMENT='People that Play on Servers' AUTO_INCREMENT=475843 ;
  </code></pre>
  
  <p>I'm storying the IPv4 (ip and server) as 4 byte integers, and using the MySQL functions NTOA(), etc to encode and decode, I heard that this way is faster, rather than varchar(15).</p>
  
  <p>The guid is a md5sum, 32 char hex. Date is stored as unix timestamp.</p>
  
  <p>I have a unique key on name, ip and guid, as to avoid duplicates of the same player.</p>
  
  <p>Do I have my keys setup right? Is the way I'm storing data efficient?</p>
  
  <p>Here is the code to query this table. You search for a name, ip, or guid, and it grabs the results of the query and cross references other records that match the name, ip, or guid from the results of the first query, and does it for each field. This is kind of hard to explain. But basically, if I search for one player by name, I'll see every other name he has used, every IP he has used and every GUID he has used.</p>
  
  <pre><code>&lt;form action="&lt;?php echo $_SERVER['PHP_SELF']; ?&gt;" method="post"&gt;
  Search: &lt;input type="text" name="query" id="query" /&gt;&lt;input type="submit" name="btnSubmit" value="Submit" /&gt;
  &lt;/form&gt;
  
  &lt;?php if (!empty($_POST['query'])) { ?&gt;
  
  &lt;table cellspacing="1" id="1up_people" class="tablesorter" width="300"&gt;
  &lt;thead&gt;
  &lt;tr&gt;
      &lt;th&gt;ID&lt;/th&gt;
      &lt;th&gt;Player Name&lt;/th&gt;
      &lt;th&gt;Player IP&lt;/th&gt;
      &lt;th&gt;Player GUID&lt;/th&gt;
      &lt;th&gt;Server&lt;/th&gt;
      &lt;th&gt;Date&lt;/th&gt;
  &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
  &lt;?php
  
  function super_unique($array)
  {
    $result = array_map("unserialize", array_unique(array_map("serialize", $array)));
  
    foreach ($result as $key =&gt; $value)
    {
      if ( is_array($value) )
      {
        $result[$key] = super_unique($value);
      }
    }
  
    return $result;
  }
  
      if (!empty($_POST['query'])) {
          $query = trim($_POST['query']);
          $count = 0;
          $people = array();
          $link = mysql_connect('localhost', 'mysqluser', 'yea right!');
                  if (!$link) {
                          die('Could not connect: ' . mysql_error());
                  }
                  mysql_select_db("1up");
                  $sql = "SELECT id, name, INET_NTOA(ip) AS ip, guid, INET_NTOA(server) AS server, date FROM 1up_people WHERE (name LIKE \"%$query%\" OR INET_NTOA(ip) LIKE \"%$query%\" OR guid LIKE \"%$query%\")";
          $result = mysql_query($sql, $link);
          if (!$result) {
              die(mysql_error());
          }
          // Now take the initial results and parse each column into its own array
          while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
              $name = htmlspecialchars($row[1]);
              $people[] = array(
                  'id' =&gt; $row[0],
                  'name' =&gt; $name,
                  'ip' =&gt; $row[2],
                  'guid' =&gt; $row[3],
                  'server' =&gt; $row[4],
                  'date' =&gt; $row[5]
              );
          }
          // now for each name, ip, guid in results, find additonal records
          $people2 = array();
          foreach ($people AS $person) {
              $ip = $person['ip'];
              $sql = "SELECT id, name, INET_NTOA(ip) AS ip, guid, INET_NTOA(server) AS server, date FROM 1up_people WHERE (ip = \"$ip\")";
              $result = mysql_query($sql, $link);
              while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
                  $name = htmlspecialchars($row[1]);
                  $people2[] = array(
                      'id' =&gt; $row[0],
                      'name' =&gt; $name,
                      'ip' =&gt; $row[2],
                      'guid' =&gt; $row[3],
                      'server' =&gt; $row[4],
                      'date' =&gt; $row[5]
                  );
              }
          }
  
                  $people3 = array();
                  foreach ($people AS $person) {
                          $guid = $person['guid'];
                          $sql = "SELECT id, name, INET_NTOA(ip) AS ip, guid, INET_NTOA(server) AS server, date FROM 1up_people WHERE (guid = \"$guid\")";
                          $result = mysql_query($sql, $link);
                          while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
                                  $name = htmlspecialchars($row[1]);
                                  $people3[] = array(
                                          'id' =&gt; $row[0],
                                          'name' =&gt; $name,
                                          'ip' =&gt; $row[2],
                      'guid' =&gt; $row[3],
                      'server' =&gt; $row[4],
                      'date' =&gt; $row[5]
                                  );
                          }
                  }
  
  
                  $people4 = array();
                  foreach ($people AS $person) {
                          $name = $person['name'];
                          $sql = "SELECT id, name, INET_NTOA(ip) AS ip, guid, INET_NTOA(server) AS server, date FROM 1up_people WHERE (name = \"$name\")";
                          $result = mysql_query($sql, $link);
                          while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
                                  $name = htmlspecialchars($row[1]);
                                  $people4[] = array(
                                          'id' =&gt; $row[0],
                                          'name' =&gt; $name,
                                          'ip' =&gt; $row[2],
                      'guid' =&gt; $row[3],
                      'server' =&gt; $row[4],
                      'date' =&gt; $row[5]
                                  );
                          }
                  }
  
  
          // Combine people and people2 into just people
          $people = array_merge($people, $people2);
          $people = array_merge($people, $people3);
          $people = array_merge($people, $people4);
  
          $people = super_unique($people);        
  
          foreach ($people AS $person) {
              $date = ($person['date']) ? date("M d, Y", $person['date']) : 'Before 8/1/10';
              echo "&lt;tr&gt;\n";
              echo "&lt;td&gt;".$person['id']."&lt;/td&gt;";
              echo "&lt;td&gt;".$person['name']."&lt;/td&gt;";
              echo "&lt;td&gt;".$person['ip']."&lt;/td&gt;";
              echo "&lt;td&gt;".$person['guid']."&lt;/td&gt;";
              echo "&lt;td&gt;".$person['server']."&lt;/td&gt;";
              echo "&lt;td&gt;".$date."&lt;/td&gt;";
              echo "&lt;/tr&gt;\n";
              $count++;
          }
          // Find Total Records
          //$result = mysql_query("SELECT id FROM 1up_people", $link);
          //$total  = mysql_num_rows($result);
          mysql_close($link);
      }
  ?&gt;
  &lt;/tbody&gt;
  &lt;/table&gt;
  &lt;p&gt;
  &lt;?php 
      echo $count." Records Found for \"".$_POST['query']."\" out of $total";
  ?&gt;
  &lt;/p&gt;
  
  &lt;?php 
  } 
  $time_stop = microtime(true);
  print("Done (ran for ".round($time_stop-$time_start)." seconds).");
  
  ?&gt;
  </code></pre>
  
  <p>Any help at all is appreciated!</p>
  
  <p>Thank you.</p>