Skip to content

Commit

Permalink
Use inner join on new items
Browse files Browse the repository at this point in the history
  • Loading branch information
rosylilly committed Sep 8, 2019
1 parent e0987b5 commit f70d520
Showing 1 changed file with 41 additions and 4 deletions.
45 changes: 41 additions & 4 deletions webapp/ruby/lib/isucari/web.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ def initialize(*args)
end

def query(sql, *args)
puts sql if ENV['LOCAL']
callback = -> (result, metrics, elapsed) do
NewRelic::Agent::Datastores.notice_sql(sql, metrics, elapsed)
end
Expand Down Expand Up @@ -201,17 +202,53 @@ def halt_with_error(status = 500, error = 'unknown')

items = if item_id > 0 && created_at > 0
# paging
db.xquery("SELECT * FROM `items` WHERE `status` IN (?, ?) AND (`created_at` < ? OR (`created_at` <= ? AND `id` < ?)) ORDER BY `created_at` DESC, `id` DESC LIMIT #{ITEMS_PER_PAGE + 1}", ITEM_STATUS_ON_SALE, ITEM_STATUS_SOLD_OUT, Time.at(created_at), Time.at(created_at), item_id)
db.xquery(
"SELECT *, " +
"`users`.`account_name`, `users`.`num_sell_items`, " +
"`categories`.`parent_id`, `categories`.`category_name` " +
"FROM `items` " +
"INNER JOIN `users` ON `items`.`seller_id` = `users`.`id` " +
"INNER JOIN `categories` ON `items`.`category_id` = `categories`.`id` " +
"WHERE `items`.`status` IN (?, ?) " +
"AND (`items`.`created_at` < ? OR (`items`.`created_at` <= ? AND `items`.`id` < ?)) " +
"ORDER BY `items`.`created_at` DESC, `items`.`id` DESC LIMIT #{ITEMS_PER_PAGE + 1}",
ITEM_STATUS_ON_SALE,
ITEM_STATUS_SOLD_OUT,
Time.at(created_at),
Time.at(created_at),
item_id
)
else
# 1st page
db.xquery("SELECT * FROM `items` WHERE `status` IN (?, ?) ORDER BY `created_at` DESC, `id` DESC LIMIT #{ITEMS_PER_PAGE + 1}", ITEM_STATUS_ON_SALE, ITEM_STATUS_SOLD_OUT)
db.xquery(
"SELECT *, " +
"`users`.`account_name`, `users`.`num_sell_items`, " +
"`categories`.`parent_id`, `categories`.`category_name` " +
"FROM `items` " +
"INNER JOIN `users` ON `items`.`seller_id` = `users`.`id` " +
"INNER JOIN `categories` ON `items`.`category_id` = `categories`.`id` " +
"WHERE `status` IN (?, ?) " +
"ORDER BY `items`.`created_at` DESC, `items`.`id` DESC LIMIT #{ITEMS_PER_PAGE + 1}",
ITEM_STATUS_ON_SALE,
ITEM_STATUS_SOLD_OUT
)
end

item_simples = items.map do |item|
seller = get_user_simple_by_id(item['seller_id'])
seller = {
'id' => item['seller_id'],
'account_name' => item['account_name'],
'num_sell_items' => item['num_sell_items']
}
halt_with_error 404, 'seller not found' if seller.nil?

category = get_category_by_id(item['category_id'])
parent_category_name = item['parent_id'] ? get_category_by_id(item['parent_id'])['category_name'] : nil
category = {
'id' => item['category_id'],
'parent_id' => item['parent_id'],
'category_name' => item['category_name'],
'parent_category_name' => parent_category_name
}
halt_with_error 404, 'category not found' if category.nil?

{
Expand Down

0 comments on commit f70d520

Please sign in to comment.