MySQL - replacing subquery with something more efficient -


I am trying to display a list of customers, as well as the number of their orders and the date of their last date order. Some customers may not have placed any orders.

I am filtering customers from their registration date, currently I am interested in registered customers recently. I think my current query is running the first sub-section for all customers and then look at WHERE.

How can I make this query more efficient?

  Select Customers. , Addresses *, (Select from where customer ID = customers.customerID) select the order (*), (Choose order date from order where customer id = customers.customerID order orderID desc limit 1) Customers from previous order customers address Have been added. DefaultCollection = Addresses.addressID where registration date> = '2014-02-04'  

This is your query, with the aliases of the table on every column, it is clearly known where they come from (I hope I will correct them):

  select c. *, A. *, (From the count count (*) order where o.customerID = c.customerID), select o.orderDate from the order (where o.customerID = c.customerID order o.orderID desc range 1) by customers When joining C as the last order, c.defaultCollection = a.addressID addresses where c.registrationDate & gt; = '2014-02-04'; The query is not necessarily bad. It will benefit from some indexing:  Customer (Registration Date, Default Selection, Customer ID) ,  Addresses (Address ID ) , and the most important  order (customer ID, order ID, order date) . These indexes you want that can be enough to reach the display. 

In fact, it can not be converted to a set query, because you must add a group:

  C. *, A. *, Count (o.customerId) as numOrders, substring_index (order order order by o.Oderder DD), ',', 1) Select Customer as LastOrderIdOrderDate, when connecting to c.defaultCollection = A.addressID click left oo o.customerId = c.customerId where c.registrationDate & gt; = '2014-02-04' group c.customerId, a.AddressId;  

substring_index () Expression What to do in your question - Get the date of the largest orderId . If this is the date of the maximum order, then a better expression is maximum (O.


Comments