I'm a junior dev learning rails. I have this code that queries the db and it runs really slow.
query = Users
.select("user_id")
.where(user_id: [array_of_ids], article_id: "article_id")
.where("date IN (?)", @date_range)
query = query.where(store_id: [store_ids]) if store_ids.any?
I have index on article_id, index on date, and index on user_id. But it didn't help and table is huge.
Can you help with what indices need to be added to speed up this query?
Tried adding individual indices and composite indices. It did not help with optimization. Probably I have incorrect indices.
EXPLAINand post the result into your question. Also post your current table structureSHOW CREATE TABLE table_name;.