jeudi 19 novembre 2015

How to optimize and Fast run SQL query

I have following SQL query that taking too much time to fetch data.

Customer.joins("LEFT OUTER JOIN renewals ON customers.id = renewals.customer_id").where("renewals.customer_id IS NULL && customers.status_id = 4").order("created_at DESC").select('first_name, last_name, customer_state, customers.created_at, customers.customer_state, customers.id, customers.status_id')

Above query takes 230976.6ms to execute.

I added indexing on firstname, lastname, customer_state and status_id.

How can I execute query within less then 3 sec. ?

Aucun commentaire:

Enregistrer un commentaire