views:

58

answers:

2

I'm trying to speed up a query which I currently have as:

SELECT * 
FROM `events` 
WHERE (field1 = 'some string' or field1 = 'some string') 
    and is_current = true 
GROUP BY event_id 
ORDER BY pub_date

this takes roughly 30seconds.

field1 is a varchar(150)

I'm currently indexing field1, is_current, event_id, pub_data charity, pub_date, is_current and all the fields individually...

I'm really not sure what fields should be indexed together, when I remove the order by, the query speeds up to around 8 seconds, and if I removed both the order by and group by, it's less than 1 second...

What exactly should be indexed in this case to speed up the query?

Edit: I've run explain on the modified query (which no longer includes the group by):

id  select_type     table   type    possible_keys   key     key_len     ref     rows    Extra
    1   SIMPLE  events  range   is_current,field1_2,field1_3,field1_4,field1    field1_3    153     NULL    204336  Using where; Using filesort

Which indicates it's using the key field1_3 which is: field1 & is_current
Although it's not using the key which includes those two fields and pub_date (for the ordering..?)

It's also using FILESORT which seems to be the main problem..

any ideas why it's using a filesort even though the pub_date field is also indexed (with the other fields)?

+1  A: 

Everything, (field1, is_current, event_id, pub_date) in one index. MySQL can only use one index per joined table in a query.

Use EXPLAIN to see what happens when you do.

Also, an aside - as KoolKabin says, * is rarely a good idea. Sometimes MySQL will copy the rows in a temporary table; and then there are the communication costs. The less you ask from it the faster things will work.

UPDATE: I was actually wrong. Sorry. First off, you can't get full use of indexing if your grouping is different than your ordering. Second, do you have an index where your ordering key (pub_date) is first? If not, try if that fixes the ordering thing.

Amadan
I've limited the query to select only the 5 fields I'm interested in, and removed the group by for the moment, although it still takes 8-9 seconds to return the results. Also with the relevant index for all the fields used in the query.
easyjo
Hmm... Can you run EXPLAIN on it and paste it into the question? Maybe there'll be some clue...
Amadan
just updated the question with the details, it seems to be using the index which doesn't include the ordered by field in the index? (even though one exists)
easyjo
Sorry, seems I was actually a bit off. I updated the answer.
Amadan
easyjo
Umm, then I don't know why the optimizer does not select it. Sorry...
Amadan
A: 

any ideas why it's using a filesort even though the pub_date field is also indexed (with the other fields)?

This is because the mysql optimizer is trying to use index "field1" and you want the data ordered by pub_date. If you are using mysql 5.1 (the following query will give error in earlier versionn), you can force mysql to use the pub_date index for order by, something like this

SELECT * 
FROM `events` 
force index for order by (pub_date)
WHERE (field1 = 'some string' or field1 = 'some string') 
    and is_current = true 
GROUP BY event_id 
ORDER BY pub_date
gajendra.bang
Thanks for this, I'm currently running 4.x but the server is due to be upgraded to 5.x over the weekend so I will try this then.
easyjo
ok, 5.1 is the version
gajendra.bang