Large script with many parts... how can I optimize this?
<p>I have a script that is taking in information from one table, determining if it needs to make an entry for another table (avoiding duplicates) and then inserting the information into the pages table.  The problem is, doing queries for approx 10,000 entries the way it's currently coded, is making causing performance issues.  </p>

<p>What way could I optimize this in order to make it run more efficiently and quicker? As you can see from the code, there are many parts to this.</p>

<pre><code>$eventquery = mysql_query("SELECT * FROM maintable WHERE PCatID != '3' AND PCatID != '4'");
while($event = mysql_fetch_array($eventquery))
{
$querystring = null;
$performer_id = array();
$hasharray = array();
$venue = preg_replace("/ \([fF]ormerly (.+?)\)/", '', $event['Venue']);

$perfquery = mysql_query("SELECT * FROM maintable WHERE EventID = '".$event['EventID']."'");   
while($perf = mysql_fetch_array($perfquery))
    {
        $performer_id[$perf['PerformerID']] = $perf['Performer'];
        $hasharray[] = $perf['id'];
    }

$eventtime = getEventTime($event['DateTime']);
$performer_id = serialize($performer_id);
$hasharray = serialize($hasharray);

if($event['PCatID'] == "2" &amp;&amp; $event['CCatID'] != "24")
{
    $request = "buy-".strCleanupForPage($event['Event'])."-concert-tickets/".strCleanupForPage($event['City'])."-".
        strCleanupForPage(stateabb($event['State']))."-".strCleanupForPage($venue)."-".$eventtime['date'];

    $catnum = "202";

    $querystring = "INSERT IGNORE INTO pages (request, catnum, name, perfarray, event_id, hasharray, mainpage) VALUES 
        ('".$request."','".$catnum."','".addslashes($event['Event'])."','".addslashes($performer_id)."',
            '".$event['EventID']."','".$hasharray."','n')";


}
elseif($event['GCatID'] == "32" || $event['GCatID'] == "16" || $event['GCatID'] == "30" || $event['GCatID'] == "19" ||
         $event['GCatID'] == "31" || $event['GCatID'] == "22")
{ 
    if($event['GCatID'] == "32")
        {$catnum = "102";}
    elseif($event['GCatID'] == "16")
        {$catnum = "101";}
    elseif($event['GCatID'] == "30")
        {$catnum = "103";}
    elseif($event['GCatID'] == "19")
        {$catnum = "104";}
    elseif($event['GCatID'] == "31")
        {$catnum = "109";}
    elseif($event['GCatID'] == "22")
        {$catnum = "108";}
    elseif($event['GCatID'] == "17")
        {
            if($event['CCatID'] == "66")
                {$catnum = "106";}
            elseif($event['CCatID'] == "65")
                {$catnum = "105";}
        }

    if(strstr($event['Event']," vs. "))
        {
            $game = true;
            $teams = explode(" vs. ",$event['Event']);
        }
    elseif(strstr($event['Event']," Vs. "))
        {
            $game = true;
            $teams = explode(" Vs. ",$event['Event']);
        }
    else
        {
            $game = false;            
        }

    if($game)
        {           

            $catnum .= "2";
            $homequery = mysql_query("SELECT * FROM sportsteams WHERE fullname LIKE '".$teams[0]."'");  
            $home = mysql_fetch_array($homequery);  
            $awayquery = mysql_query("SELECT * FROM sportsteams WHERE fullname LIKE '".$teams[1]."'");  
            $away = mysql_fetch_array($awayquery);

            if((mysql_num_rows($homequery)) == 0 || (mysql_num_rows($awayquery)) == 0)
            {
                if($catnum == "1012")
                    {$request = "mlb-baseball-tickets";}
                elseif($catnum == "1022")
                    {$request = "nfl-football-tickets";}
                elseif($catnum == "1032")
                    {$request = "nba-basketball-tickets";}
                elseif($catnum == "1042")
                    {$request = "nhl-hockey-tickets";}
                elseif($catnum == "1052")
                    {$request = "ncaa-football-tickets";}
                elseif($catnum == "1062")
                    {$request = "ncaa-basketball-tickets";}
                elseif($catnum == "1082")
                    {$request = "mls-soccer-tickets";}
                elseif($catnum == "1092")
                    {$request = "wnba-basketball-tickets";}
                $eventname = preg_replace("/\([iI]ncludes (.+?)\)/", '', $event['Event']);
                $request .= "/".strCleanupForPage($eventname)."-".strCleanupForPage($event['City'])."-".
                     strCleanupForPage(stateabb($event['State']));
                }
            else
                {
                    $request = strCleanupForPage($home['fullname'])."-tickets-".strCleanupForPage($venue)."/".strCleanupForPage($home['nickname'])."-vs-".strCleanupForPage($away['nickname'])."-".strCleanupForPage($event['City'])."-".
                    strCleanupForPage(stateabb($event['State']))."-".$eventtime['date'];
                    $request = str_replace("-/","/",$request);
                }

        $querystring = "INSERT IGNORE INTO pages (request, catnum, name, perfarray, event_id, hasharray, mainpage) VALUES 
        ('".$request."','".$catnum."','".addslashes($event['Event'])."','".addslashes($performer_id)."',
            '".$event['EventID']."','".$hasharray."','n')";
    }
    else
    {   
        $catnum .= "3";
        if($catnum == "1013")
            {$request = "mlb-baseball";}
        elseif($catnum == "1023")
            {$request = "nfl-football";}
        elseif($catnum == "1033")
            {$request = "nba-basketball";}
        elseif($catnum == "1043")
            {$request = "nhl-hockey";}
        elseif($catnum == "1053")
            {$request = "ncaa-football";}
        elseif($catnum == "1063")
            {$request = "ncaa-basketball";}
        elseif($catnum == "1083")
            {$request = "mls-soccer";}
        elseif($catnum == "1093")
            {$request = "wnba-basketball";}

        $eventname = preg_replace("/\([iI]ncludes (.+?)\)/", '', $event['Event']);
        $request .= "/".strCleanupForPage($eventname)."-".strCleanupForPage($event['City'])."-".
        strCleanupForPage(stateabb($event['State']));

        $querystring = "INSERT IGNORE INTO pages (request, catnum, name, perfarray, event_id, hasharray, mainpage) VALUES 
        ('".$request."','".$catnum."','".addslashes($event['Event'])."','".addslashes($performer_id)."',
            '".$event['EventID']."','".$hasharray."','n')";
    }
}
elseif($event['CCatID'] == "50")
{
    $request = "boxing-tickets/".strCleanupForPage($event['Event'])."-".strCleanupForPage($event['City'])."-".$eventtime['date'];
    $querystring = "INSERT IGNORE INTO pages (request, catnum, name, perfarray, event_id, hasharray) VALUES
        ('".$request."','1142','".addslashes($event['Event'])."','".addslashes($performer_id)."','".$event['EventID']."',
            '".$hasharray."')";
}    
elseif($event['CCatID'] == "67")
{
    $request = "golf-tickets/".strCleanupForPage($event['Event'])."-".strCleanupForPage($event['City'])."-".$eventtime['date'];
    $querystring = "INSERT IGNORE INTO pages (request, catnum, name, perfarray, event_id, hasharray) VALUES
        ('".$request."','1112','".addslashes($event['Event'])."','".addslashes($performer_id)."','".$event['EventID']."',
            '".$hasharray."')";
}    
elseif($event['CCatID'] == "27")
{
    $request = "tennis-tickets/".strCleanupForPage($event['Event'])."-".strCleanupForPage($event['City'])."-".$eventtime['date'];
    $querystring = "INSERT IGNORE INTO pages (request, catnum, name, perfarray, event_id, hasharray) VALUES
        ('".$request."','1102','".addslashes($event['Event'])."','".addslashes($performer_id)."','".$event['EventID']."',
            '".$hasharray."')";
}    
elseif($event['CCatID'] == "101")
{
    $request = "ufc-mma-tickets/".strCleanupForPage($event['Event'])."-".strCleanupForPage($event['City'])."-".$eventtime['date'];
    $querystring = "INSERT IGNORE INTO pages (request, catnum, name, perfarray, event_id, hasharray) VALUES
        ('".$request."','1152','".addslashes($event['Event'])."','".addslashes($performer_id)."','".$event['EventID']."',
            '".$hasharray."')";
}    
elseif($event['CCatID'] == "69")
{
    if(strstr($event['Event'],"NASCAR"))
    {                    
        $request = "nascar-tickets/".strCleanupForPage($event['Event'])."-".strCleanupForPage($event['City'])."-".$eventtime['date'];
        $querystring = "INSERT IGNORE INTO pages (request, catnum, name, perfarray, event_id, hasharray) VALUES
            ('".$request."','1122','".addslashes($event['Event'])."','".addslashes($performer_id)."','".$event['EventID']."',
            '".$hasharray."')";
    }
    elseif($event['GCatID'] == "35")
    {
        $request = "horse-racing-tickets/".strCleanupForPage($event['Event'])."-".strCleanupForPage($event['City'])."-".$eventtime['date'];
        $querystring = "INSERT IGNORE INTO pages (request, catnum, name, perfarray, event_id, hasharray) VALUES
            ('".$request."','1132','".addslashes($event['Event'])."','".addslashes($performer_id)."','".$event['EventID']."',
            '".$hasharray."')";
    }
}
elseif($event['GCatID'] == "26")
{
    $request = "wwe-tickets/".strCleanupForPage($event['Event'])."-".strCleanupForPage($event['City'])."-".$eventtime['date'];
    $querystring = "INSERT IGNORE INTO pages (request, catnum, name, perfarray, event_id, hasharray) VALUES
        ('".$request."','1162','".addslashes($event['Event'])."','".addslashes($performer_id)."','".$event['EventID']."',
            '".$hasharray."')";
}
elseif($event['CCatID'] == "24")
{   
    $catnum = "402";
    $request = strCleanupForPage($event['Event'])."-comedy-tickets/".strCleanupForPage($event['City'])."-".strCleanupForPage(stateabb($event['State'])).
        "-".strCleanupForPage($event['Venue'])."-".$eventtime['date'];
    $querystring = "INSERT IGNORE INTO pages (request, catnum, name, perfarray, event_id, hasharray, mainpage) VALUES 
        ('".$request."','".$catnum."','".addslashes($event['Event'])."','".addslashes($performer_id)."',
            '".$event['EventID']."','".$hasharray."','n')";
}

if($querystring)
{
    $request = str_replace("--","-",$request);
    if(!mysql_query($querystring))
        {
            exit("&lt;br&gt;" . mysql_error());
        }
    else
    {
        if(mysql_affected_rows() != 0)
            {               
                echo date('H:i:s') . ": ";
                echo $request . " entry inserted&lt;br&gt;";
            }
        else
            {
                //echo "&lt;i&gt;" .$request . " entry ignored&lt;/i&gt;&lt;br&gt;";
            }
    }
}

}
</code></pre>

<p>Any help is greatly appreciated.  Thank you in advance!</p>
