Forum Moderators: coopster
this is the exat query
SELECT ip_locate FROM iplocate
WHERE INET_ATON(%s) BETWEEN ip_start AND ip_end
ORDER BY ip_end-ip_start
LIMIT 1
explain result:
string(8) table='iplocate'
string(3) type='ALL'
NULL possible_keys=null
NULL key=null
NULL key_len=null
NULL ref=null
string(5) rows='29032'
string(27) Extra='Using where; Using filesort'
after changed into "WHERE ip_start<INET_ATON(%s) AND INET_ATON(%s)<ip_end"
got explain result:
string(8) table='iplocate'
string(5) type='range'
string(8) possible_keys='ip_start'
string(8) key='ip_start'
string(1) key_len='4'
NULL ref=null
string(3) rows='788'
string(27) Extra='Using where; Using filesort'
my index is: key(ip_start,ip_end)
ip_end is not the possible_keys
changed into: key1(ip_start), key2(ip_end)
ip_end is in the possible_keys, but not used, only ip_start is used
hope anyone can tell me how to optimize it
and the order by can be optimize?
key it's not used if i store ip_end-ip_start into ip_width, then order by ip_width (indexed)
[mysql.com...]
ALL
A full table scan will be done for each combination of rows from the previous tables. This is normally not good if the table is the first table not marked const, and usually very bad in all other cases. You normally can avoid ALL by adding more indexes, so that the row can be retrieved based on constant values or column values from earlier tables.
So what about trying to add more indexes as it mentions?