I’m having a problem optimizing a MySQL Query, but so far it takes too long to pull a simple result limited to 500 records.
My query is this:
SELECT ou.*, gr.accepted, gr.value
FROM offer_usermeta ou
LEFT JOIN gateway_requests AS gr ON ou.customer_id = gr.customer_id
WHERE ou.customer_id != ""
AND ou.created >= '2012-10-08 00:00:00'
AND ou.created <= '2012-10-08 23:59:59'
ORDER BY ou.created DESC LIMIT 500
This query is taking an entire minute to iterate over maybe 40,000 records. I need it to pull in gateway responses based on the customer ID if there are any and the data looks correct with this query, but I need tips on optimizing. I was considering just pulling the data in separately and foreach’ing it together as necessary, but I know that’s probably going to be far slower…
Any tips? (I’m using codeigniter if anyone knows a fancy trick in there as well)
Add an index on the
createdcolumn if you haven’t so far. The DB will have to fetch all records to do theorder byand match thewherecondition no matter if you have alimitof only 500 records. After that try