Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Dodanie orderBy znacząco zwalnia zapytanie
Intenso
post 1.05.2023, 21:11:04
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
Intenso
post 2.05.2023, 07:15:36
Post #2





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

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


Ustawiłem indeksy na wyszukiwanych kolumnach

ALTER TABLE order_items ADD INDEX idx_order_items_id (id);
ALTER TABLE order_items ADD INDEX idx_order_items_name (name);
ALTER TABLE order_items ADD INDEX idx_order_items_quantity (quantity);
ALTER TABLE customers ADD INDEX idx_customers_name (name);
ALTER TABLE payment_methods ADD INDEX idx_payment_methods_name (name);
ALTER TABLE shipping_methods ADD INDEX idx_shipping_methods_name (name);
ALTER TABLE marketplaces ADD INDEX idx_marketplaces_name (name);

mimo, że np. na name używam like %% to postanowiłem w celach testowych też założyć indeks. Niestety po wprowadzeniu tych zmian zapytania wykonują się w identycznym czasie.
Go to the top of the page
+Quote Post

Posty w temacie


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

 



RSS Wersja Lo-Fi Aktualny czas: 25.05.2024 - 00:25