【发布时间】:2012-06-20 15:59:30
【问题描述】:
我想问有没有更快的方法来做这个查询?实际上它需要大约 100 秒才能完成。我在一个表中有大约 10 000 000 (1 GB) 行。这是统计生成脚本。
这是查询:
$results=mysql_query("SELECT * FROM hawkeye WHERE player_id='".$playerID."'") or die("MYSQL ERROR: ".mysql_error());
然后是php代码:
$row = mysql_fetch_array($results)
if($row["action"] == 4){$commandcount++;}
else if($row["action"] == 3){$chatcount++;}
else if($row["action"] == 1){$placedcount++;}
else if($row["action"] == 0){$breakcount++;}
else if($row["action"] == 5){$joincount++;}
else if($row["action"] == 6){$quitcount++;}
else if($row["action"] == 7){$qteleportcount++;}
else if($row["action"] == 12){$pvpdeathcount++;}
else if($row["action"] == 21){$mobkillcount++;}
else if($row["action"] == 22){$otherdeathcount++;}
else {}
感谢您的回答!
【问题讨论】:
-
您真的需要一次选择所有 10 000 000 行吗?为什么?
-
立即从您的查询中删除 *。定位你想要的列,即使你想要它们,不使用*会更快。
-
让我猜猜:
player_id字段上没有索引,对吧? -
@Pekka 因为它是用于统计的。
-
可能可以直接在 mySQL 中进行这些统计。这样可以节省大量流量
标签: php mysql sql optimization