首页 文章

带有索引的MariaDB SQL性能问题

提问于
浏览
0

我们有一个表,用于在MariaDB mariadb-5.5.37-1.el7_0.x86_64上查找IP地理位置,如下所示:

CREATE TABLE ip2location_db24(id int(11)NOT NULL AUTO_INCREMENT,ip_from int(10)unsigned DEFAULT NULL,ip_to int(10)unsigned DEFAULT NULL,country_code char(2)COLLATE utf8_bin DEFAULT NULL,country_name varchar(64)COLLATE utf8_bin DEFAULT NULL,region_name varchar(128)COLLATE utf8_bin DEFAULT NULL,city_name varchar(128)COLLATE utf8_bin DEFAULT NULL,纬度double DEFAULT NULL,经度double DEFAULT NULL,zip_code varchar(30)COLLATE utf8_bin DEFAULT NULL,time_zone varchar(8)COLLATE utf8_bin DEFAULT NULL,isp varchar(255)COLLATE utf8_bin DEFAULT NULL,domain varchar(128)COLLATE utf8_bin DEFAULT NULL,net_speed varchar(8)COLLATE utf8_bin DEFAULT NULL,idd_code varchar(5)COLLATE utf8_bin DEFAULT NULL,area_code varchar(30)COLLATE utf8_bin DEFAULT NULL,weather_station_code varchar(10)COLLATE utf8_bin DEFAULT NULL,weather_station_name varchar(128)COLLATE utf8_bin DEFAULT NULL,mcc varchar(256)COLLATE utf8_bin DEFAULT NULL,mnc varchar(256)COLLATE utf8_bin DEFAU LT NULL,mobile_brand varchar(128)COLLATE utf8_bin DEFAULT NULL,elevation int(10)DEFAULT NULL,usage_type varchar(11)COLLATE utf8_bin DEFAULT NULL,PRIMARY KEY(id),KEY idx_ip_from(ip_from),KEY idx_latitude(latitude),KEY idx_longitude(经度),KEY idx_ip_from_to_2(ip_to,ip_from))ENGINE = InnoDB AUTO_INCREMENT = 9541211 DEFAULT CHARSET = utf8 COLLATE = utf8_bin

ip_from和ip_to列定义每个地理位置的起始和结束边界 .

我们在此表中有大约1000万条记录 .

在查询给定IP的地理位置时,我们发现服务器在以下SQL中遇到严重的性能问题:

select * from ip2location_db24其中ip_to> = 1908980838,ip_from <= 1908980838 limit 1; *************************** 1 . 排******************** ******* id:5475739 ip_from:1908932608 ip_to:1909063679 country_code:CN country_name:CHINA region_name:Shanxi city_name:TAIYUAN纬度:37.86944经度:112.56028 zip_code: - time_zone:08:00 isp:CHINA UNICOM SHANNXI PROVINCE NETWORK domain :CHINAUNICOM.COM net_speed:DSL idd_code:86 area_code:0351 weather_station_code:CHXX0129 weather_station_name:TAIYUAN mcc:460 mnc:01/06 mobile_brand:CHINA UNICOM提升:787 usage_type:ISP / MOB 1行(15.08秒)

但是,在使用以下等效SQL查询时,它的速度非常快 .

select * from ip2location_db24 where ip_from <= 1908980838 order by ip_from desc limit 1 \ G *************************** 1. row ** ************************* id:5475739 ip_from:1908932608 ip_to:1909063679 country_code:CN country_name:CHINA region_name:Shanxi city_name:TAIYUAN纬度:37.86944经度:112.56028 zip_code: - time_zone:08:00 isp:CHINA UNICOM SHANNXI PROVINCE NETWORK域名:CHINAUNICOM.COM net_speed:DSL idd_code:86 area_code:0351 weather_station_code:CHXX0129 weather_station_name:TAIYUAN mcc:460 mnc:01/06 mobile_brand:CHINA UNICOM elevation :787 usage_type:ISP / MOB 1行集(0.00秒)

问题是,当我们检查执行计划时,两个查询都对ip_from列的索引使用相同的范围扫描 . 但这两个SQL的表现远未接近 . 任何人都知道这个原因吗?

为了提供更多信息,我们还测试了其输出列完全被索引覆盖的查询 .

MariaDB [ip2location]选择ip_from,ip_to来自ip2location_db24,其中ip_to> = 1908980838,ip_from <= 1908980838 limit 1; ------------ ------------ | ip_from | ip_to | ------------ ------------ | 1908932608 | 1909063679 | ------------ ------------ 1行(0.01秒)

注意上面的查询SQL很快 . 但是当查询索引未涵盖的任何其他列时,需要令人难以置信的长时间:

MariaDB [ip2location]从ip2location_db24中选择ip_from,ip_to,country_code,其中ip_to> = 1908980838,ip_from <= 1908980838 limit 1; ------------ ------------ -------------- | ip_from | ip_to | country_code | ------------ ------------ -------------- | 1908932608 | 1909063679 | CN | ------------ ------------ -------------- 1排(10.15秒)

2 回答

  • 0
    select * from ip2location_db24 where ip_to >=1908980838 and ip_from <=1908980838 limit 1;
    

    由于比较两列缓慢 .

    select * from ip2location_db24 where ip_from <=1908980838 order by ip_from desc limit 1
    

    快速因为只有一个索引列进行了比较 .

  • -1

    按照MariaDB手册部分Forcing an index中的说明尝试 force index

    select ip_from,ip_to,country_code
    from ip2location_db24 force index (idx_ip_from_to_2)
    where ip_to >=1908980838 and ip_from <=1908980838
    limit 1;
    

相关问题