문제

아래에 엄청나게 큰 추악한 쿼리가 있습니다. 카탈로그 보기에서 이를 정렬하고 싶습니다.다음과 같은 생각 http://wow.dev:3000/catalog_items?&order=deals.어떤 의견이나 답변이라도 미리 감사드립니다.

select 100 - round((current_price / item.estimated_price)*100) as percent, item.cached_thumbnail_url, item.item_id, it.name,
          ci.current_price, ci.close_date
           from item
           join catalog_item ci on ci.item_id = item.item_id
           join item_translations as it on (it.item_id = item.item_id)
           where  (100 - round((current_price / item.estimated_price)*100)) > 49 and 
           item.estimated_price > 0 and ci.current_price > 0 and ci.close_date > now() and item.active = 1 and ci.active = 1 and 
           (current_price / estimated_price) < 1
           order by (ci.close_date < DATE_ADD(now(), INTERVAL 17 hour))  and (item.estimated_price - current_price) desc
           limit 12
도움이 되었습니까?

해결책

이것이 ROR과 어떻게 관련될 수 있는지는 확실하지 않지만 어쨌든:

  1. 당신은 100 - round((current_price / item.estimated_price)*100) as percent SELECT 절에 있지만 어쨌든 WHERE 조건에서 동일한 표현식을 사용합니다.

  2. 할 수 있다 item.estimated_price 0보다 작을까?그렇지 않다면 item.estimated_price > 0 조건이 과도합니다. 0이면 (100 - round((current_price / item.estimated_price)*100)) > 49 조건이 거짓이 됩니다

  3. 그만큼 (current_price / estimated_price) < 1 같은 이유로 너무 과하다

따라서 쿼리를 다음과 같이 좀 더 명확하게 다시 작성할 수 있습니다.

select (100 - round((current_price / item.estimated_price)*100)) as percent,
   item.cached_thumbnail_url, item.item_id, it.name,
   ci.current_price, ci.close_date
from item
   join catalog_item ci on ci.item_id = item.item_id
   join item_translations as it on (it.item_id = item.item_id)
where
   percent > 49
   and ci.current_price > 0
   and ci.close_date > now()
   and item.active = 1
   and ci.active = 1
order by
   (ci.close_date < DATE_ADD(now(), INTERVAL 17 hour))
   and (item.estimated_price - current_price) desc
limit 12

이로 인해 상황이 많이 개선되지는 않지만 DB 아키텍처에 대한 더 이상의 이유를 알지 못하면 더 이상 말할 수 없습니다.

그런데 귀하의 질문에 있는 링크가 작동하지 않습니다(분명히 로컬 링크입니다)

다른 팁

Binarycode의 답변을 바탕으로 ARel에서 쿼리를 래핑한 다음 컨트롤러 작업에서 다음과 같이 params 해시에 전달된 필드에 대해 주문할 수 있습니다.

class ItemsController < ApplicationController
  ...
  def index
    @items = Item.select(%{(100 - round((current_price / item.estimated_price)*100)) as percent,
               item.cached_thumbnail_url, item.item_id, it.name,
               ci.current_price, ci.close_date}).
             joins('join catalog_item ci on ci.item_id = item.item_id').
             joins('join item_translations as it on (it.item_id = item.item_id)').
             where('percent > 49 and ci.current_price > 0 and ci.close_date > now() and item.active = 1 and ci.active = 1').
             order(%{(ci.close_date < DATE_ADD(now(), INTERVAL 17 hour))
               and (item.estimated_price - current_price) desc)}).limit(12)
    @items = @items.order(params[:order]) if params[:order]
    ...
end

편집하다

바이너리 코드가 아래에서 지적한 것처럼 기본 쿼리를 작업 외부(아마도 Item 모델을 반환하고 여전히 Relation (현재 명령문과 마찬가지로) 그것으로부터 객체를 생성하므로 추가 항목을 연결할 수 있습니다. order 나중에:

def index
  @items = Item.by_price_ratio
  @items = @items.order(params[:order]) if params[:order]
end
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top