I am wondering if my Index setup is correct in MySQL (InnoDB). I am not an expert on indexes but from what I read about them I hope I understood well. To make sure I am right, I would like to ask members of this community.
I have two tables:
1) "users", with fields: id, fullname, status, sex,...
2) "orders", with fields id, id_user, order_date
When I locate an order I link it through "id_user" to table "users" (id).
There are around 1 mio of users and "status" field in "orders" can have 10 different values.
Primary Index
I have a PRIMARY index on field "users:id", which I believe it is useful when I do queries like "SELECT FROM users USE INDEX (PRIMARY) WHERE (id=33)" and when I will link order to certain user. Similar index I have in table "orders". This is probably ok.
The problem
Because I will search orders by their status and then link it to correct user, I created an index on two fields in table "orders":
Index name "status__id_user", fields (status, id_user)
My query to list new orders and link it with right users looks like:
SELECT * FROM orders USE INDEX (status__id_user), users USE INDEX (PRIMARY) WHERE (orders.status='new') AND (orders.id_user = users.id)
Question
Do I need index on two fields just because I am linking to users table with field "id_user"? Does it speed up things or it would be same if my index in "orders" table is just by field "status"? In that case 100,000 records would be selected and then MySQL would link it to "users" table to get correct users.
My question is if my index helps in anyway to speed up this part of the query:
AND (orders.id_user = users.id)
Thank you!
Aucun commentaire:
Enregistrer un commentaire