MySQL: Optimal index for between queries

后端 未结 5 861
南笙
南笙 2021-02-19 13:26

I have a table with the following structure:

CREATE TABLE `geo_ip` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `start_ip` int(10) unsigned NOT NULL,
  `end_ip         


        
5条回答
  •  刺人心
    刺人心 (楼主)
    2021-02-19 14:15

    If you create an index for start_ip and one for end_ip, I found I could get comparable results to Jeshurun's results without doing the order by, using an inner join with the same table:

    select a.* from geo_ip a inner join geo_ip b on a.id=b.id where 2393196360 >= a.start_ip and 2393196360 <= b.end_ip limit 1;
    

    Also you will find MySQL uses a partial index instead of reporting a full-index scan which is more comforting to me.

提交回复
热议问题