开发者

Mysql, does using index for selects with operators >,< improve performance?

I need to select a row from table that has m开发者_StackOverflow社区ore than 5 millions of rows. It is table of all IP ranges. Every row has columns upperbound and lowerbound. All are bigintegers, and the number is integer representation of IP address.

The select is:

select * 
  from iptocity 
 where lowerbound < 3529167967 
   and upperbound >= 3529167967 
 limit 1;

My problem is

...that the select takes too long time. Without index on an InnoDB table, it takes 20 seconds. If it is on MyISAM then it takes 6 seconds. I need it to be less than 0.1 sec. And I need to be able to handle hundreds of such selects per second.

I tried to create index on InnoDB, on both columns upperbound and lowerbound. But it didn't help, it took even more time, like 40 seconds to select ip. What is wrong? Why did index not help? Does index work only on = operator, and >,< operators can't use indexes? What should I do to make it faster to have less than 0.1 sec selection time?


Did you create one index on both columns, or two indexes one column each? If only one index, then this could be your problem, make one index for each. Indexes should still work for < and >

Aside from changing indexes around, run:

EXPLAIN
select *
from iptocity
where lowerbound<3529167967
  and upperbound>=3529167967
limit 1;

Same query as you had, just added EXPLAIN and some linebreaks for readability.

The EXPLAIN keyword will make MySQL explain to you how it's running the query, and it will tell you whether indexes are being used or not. For any slow running query, always use explain to try figuring out what's going on.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜