我有一个问题,在foreach循环内执行数据库查询。
造成性能问题?
<?php
$conn=$database->openConnection();
$stmt=$conn->query('SELECT id,uuid,banned_by_uuid,until FROM bans WHERE active = 1');
$s = microtime(true);
foreach($stmt as $row){
$playerName=current($conn->query('SELECT Playername FROM SYSTEM WHERE UUID="'.$row['uuid'].'"')->fetch());
$playerNameBannedBy=current($conn->query('SELECT Playername FROM SYSTEM WHERE UUID="'.$row['banned_by_uuid'].'"')->fetch());
$playerNamePrint=$playerName == null ? 'Undefined' : htmlspecialchars($playerName);
$playerBannerPrint=$playerNameBannedBy == null ? 'Undefined' : htmlspecialchars($playerNameBannedBy);
echo '<tr><td>'.$row['id'].'</td><td>'.$playerNamePrint.'</td><td>'.$playerBannerPrint.'</td><td><i class="material-icons">access_time</i> '.ago($row['until']).'</td><td><button class="btn btn-icon white" onClick="getReason('.$row['id'].')" data-toggle="tooltip" data-placement="top" data-original-title="Click here to see Ban Reason"><i class="material-icons">info</i></button></td></tr>';
}
$e = microtime(true);
?>
答案 0 :(得分:1)
您可以通过一次查询获得所有结果
SELECT b.id,b.uuid,b.banned_by_uuid,b.until, sa.Playername, sb.Playername banned
FROM bans b
INNER JOIN SYSTEM sa on sa.UUID = b.uuid
INNER JOIN SYSTEM sb on sb.UUID = b.banned_by_uuid
WHERE b.active = 1
答案 1 :(得分:1)
是的,如果bans表中有很多记录,则对返回的记录再执行2个查询,肯定会引起问题。
尝试像这样使用联接:
SELECT id,uuid,banned_by_uuid,until, banned_players.Playername banner_player_name, banned_by_players.Playername banned_by_player_name
FROM bans
INNER JOIN SYSTEM banned_players ON banned_players.uuid = bans.uuid
INNER JOIN SYSTEM banned_by_players ON banned_by_players.uuid = bans.banned_by_uuid
WHERE active = 1
答案 2 :(得分:0)
您可以使用此查询
SELECT (CASE WHEN s.uuid = b.uuid THEN 1 ELSE 0 END) AS Separator, s.*
FROM system AS s JOIN bans AS b
ON s.uuid = b.uuid OR s.uuid = banned_by_uuid
WHERE b.active = 1
您可以了解使用分隔符列得到的两个结果之间的区别。