Which fields should be indexed together? group by? Sort by?

I am trying to speed up the query that I currently have:

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

It takes about 30 seconds.

field1 is varchar (150)

I am now indexing field1, is_current, event_id, pub_data charity, pub_date, is_current and all fields individually ...

I'm really not sure which fields should be indexed together when I delete an order, the query speeds up to about 8 seconds, and if I delete both the order and the group, less than 1 second ...

What exactly needs to be indexed in this case to speed up the request?

Edit: I followed the explanation for the modified query (which no longer includes the group):

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

, 1_3, : field1 is_current
, pub_date ( ..?)

FILESORT, , , .

, , pub_date ( )?

+3
2

, (field1, is_current, event_id, pub_date) . MySQL .

EXPLAIN, , , .

, - KoolKabin, * . MySQL ; . , .

. . . -, , . -, , (pub_date) ? , , .

+1

, , pub_date ( )?

, mysql "field1", , pub_date. mysql 5.1 ( n), mysql pub_date , -

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
0

Source: https://habr.com/ru/post/1768109/


All Articles