我有一个脚本从一个表中获取信息,确定是否需要为另一个表创建一个条目(避免重复),然后将信息插入到pages表中。问题是,以当前编码的方式查询大约10,000个条目会导致性能问题。
我可以通过哪种方式对其进行优化,以使其更高效,更快速地运行?从代码中可以看出,这有很多部分。
$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" && $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("<br>" . mysql_error());
}
else
{
if(mysql_affected_rows() != 0)
{
echo date('H:i:s') . ": ";
echo $request . " entry inserted<br>";
}
else
{
//echo "<i>" .$request . " entry ignored</i><br>";
}
}
}
}
非常感谢任何帮助。提前谢谢!
答案 0 :(得分:3)
如果您正在寻找重构的机会,这里有一些常见的代码香味可以提供一个可以改进的区域:
long if / elseif / else语句:
if () {
...
} elseif () {
...
} else {
...
}
有些人喜欢用switch
语句替换它们,但面向对象有更强大的工具来处理这个问题。有时,只需一个写得很好的方法就可以减少对它们的需求。
我在代码中注意到的另一件事是(看似)不同条件下的重复操作:
if(strstr($event['Event']," vs. "))
{
$game = true;
$teams = explode(" vs. ",$event['Event']);
}
elseif(strstr($event['Event']," Vs. "))
{
$game = true;
$teams = explode(" Vs. ",$event['Event']);
}
如果您进行了不区分大小写的比较,则可以保存五行几乎完全相同的代码
if(stristr ($event['Event']," vs. "))
{
$game = true;
$teams = explode(" vs. ",$event['Event']);
}
您可以通过在函数中反转if语句来减少嵌套并提高可读性:
if($querystring)
{
// lots of code here
// lots of code here
// lots of code here
// lots of code here
}
变为
if(!$querystring)
{
return;
}
// lots of code here
// lots of code here
// lots of code here
// lots of code here
当你将if语句嵌套为更多嵌套时,这一点尤其有用,读取起来就越困难。
就性能而言,访问数据库的次数越多,代码就越慢。如果你可以批处理查询,或完全避免它们 - 这是你的代码中使用毫秒的最大成本!
性能注意事项:您应该在第一个实例中优化代码的可读性。易于阅读,易于更改。然后测量它。如果需要太长时间,那么请查看性能调整。在每次更改后进行测量,看看它是否真的能提高性能。
答案 1 :(得分:1)
一个选项是改变这个:
$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";}
并使用开关switch
答案 2 :(得分:1)
EDIT1 :我更新了下面的代码以避免SQL注入(并且它使它更短更容易阅读)。
最大的性能提升是通过INSERT INTO ... (fields) VALUES (values1),(values2)...
一次插入多条记录。这是一个例子:
$eventquery = mysql_query("SELECT * FROM maintable WHERE PCatID != '3' AND PCatID != '4'");
$page_fields = array();
while($event = mysql_fetch_array($eventquery))
{
$querystring = null;
$performer_id = array();
$hasharray = array();
$venue = preg_replace("/ \(formerly (.+?)\)/i", '', $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" && $event['CCatID'] != "24")
{
$request = "buy-".strCleanupForPage($event['Event'])."-concert-tickets/".strCleanupForPage($event['City'])."-".
strCleanupForPage(stateabb($event['State']))."-".strCleanupForPage($venue)."-".$eventtime['date'];
$catnum = "202";
$page_fields[] = array($request, $catnum, $event['Event'], $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);
}
$page_fields[] = array($request, $catnum, $event['Event'], $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']));
$page_fields[] = array($request, $catnum, $event['Event'], $performer_id, $event['EventID'], $hasharray, 'n');
}
}
elseif($event['CCatID'] == "50")
{
$request = "boxing-tickets/".strCleanupForPage($event['Event'])."-".strCleanupForPage($event['City'])."-".$eventtime['date'];
$catnum = '1142';
$page_fields[] = array($request, $catnum, $event['Event'], $performer_id, $event['EventID'], $hasharray, 'n');
}
elseif($event['CCatID'] == "67")
{
$request = "golf-tickets/".strCleanupForPage($event['Event'])."-".strCleanupForPage($event['City'])."-".$eventtime['date'];
$catnum = '1112';
$page_fields[] = array($request, $catnum, $event['Event'], $performer_id, $event['EventID'], $hasharray, 'n');
}
elseif($event['CCatID'] == "27")
{
$request = "tennis-tickets/".strCleanupForPage($event['Event'])."-".strCleanupForPage($event['City'])."-".$eventtime['date'];
$catnum = '1102';
$page_fields[] = array($request, $catnum, $event['Event'], $performer_id, $event['EventID'], $hasharray, 'n');
}
elseif($event['CCatID'] == "101")
{
$request = "ufc-mma-tickets/".strCleanupForPage($event['Event'])."-".strCleanupForPage($event['City'])."-".$eventtime['date'];
$catnum = '1152';
$page_fields[] = array($request, $catnum, $event['Event'], $performer_id, $event['EventID'], $hasharray, 'n');
}
elseif($event['CCatID'] == "69")
{
if(strstr($event['Event'],"NASCAR"))
{
$request = "nascar-tickets/".strCleanupForPage($event['Event'])."-".strCleanupForPage($event['City'])."-".$eventtime['date'];
$catnum = '1122';
$page_fields[] = array($request, $catnum, $event['Event'], $performer_id, $event['EventID'], $hasharray, 'n');
}
elseif($event['GCatID'] == "35")
{
$request = "horse-racing-tickets/".strCleanupForPage($event['Event'])."-".strCleanupForPage($event['City'])."-".$eventtime['date'];
$catnum = '1132';
$page_fields[] = array($request, $catnum, $event['Event'], $performer_id, $event['EventID'], $hasharray, 'n');
}
}
elseif($event['GCatID'] == "26")
{
$request = "wwe-tickets/".strCleanupForPage($event['Event'])."-".strCleanupForPage($event['City'])."-".$eventtime['date'];
$catnum = '1162';
$page_fields[] = array($request, $catnum, $event['Event'], $performer_id, $event['EventID'], $hasharray, 'n');
}
elseif($event['CCatID'] == "24")
{
$catnum = "402";
$request = strCleanupForPage($event['Event'])."-comedy-tickets/".strCleanupForPage($event['City'])."-".strCleanupForPage(stateabb($event['State'])).
"-".strCleanupForPage($event['Venue'])."-".$eventtime['date'];
$page_fields[] = array($request, $catnum, $event['Event'], $performer_id, $event['EventID'], $hasharray, 'n');
}
if($page_fields)
{
$rows = array();
foreach ($page_fields as $fields) {
for ($i = 0; $i < count($fields), ++$i) {
$fields[$i] = sprintf("'%s'", mysql_escape_string($fields[$i]));
}
$rows[] = sprintf('(%s)', join(',', $fields));
}
$querystring = "INSERT IGNORE INTO pages (request, catnum, name, perfarray, event_id, hasharray, mainpage) VALUES\n"
. join(",\n", $rows);
$request = str_replace("--","-",$request);
if(!mysql_query($querystring))
{
exit("<br>" . mysql_error());
}
else
{
if(mysql_affected_rows() != 0)
{
echo date('H:i:s') . ": ";
echo $request . " entry inserted<br>";
}
else
{
//echo "<i>" .$request . " entry ignored</i><br>";
}
}
}
}
当然,如果您达到了系统设置的限制,您可以调整max_allowed_packet
和/或bulk_insert_buffer_size
设置。
答案 3 :(得分:0)
时间不长?如果你想降低代码的复杂性,在大型应用程序中,切换到面向对象的编程。 您仍在使用mysql_query等已弃用的函数。它的支持尚未被删除,但不鼓励使用它。切换到更面向对象的方法。 PDO是最好的
答案 4 :(得分:0)
你的数据库结构看起来非常奇怪,但是我错了,因为你的代码读起来很可怕。
一个问题是它没有正常化。对于maintable
,你必须为1个事件做几次选择。那么,您的主表应首先更改名称,然后仅保存事件和仅与事件直接相关的数据。
对于参与的表演者或团队,您希望查询另一个表(可能包含此脚本的联接),该表将事件ID与表演者数据一起保存。
您的类别ID也应该在数据库中,也可以在第一个查询中加入。任何使处理不同的类别属性也会存储在此表中。
你的问题不是你有10k行,而是数据库结构。
阅读数据库规范化,尤其是第三范式。然后试一试,随时回来询问结果。