From f70d520bf123d19a6aec7874d10d4db10abac2fd Mon Sep 17 00:00:00 2001 From: Sho Kusano Date: Sun, 8 Sep 2019 11:36:27 +0900 Subject: [PATCH] Use inner join on new items --- webapp/ruby/lib/isucari/web.rb | 45 +++++++++++++++++++++++++++++++--- 1 file changed, 41 insertions(+), 4 deletions(-) diff --git a/webapp/ruby/lib/isucari/web.rb b/webapp/ruby/lib/isucari/web.rb index 85f3ace..8cb1031 100755 --- a/webapp/ruby/lib/isucari/web.rb +++ b/webapp/ruby/lib/isucari/web.rb @@ -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 @@ -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? {