I have a orders database setup with the following
Customers
Orders (which references Customers)
LineItems (which references Orders)
I’m trying to write a query that will return the top ten states based on number of line items.
My query is:
SELECT count() AS count_all, c.state AS c_state FROM line_items as li inner join orders as o on li.order_id = o.id inner join customers as c on o.customer_id = c.id GROUP BY c.state ORDER BY count() DESC;
The problem is that mysql does not use an index for one of the queries made. Here is the explain:
±—±------------±------±-----±------------------------------±-----------------------±--------±--------------±-----±--------------------------------+| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |±—±------------±------±-----±------------------------------±-----------------------±--------±--------------±-----±--------------------------------+| 1 | SIMPLE | c | ALL | PRIMARY | NULL | NULL | NULL | 6 | Using temporary; Using filesort | | 1 | SIMPLE | o | ref | PRIMARY,fk_orders_customer_id | fk_orders_customer_id | 4 | my_store.c.id | 1 | Using index | | 1 | SIMPLE | li | ref | fk_line_items_order_id | fk_line_items_order_id | 4 | my_store.o.id | 1 | Using index | ±—±------------±------±-----±------------------------------±-----------------------±--------±--------------±-----±--------------------------------+
As you can see in the first row mysql does not ever use the index on state in the customers table. Does anyone know why or how I can get it to do so? Any help would be greatly appreciated. Thanks!