0x... # This item is included in the report because its total time is ... # Scores: V/M = 0.01 # Time range: ... to ... # Attribute pct total min max avg ... # ============ === ======= ======= ======= ======= ======= # Count 50 1800 # **Exec time** 63 5225s 2s 4s 3s ... # ... SELECT `posts`.* FROM `posts` ORDER BY `created_at` DESC LIMIT 10 OFFSET 10; # Query 2: ... SELECT `comments`.* FROM `comments` WHERE `post_id` = ? ORDER BY `created_at` DESC; Exec time (合計実行時間) が大きいクエリが怪しい! 26
db.Query("SELECT * FROM posts") for posts.Next() { var post Post posts.Scan(&post.ID, &post.Title) // ループの中でクエリが発生!(N回) comments, _ := db.Query("SELECT * FROM comments WHERE post_id = ?", post.ID) count := 0 for comments.Next() { count++ } fmt.Printf("%s has %d comments\n", post.Title, count) } // → 投稿が100件あったら、101回もデータベースと通信! for 文の中で何度も DB に問い合わせをしている。 55