I’ve been working on a mysql database, and one of the most important tables that have the DataBase will be between 900 and 1000 million records. Im able to do this query:
select MAX(SEN_ID) as SEN_ID from senal group by variable_VAR_ID
but it takes like 12 minutes. How can I optimize this query?
This query was working fine when the database was small, but now that there are millions of rows in the database, I am realizing I should have looked at optimizing this earlier.
The query execute within EXPLAIN show this:
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE senal index NULL fk_senal_variable1 4 NULL 6333242 Using index
You need to create composite index
variable_VAR_ID + SEN_IDNote 1: not two separated indexes, but one composite
Note 2: yes, the order matters
PS: here is a syntax description of how to create indexes: http://dev.mysql.com/doc/refman/5.5/en/create-index.html