2

I have a table that I often do this type of where clause on queries:

where brand_id = 330516084 
    and influencer_id = 28995 
    and date_retrieved >= '2011-01-24' 
    and date_retrieved <= '2011-02-23'

my table has an id and 8 or so other fields, it is innodb

To make this query fast, should I create an index for brand_id and for influencer_id and for date_retrieved (1 index for each) or create 1 index with all 3 fields? Or something else?

thanks Joel

1 Answer 1

1

For that query the fastest index would be one index on brand_id and influencer_id (put the one with the most unique values first). The date conditions will not use any index because you have less than and greater than signs in the operators.

Sign up to request clarification or add additional context in comments.

2 Comments

ok cool, is the best thing to do for the date range stuff is to just clear off the stuff thats older than 6 months and put in some sort of archive table, then all my queries can run without date range?
I would not move around tables to try and predict query speed. It may not even help. If your first two indexes are selective enough the date conditions will not make that much of an impact. Best to keep a normalized design and optimize at the end. (You could use a date index if you changed >= <= to IN condition and listed the specific dates you want.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.