试图改善缓慢的MySQL查询查找

问题描述 投票:0回答:1

我有一个大约有300万条记录的MySQL数据库:

enter image description here

它包含IP地理位置信息:

enter image description here

并且该表定义如下:

enter image description here

我的目标是通过任意IP地址找到一个条目。我为此使用以下SQL(PHP语法):

$n_ip = ip2long("106.87.84.64");    //or 1784108096
$res = mysqli_query($link, 
    "SELECT * FROM `ip2location` WHERE `ip_from`<='$n_ip' AND `ip_to`>='$n_ip' LIMIT 1");

上面的查询似乎需要大约4.5秒。

我想知道,我可以做些什么来提高性能吗?

mysql performance query-performance
1个回答
0
投票

仅通过更改索引并不能解决IP查找问题。参见此内容,以获取有关如何使此类查找非常高效(毫秒而不是秒)的讨论:http://mysql.rjweb.org/doc.php/ipranges

© www.soinside.com 2019 - 2024. All rights reserved.