Skip to content

Commit

Permalink
Fix buyer N+1
Browse files Browse the repository at this point in the history
  • Loading branch information
rosylilly committed Sep 8, 2019
1 parent 7783ec0 commit 8463ba3
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions webapp/ruby/lib/isucari/web.rb
Original file line number Diff line number Diff line change
Expand Up @@ -437,8 +437,11 @@ def halt_with_error(status = 500, error = 'unknown')
db.xquery(
"SELECT `items`.*, " \
"`user_stats`.`account_name`, `user_stats`.`num_sell_items` " \
", `buyer_stats`.`account_name` AS `buyer_name`, " \
"`buyer_stats`.`num_sell_items` AS `buyer_num_sell_items` " \
"FROM `items` " \
"INNER JOIN `user_stats` ON `user_stats`.`user_id` = `items`.`seller_id`" \
"LEFT OUTER JOIN `user_stats` `buyer_stats` ON `buyer_stats`.`user_id` = `items`.`buyer_id` " \
"WHERE (`items`.`seller_id` = ? OR `items`.`buyer_id` = ?) " \
"AND `items`.`status` IN (?, ?, ?, ?, ?) " \
"AND (`items`.`created_at` < ? OR (`items`.`created_at` <= ? AND `items`.`id` < ?)) " \
Expand All @@ -465,8 +468,11 @@ def halt_with_error(status = 500, error = 'unknown')
db.xquery(
"SELECT `items`.*, " \
"`user_stats`.`account_name`, `user_stats`.`num_sell_items` " \
", `buyer_stats`.`account_name` AS `buyer_name`, " \
"`buyer_stats`.`num_sell_items` AS `buyer_num_sell_items` " \
"FROM `items` " \
"INNER JOIN `user_stats` ON `user_stats`.`user_id` = `items`.`seller_id`" \
"LEFT OUTER JOIN `user_stats` `buyer_stats` ON `buyer_stats`.`user_id` = `items`.`buyer_id` " \
"WHERE (`items`.`seller_id` = ? OR `items`.`buyer_id` = ?) " \
"AND `items`.`status` IN (?, ?, ?, ?, ?) " \
"ORDER BY `items`.`created_at` DESC, `items`.`id` DESC LIMIT #{TRANSACTIONS_PER_PAGE + 1}",
Expand Down Expand Up @@ -545,14 +551,12 @@ def halt_with_error(status = 500, error = 'unknown')
}

if item['buyer_id'] != 0
buyer = buyers[item['buyer_id']]
if buyer.nil?
db.query('ROLLBACK')
halt_with_error 404, 'buyer not found'
end

item_detail['buyer_id'] = item['buyer_id']
item_detail['buyer'] = buyer
item_detail['buyer'] = {
'id' => item['buyer_id'],
'account_name' => item['buyer_name'],
'num_sell_items' => item['buyer_num_sell_items']
}
end

shipping = shippings[item['id']]
Expand Down Expand Up @@ -1423,7 +1427,6 @@ def halt_with_error(status = 500, error = 'unknown')
db.xquery('INSERT INTO `users` (`account_name`, `hashed_password`, `address`) VALUES (?, ?, ?)', account_name, hashed_password, address)
user_id = db.last_id
db.xquery('INSERT INTO `user_stats` (`account_name`, `hashed_password`, `address`, `user_id`) VALUES (?)', account_name, hashed_password, address, user_id)
db.query('COMMIT')
rescue => e
puts e.full_message
db.query('ROLLBACK')
Expand Down

0 comments on commit 8463ba3

Please sign in to comment.