Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Dodanie orderBy znacząco zwalnia zapytanie
Intenso
post
Post #1





Grupa: Zarejestrowani
Postów: 7
Pomógł: 0
Dołączył: 16.08.2022

Ostrzeżenie: (0%)
-----


Cześć,
mam tabelę którą wypełniłem seederem i łącznie mam w niej około 500k rekordów. W Laravel generuję query builderem coś takiego:
  1. $orderItems = DB::table('order_items')
  2. ->join('orders', 'order_items.id', '=', 'orders.id')
  3. ->join('customers', 'customers.id', '=', 'orders.customer_id')
  4. ->join('payment_methods', 'payment_methods.id', '=', 'orders.payment_method_id')
  5. ->join('shipping_methods', 'shipping_methods.id', '=', 'orders.shipping_method_id')
  6. ->join('marketplaces', 'marketplaces.id', '=', 'orders.marketplace_id')
  7. ->select
  8. (
  9. 'order_items.id as order_item_id',
  10. 'order_items.created_at as order_item_created_at',
  11. 'order_items.name as order_item_name',
  12. 'order_items.quantity as order_item_quantity',
  13. 'order_items.price as order_item_price',
  14. 'customers.name as customer_name',
  15. 'payment_methods.name as payment_method_name',
  16. 'shipping_methods.name as shipping_method_name',
  17. 'marketplaces.name as marketplace_name'
  18. )
  19. ->where(function(Builder $query) {
  20. $query->where('order_items.id', '=', $this->search)
  21. ->orwhere('order_items.name', 'like', '%' . $this->search . '%')
  22. ->orwhere('order_items.quantity', '=', $this->search)
  23. ->orwhere('customers.name', 'like', '%' . $this->search . '%')
  24. ->orwhere('payment_methods.name', 'like', '%' . $this->search . '%')
  25. ->orwhere('shipping_methods.name', 'like', '%' . $this->search . '%')
  26. ->orwhere('marketplaces.name', 'like', '%' . $this->search . '%');
  27. })
  28. ->orderBy($this->sortField, $this->sortDirection)
  29. ->limit(100)
  30. ->get();


i generuje to takie zapytanie:
  1. SELECT `order_items`.`id` AS `order_item_id`, `order_items`.`created_at` AS `order_item_created_at`, `order_items`.`name` AS `order_item_name`, `order_items`.`quantity` AS `order_item_quantity`, `order_items`.`price` AS `order_item_price`, `customers`.`name` AS `customer_name`, `payment_methods`.`name` AS `payment_method_name`, `shipping_methods`.`name` AS `shipping_method_name`, `marketplaces`.`name` AS `marketplace_name` FROM `order_items` INNER JOIN `orders` ON `order_items`.`id` = `orders`.`id` INNER JOIN `customers` ON `customers`.`id` = `orders`.`customer_id` INNER JOIN `payment_methods` ON `payment_methods`.`id` = `orders`.`payment_method_id` INNER JOIN `shipping_methods` ON `shipping_methods`.`id` = `orders`.`shipping_method_id` INNER JOIN `marketplaces` ON `marketplaces`.`id` = `orders`.`marketplace_id` WHERE (`order_items`.`id` = '' OR `order_items`.`name` LIKE '%%' OR `order_items`.`quantity` = '' OR `customers`.`name` LIKE '%%' OR `payment_methods`.`name` LIKE '%%' OR `shipping_methods`.`name` LIKE '%%' OR `marketplaces`.`name` LIKE '%%') ORDER BY `order_items`.`id` DESC LIMIT 100


Problem z tym wszystkim jest takie, że dodanie order by zwiększa czas wykonywania zapytania z 1,5ms na ponad 500ms co jest kolosalną różnicą. Powyższe zapytanie jest używane do livesearch. Co tutaj jest nie tak, że dodanie sortowanie aż tak mocno zwalnia zapytanie?
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
nospor
post
Post #2





Grupa: Moderatorzy
Postów: 36 561
Pomógł: 6315
Dołączył: 27.12.2004




1) jak juz mowiono, jak nie okresliles search to nie produkuj WHERE ktore po search szukaja.
2) Jak nie musisz joinowac czegos to nei joinuj - patrz punk wyzej
3) Zamien INNER JOIN na LEFT JOIN. Wbrew pozorom to ma znaczenie. Jak LEFT JOIN nie pomoze to sprobuj samo JOIN

Oczywiscie testuj na samym order by bez warunkow.
I jesli to nie pomoze to wymus index ktory ma zostac uzyty, czyli w twoim wypadku wymus index po polu po ktorym sortujesz.

DOdatkowo zalozyleles klucze obce na laczane tabele? Bo to wszystko wyglada jakby bylo robione z lapanki
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 14.12.2025 - 17:52