影響PHP+MYSQL執(zhí)行速度的寫法對比
更新時間:2007年01月02日 00:00:00 作者:
-1)*$page_size;
$query = "select tid,truename,school,study,city,logo,year,month,x1,x2 from ask_member where $t order by logo desc, logintime desc limit $a,$page_size ";
$result = mysql_db_query($DataBase, $query);
while($r2[] = mysql_fetch_array($result))
{
}
foreach($r2 as $rr2)
{
$query = "select name,tid from ask_school where tid='$rr2[x2]' ";
$result2 = mysql_db_query($DataBase, $query);
$r5[] = mysql_fetch_array($result2);
}
$n=count($r2)-1;
?>
<?php
for($i=0;$i<$n;$i++)
{
?>
<table width="100%" border="0" cellpadding="5" cellspacing="1" bgcolor="#57ACF2">
<tr>
<td align="center" bgcolor="#FFFFFF">
<table width="690" border="0" cellspacing="0" cellpadding="0">
<tr valign="middle">
<td width="80"><img
src="../ask/center/memberlogo/<?php if($r2[$i][logo]!=''){echo $r2[$i][logo];}else if ($r2[$i][sex]=='女'){echo "default.gif";}else{echo "default3.gif";} ?>" border=0 width="100" height="127" onload="javascript:DrawImage(this,100,127)"></td>
<td width="430"><strong><?php echo $r2[$i][truename]; ?> - <font color="#85B000">信息 :)</font></strong><br>
字段一:<a href="#" class="blue"><?php echo $r5[$i][name]==''?'內(nèi)容':$r5[$i][name]; ?></a> <br>
字段二:<a href="#" class="blue"><?php echo $r2[$i][study]; ?></a><br>
字段三:<a href="#" class="blue"><?php echo $r2[$i][year]; ?><?php echo $r2[$i][month]; ?></a><br>
瀏覽:<a href="#" class="blue">一</a> <font color="#0E6FBE">|</font>
<a href="#" class="blue">二</a> <font color="#0E6FBE">|</font>
<a href="#" class="blue">三</a> </td>
<td width="180" align="right"><table width="110" border="0" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td height="20" align="center" bgcolor="#eeeeee"><a href="hello.php?tid=<?php echo $r2[$i][tid]; ?>">打個招呼</a></td>
</tr>
</table>
<table width="110" border="0" cellspacing="0" cellpadding="0">
<tr>
<td height="3"></td>
</tr>
</table>
<table width="110" border="0" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td height="20" align="center" bgcolor="#eeeeee"><a href="sendmessagelook.php?tid=<?php echo $r2[$i][tid]; ?>">其它操作</a></td>
</tr>
</table>
<table width="110" border="0" cellspacing="0" cellpadding="0">
<tr>
<td height="3"></td>
</tr>
</table>
<table width="110" border="0" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td height="20" align="center" bgcolor="#eeeeee"><a href="addfriend.php?tid=<?php echo $r2[$i][tid]; ?>">操作二</a></td>
</tr>
</table>
<table width="110" border="0" cellspacing="0" cellpadding="0">
<tr>
<td height="3"></td>
</tr>
</table>
<table width="110" border="0" cellspacing="0" cellpadding="0">
<tr>
<td height="3"></td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td height="10"></td>
</tr>
</table>
<?php
}
?> [/code]
問題一:為什么要用
而不用:
答:因為PHP網(wǎng)站在運(yùn)行過程中,真正影響速度的原因只有一個,那就是對MYSQL的讀取,至于程序多運(yùn)行一兩次復(fù)制和拷貝,很難對速度造成影響.所以在讀取MYSQL的時候,我寧愿多一次內(nèi)存中的運(yùn)算,也不愿有什么操作影響到對MYSQL的讀取.
問題二:為什么不用模版寫程序?
答:因為PHP已經(jīng)是最好的模版引擎了,為什么還要用其它的模版呢?
問題三:為什么把大量的PHP代碼放在<HTML>的上方,而不是放面HTML的下方?
答:這個是有很大區(qū)別的,其一:代碼放在<HTML>上方,如果對代碼進(jìn)行修改的話,用DREARWEAVER再看下面視圖的時候,不會有慢的感覺.
其二:大部分代碼全部放在<HTML>上方,而不是放在<HTML>下方,對PHP腳本的解析運(yùn)行有好處,可以減輕PHP服務(wù)器解析腳本的負(fù)擔(dān),并且符合模版及緩存設(shè)計思想.
問題三:為什么要用
view plaincopy to clipboardprint?
<?php
$query = "select count(*) from ask_member where $t ";
$result2 = mysql_db_query($DataBase, $query);
$r3 = mysql_fetch_array($result2);
$amount=$r3[0];
?>
<?php
$query = "select count(*) from ask_member where $t ";
$result2 = mysql_db_query($DataBase, $query);
$r3 = mysql_fetch_array($result2);
$amount=$r3[0];
?>
而不是用:
view plaincopy to clipboardprint?
<?php
$query = "select * from ask_member ";
$result2 = mysql_db_query($DataBase, $query);
$amount=mysql_num_rows($result2);
?>
<?php
$query = "select * from ask_member ";
$result2 = mysql_db_query($DataBase, $query);
$amount=mysql_num_rows($result2);
?>
答:第一種是對一條記錄的查詢,第二種是對所有記錄的查詢,兩者速度有天壤之別.
問題四:為什么上面要多一行
view plaincopy to clipboardprint?
<?php
$n=count($r2)-1;
?>
<?php
$n=count($r2)-1;
?>
而不是下面直接用:
view plaincopy to clipboardprint?
<?php
for($i=0;$i<count($r2)-1;$i++)
{
?>
<?php
for($i=0;$i<count($r2)-1;$i++)
{
?>
答:應(yīng)該盡量減少在<HTML>以下進(jìn)行PHP的運(yùn)算,如果過多地在<HTML>下進(jìn)行運(yùn)算的話,那就成了混編了.
問題五:為什么說以上代碼是核心代碼?
答:任何一位程序員都知道,讀數(shù)據(jù)的分頁讀出是最常用的,我想大部分的PHP程序員30%的時間都是發(fā)在這個上面吧.
以上是我總結(jié)出的最快的寫PHP的經(jīng)驗,請廣大網(wǎng)友指正,如果覺得可以的話,希望所有人都能用這種方式來寫程序,那樣的話我以后安排一批PHP程序員進(jìn)行分工協(xié)調(diào)的時候也好辦多了.
$query = "select tid,truename,school,study,city,logo,year,month,x1,x2 from ask_member where $t order by logo desc, logintime desc limit $a,$page_size ";
$result = mysql_db_query($DataBase, $query);
while($r2[] = mysql_fetch_array($result))
{
}
foreach($r2 as $rr2)
{
$query = "select name,tid from ask_school where tid='$rr2[x2]' ";
$result2 = mysql_db_query($DataBase, $query);
$r5[] = mysql_fetch_array($result2);
}
$n=count($r2)-1;
?>
<?php
for($i=0;$i<$n;$i++)
{
?>
<table width="100%" border="0" cellpadding="5" cellspacing="1" bgcolor="#57ACF2">
<tr>
<td align="center" bgcolor="#FFFFFF">
<table width="690" border="0" cellspacing="0" cellpadding="0">
<tr valign="middle">
<td width="80"><img
src="../ask/center/memberlogo/<?php if($r2[$i][logo]!=''){echo $r2[$i][logo];}else if ($r2[$i][sex]=='女'){echo "default.gif";}else{echo "default3.gif";} ?>" border=0 width="100" height="127" onload="javascript:DrawImage(this,100,127)"></td>
<td width="430"><strong><?php echo $r2[$i][truename]; ?> - <font color="#85B000">信息 :)</font></strong><br>
字段一:<a href="#" class="blue"><?php echo $r5[$i][name]==''?'內(nèi)容':$r5[$i][name]; ?></a> <br>
字段二:<a href="#" class="blue"><?php echo $r2[$i][study]; ?></a><br>
字段三:<a href="#" class="blue"><?php echo $r2[$i][year]; ?><?php echo $r2[$i][month]; ?></a><br>
瀏覽:<a href="#" class="blue">一</a> <font color="#0E6FBE">|</font>
<a href="#" class="blue">二</a> <font color="#0E6FBE">|</font>
<a href="#" class="blue">三</a> </td>
<td width="180" align="right"><table width="110" border="0" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td height="20" align="center" bgcolor="#eeeeee"><a href="hello.php?tid=<?php echo $r2[$i][tid]; ?>">打個招呼</a></td>
</tr>
</table>
<table width="110" border="0" cellspacing="0" cellpadding="0">
<tr>
<td height="3"></td>
</tr>
</table>
<table width="110" border="0" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td height="20" align="center" bgcolor="#eeeeee"><a href="sendmessagelook.php?tid=<?php echo $r2[$i][tid]; ?>">其它操作</a></td>
</tr>
</table>
<table width="110" border="0" cellspacing="0" cellpadding="0">
<tr>
<td height="3"></td>
</tr>
</table>
<table width="110" border="0" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td height="20" align="center" bgcolor="#eeeeee"><a href="addfriend.php?tid=<?php echo $r2[$i][tid]; ?>">操作二</a></td>
</tr>
</table>
<table width="110" border="0" cellspacing="0" cellpadding="0">
<tr>
<td height="3"></td>
</tr>
</table>
<table width="110" border="0" cellspacing="0" cellpadding="0">
<tr>
<td height="3"></td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td height="10"></td>
</tr>
</table>
<?php
}
?> [/code]
問題一:為什么要用
復(fù)制代碼 代碼如下:
<?php
while($r2[] = mysql_fetch_array($result))
{
}
foreach($r2 as $rr2)
{
$query = "select name,tid from ask_school where tid='$rr2[x2]' ";
$result2 = mysql_db_query($DataBase, $query);
$r5[] = mysql_fetch_array($result2);
}
?>
while($r2[] = mysql_fetch_array($result))
{
}
foreach($r2 as $rr2)
{
$query = "select name,tid from ask_school where tid='$rr2[x2]' ";
$result2 = mysql_db_query($DataBase, $query);
$r5[] = mysql_fetch_array($result2);
}
?>
復(fù)制代碼 代碼如下:
<?php
while($r2[] = mysql_fetch_array($result))
{
}
foreach($r2 as $rr2)
{
$query = "select name,tid from ask_school where tid='$rr2[x2]' ";
$result2 = mysql_db_query($DataBase, $query);
$r5[] = mysql_fetch_array($result2);
}
?>
while($r2[] = mysql_fetch_array($result))
{
}
foreach($r2 as $rr2)
{
$query = "select name,tid from ask_school where tid='$rr2[x2]' ";
$result2 = mysql_db_query($DataBase, $query);
$r5[] = mysql_fetch_array($result2);
}
?>
而不用:
復(fù)制代碼 代碼如下:
<?php
$j=0;
while($r2[$j] = mysql_fetch_array($result))
{
$query = "select name,tid from ask_school where tid='$r2[$j][x2]' ";
$result2 = mysql_db_query($DataBase, $query);
$r5[$j] = mysql_fetch_array($result2);
$j++;
}
?>
$j=0;
while($r2[$j] = mysql_fetch_array($result))
{
$query = "select name,tid from ask_school where tid='$r2[$j][x2]' ";
$result2 = mysql_db_query($DataBase, $query);
$r5[$j] = mysql_fetch_array($result2);
$j++;
}
?>
復(fù)制代碼 代碼如下:
<?php
$j=0;
while($r2[$j] = mysql_fetch_array($result))
{
$query = "select name,tid from ask_school where tid='$r2[$j][x2]' ";
$result2 = mysql_db_query($DataBase, $query);
$r5[$j] = mysql_fetch_array($result2);
$j++;
}
?>
$j=0;
while($r2[$j] = mysql_fetch_array($result))
{
$query = "select name,tid from ask_school where tid='$r2[$j][x2]' ";
$result2 = mysql_db_query($DataBase, $query);
$r5[$j] = mysql_fetch_array($result2);
$j++;
}
?>
答:因為PHP網(wǎng)站在運(yùn)行過程中,真正影響速度的原因只有一個,那就是對MYSQL的讀取,至于程序多運(yùn)行一兩次復(fù)制和拷貝,很難對速度造成影響.所以在讀取MYSQL的時候,我寧愿多一次內(nèi)存中的運(yùn)算,也不愿有什么操作影響到對MYSQL的讀取.
問題二:為什么不用模版寫程序?
答:因為PHP已經(jīng)是最好的模版引擎了,為什么還要用其它的模版呢?
問題三:為什么把大量的PHP代碼放在<HTML>的上方,而不是放面HTML的下方?
答:這個是有很大區(qū)別的,其一:代碼放在<HTML>上方,如果對代碼進(jìn)行修改的話,用DREARWEAVER再看下面視圖的時候,不會有慢的感覺.
其二:大部分代碼全部放在<HTML>上方,而不是放在<HTML>下方,對PHP腳本的解析運(yùn)行有好處,可以減輕PHP服務(wù)器解析腳本的負(fù)擔(dān),并且符合模版及緩存設(shè)計思想.
問題三:為什么要用
view plaincopy to clipboardprint?
<?php
$query = "select count(*) from ask_member where $t ";
$result2 = mysql_db_query($DataBase, $query);
$r3 = mysql_fetch_array($result2);
$amount=$r3[0];
?>
<?php
$query = "select count(*) from ask_member where $t ";
$result2 = mysql_db_query($DataBase, $query);
$r3 = mysql_fetch_array($result2);
$amount=$r3[0];
?>
而不是用:
view plaincopy to clipboardprint?
<?php
$query = "select * from ask_member ";
$result2 = mysql_db_query($DataBase, $query);
$amount=mysql_num_rows($result2);
?>
<?php
$query = "select * from ask_member ";
$result2 = mysql_db_query($DataBase, $query);
$amount=mysql_num_rows($result2);
?>
答:第一種是對一條記錄的查詢,第二種是對所有記錄的查詢,兩者速度有天壤之別.
問題四:為什么上面要多一行
view plaincopy to clipboardprint?
<?php
$n=count($r2)-1;
?>
<?php
$n=count($r2)-1;
?>
而不是下面直接用:
view plaincopy to clipboardprint?
<?php
for($i=0;$i<count($r2)-1;$i++)
{
?>
<?php
for($i=0;$i<count($r2)-1;$i++)
{
?>
答:應(yīng)該盡量減少在<HTML>以下進(jìn)行PHP的運(yùn)算,如果過多地在<HTML>下進(jìn)行運(yùn)算的話,那就成了混編了.
問題五:為什么說以上代碼是核心代碼?
答:任何一位程序員都知道,讀數(shù)據(jù)的分頁讀出是最常用的,我想大部分的PHP程序員30%的時間都是發(fā)在這個上面吧.
以上是我總結(jié)出的最快的寫PHP的經(jīng)驗,請廣大網(wǎng)友指正,如果覺得可以的話,希望所有人都能用這種方式來寫程序,那樣的話我以后安排一批PHP程序員進(jìn)行分工協(xié)調(diào)的時候也好辦多了.
相關(guān)文章
smarty+adodb+部分自定義類的php開發(fā)模式
smarty+adodb+部分自定義類的php開發(fā)模式...2006-12-12