Permalink
Browse files

N+1 を回避

  • Loading branch information...
okashoi committed Jul 15, 2017
1 parent 080f712 commit 76ae8714bd4c1c2c184a63919e39d77146bbef89
Showing with 10 additions and 12 deletions.
  1. +10 −12 php/index.php
View
@@ -133,13 +133,12 @@ function markdown($content) {
$stmt->execute();
$memos = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach($memos as &$memo) {
$stmt = $db->prepare('SELECT username FROM users WHERE id = :id');
$stmt->bindValue(':id', $memo["user"]);
$stmt->execute();
$stmt = $db->prepare('SELECT id, username FROM users');
$stmt->execute();
$users = array_map('current', $stmt->fetchAll(PDO::FETCH_GROUP));
$result = $stmt->fetch(PDO::FETCH_ASSOC);
$memo["username"] = $result["username"];
foreach($memos as &$memo) {
$memo["username"] = $users[$memo["user"]]["username"];
}
set('memos', $memos);
@@ -162,13 +161,12 @@ function markdown($content) {
$stmt->execute();
$memos = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach($memos as &$memo) {
$stmt = $db->prepare('SELECT username FROM users WHERE id = :id');
$stmt->bindValue(':id', $memo["user"]);
$stmt->execute();
$stmt = $db->prepare('SELECT id, username FROM users');
$stmt->execute();
$users = array_map('current', $stmt->fetchAll(PDO::FETCH_GROUP));
$result = $stmt->fetch(PDO::FETCH_ASSOC);
$memo["username"] = $result["username"];
foreach($memos as &$memo) {
$memo["username"] = $users[$memo["user"]]["username"];
}
set('memos', $memos);

0 comments on commit 76ae871

Please sign in to comment.