라라벨 오더 반응 속도를 크게 늦춤으로써
불필요한 정보는 생략하고 테이블 구조는 다음과 같습니다(일부 관계 리스트는 아닙니다).
products
id
launch_date
name
product_view_history
id
account_id
product_id
timestamps
저는 불규칙적으로 시간이 걸리는 질문이 있습니다.지금까지의 프로파일링을 통해 SQL에 소비하는 실제 시간은 매우 짧습니다(<50 ms
단, 이 코드 실행에 걸리는 시간은900+ms
범위:
$this->select('products.*', DB::raw('COUNT(product_view_history.id) as view_count'))
->leftJoin('product_view_history', 'product_view_history.product_id', '=', 'products.id', 'outer')
->groupBy('product_view_history.product_id')
->orderBy('view_count', 'DESC')
->orderBy('products.id', 'DESC')
->whereNotNull('products.launch_date')
->with(['owner.images', 'owner.star', 'owner.follows', 'owner.followers', 'company.products.alphas'])
->take(Config::get('xxxx.limits.small'))
->get();
그러나 이 코드가 실행되기까지 걸리는 시간은 적절하게 단축됩니다.<50ms
코멘트하면->orderBy('view_count', 'DESC')
교환하면get()
와 함께toSql()
두 쿼리를 모두 수동으로 실행합니다. 시간이 비교적 비슷하고 짧습니다.이 시간이 걸리는 시간을 명확하게 측정하는 것은 SQL 쿼리 시간이 아닙니다.이 작업이 완료되기 전과 직후의 시간을 밀리초 단위로 취득하여 차이를 기록합니다.
왜 그런지 알 수 있는 사람 있나요?->orderBy('view_count', 'DESC')
SQL 자체는 그다지 느리지 않지만 코드 실행에 거의 1초 가까이 시간이 걸릴까요?
쿼리를 raw로 실행하고 수분을 공급하고 로딩하면 쿼리 속도가 빨라지는 것 같습니다.이 명령으로 인해 이러한 문제가 발생하는 이유는 설명되지 않지만, 당면한 문제를 해결하는 방법은 설명됩니다.
$products = self::hydrate(DB::select(
"select `products`.*, COUNT(product_view_history.id) as view_count
from `products` left join `product_view_history`
on `product_view_history`.`product_id` = `products`.`id`
where `products`.`launch_date` is not null
group by `product_view_history`.`product_id`
order by `view_count` desc, `products`.`id` desc limit {$limit}"))
->load(['owner.images', 'owner.star', 'owner.follows', 'owner.followers', 'company.products.alphas']);
이 문제는 where 쿼리에서 "잘못된" 데이터 유형을 사용했기 때문에 발생했습니다.예를 들어 varchar인 "username"이라는 열을 기준으로 필터링했지만 필터링 기준으로 Int를 값으로 삽입했습니다.orderBy를 사용할 때는 시간이 많이 걸렸지만 orderBy를 삭제할 때는 다시 빠릅니다.해결책은 적어도 사용자 이름을 String에 캐스팅하는 것이었고 orderBy는 이전과 같이 유창했습니다.진짜 이유는 모르겠지만, 데이터 타입이 일치하지 않을 경우, 언변 캐스트나 정렬이 다를 수 있습니다.
언급URL : https://stackoverflow.com/questions/43055765/laravel-orderby-slowing-down-response-greatly
'it-source' 카테고리의 다른 글
Maria DB 버전 10.1.18에서 JSON_EXTRACT를 사용하는 방법 (0) | 2023.01.15 |
---|---|
org.postgresql.displays.displayPSQLException:FATAL:죄송합니다.이미 클라이언트가 너무 많습니다. (0) | 2023.01.15 |
테이블 관계를 설정하기 위해 "Cascade", "Set Null" 및 "Restrict"는 무엇을 합니까? (0) | 2023.01.15 |
Java 8: Java.util.function의 TriFunction(및 kin)은 어디에 있습니까?아니면 대체방법이 뭐죠? (0) | 2023.01.15 |
n번째 문자마다 문자열을 분할하시겠습니까? (0) | 2023.01.15 |