-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
アプリ:mysqlの取り扱い, mysql ライブラリ #10
Comments
N+1 について |
mapを使う方法https://medium.com/eureka-engineering/golang-beginner-2d687bd8a831 Before: // 例 いいねをしたお相手一覧を取得する
// ユーザーの使ったいいねのデータを取得
likes, _ := FindLikesByUserID(meID)
responses := make([]UserResponse, len(likes))
for i, like := range likes {
r := UserResponse{}
partnerID := like.PartnerID
// partnerIDからお相手の情報を取得
// select * from `user` where `id` = [partnerID]
p, _ := FindUserByID(partnerID)
r.ID = p.ID
r.Name = p.Name
// お相手の画像を取得
// select * from `user_image` where `user_id` = [partnerID]
image, _ := FindUserImageByUserID(partnerID)
r.ImagePath = image.Path
responses[i] = r
} After: // ユーザーの使ったいいねのデータを取得
likes, _ := FindLikesByUserID(meID)
partnerIDs := make([]int64, len(likes))
for i, like := range likes {
partnerIDs[i] = like.PartnerID
}
// お相手の情報を取得
// select * from `user` where `id` in ([partnerIDs])
partners, _ := FindUsersByIDs(partnerIDs)
partnerMap := map[int64]User{}
for _, p := range partners {
partnerMap[p.ID] = p
}
// お相手の画像を取得
// select * from `user_image` where `user_id` in ([partnerIDs])
images, _ := FindUserImagesByUserIDs(partnerIDs)
partnerImageMap := map[int64]UserImage{}
for _, image := range images {
partnerImageMap[image.UserID] = image
}
responses := make([]UserResponse, len(partnerIDs))
for i, id := range partnerIDs {
r := UserResponse{}
if p, ok := partnerMap[id]; ok {
r.ID = p.ID
r.Name = p.Name
}
if image, ok := partnerImageMap[id]; ok {
r.ImagePath = image.Path
}
responses[i] = r
}
|
eagar load を利用する方法(gorm などを使っているなら)eagar load を狙った preload http://gorm.io/docs/preload.html db.Preload("Orders").Find(&users)
// SELECT * FROM users;
// SELECT * FROM orders WHERE user_id IN (1,2,3,4);
db.Preload("Orders", "state NOT IN (?)", "cancelled").Find(&users)
// SELECT * FROM users;
// SELECT * FROM orders WHERE user_id IN (1,2,3,4) AND state NOT IN ('cancelled');
db.Where("state = ?", "active").Preload("Orders", "state NOT IN (?)", "cancelled").Find(&users)
// SELECT * FROM users WHERE state = 'active';
// SELECT * FROM orders WHERE user_id IN (1,2) AND state NOT IN ('cancelled');
db.Preload("Orders").Preload("Profile").Preload("Role").Find(&users)
// SELECT * FROM users;
// SELECT * FROM orders WHERE user_id IN (1,2,3,4); // has many
// SELECT * FROM profiles WHERE user_id IN (1,2,3,4); // has one
// SELECT * FROM roles WHERE id IN (4,5,6); // belongs to |
joins を利用する方法(database/sql などを使っているなら)を使っていっぺんにデータ取得 |
|
go の mysql ライブラリの変更はやってみないとわからんなあ… |
コネクションプールのチューニングについてはこちら |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The text was updated successfully, but these errors were encountered: