2011-09-19 169 views
0

我有获取生成(按Django的)查询一个荒谬的量是这样的:查询需要的时间

SELECT `geo_ip`.`id`, `geo_ip`.`start_ip`, 
     `geo_ip`.`end_ip`, `geo_ip`.`start`, 
     `geo_ip`.`end`, `geo_ip`.`cc`, `geo_ip`.`cn` 
FROM `geo_ip` 
WHERE (`geo_ip`.`start` <= 2084738290 AND `geo_ip`.`end` >= 2084738290) 
LIMIT 1 

它查询一个大地定位表与它134189项。添加索引时,每个查询需要> 100ms才能执行,这使得它不能用于一次性事物。我将缓存响应,因此我只需要执行一次IP查找,但是我很好奇,如果我错过了一个明显的方法来让它快一点。我的表:

CREATE TABLE `geo_ip` (
    `start_ip` char(15) NOT NULL, 
    `end_ip` char(15) NOT NULL, 
    `start` bigint(20) NOT NULL, 
    `end` bigint(20) NOT NULL, 
    `cc` varchar(6) NOT NULL, 
    `cn` varchar(150) NOT NULL, 
    `id` int(11) NOT NULL AUTO_INCREMENT, 
    PRIMARY KEY (`id`), 
) ENGINE=InnoDB AUTO_INCREMENT=134190 DEFAULT CHARSET=latin1 

两个列上创建一个索引,像这样:

ALTER TABLE geo_ip ADD INDEX (start, end); 

提供了以下解释:

EXPLAIN SELECT geo_ip.id, geo_ip.start_ip, geo_ip.end_ip, 
       geo_ip.start, geo_ip.end, geo_ip.cc, geo_ip.cn 
FROM geo_ip 
WHERE (geo_ip.end >= 2084738290 AND geo_ip.start < 2084738290) 
LIMIT 1; 
+----+-------------+--------+-------+---------------+-------+---------+------+-------+----------+-------------+ 
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | filtered | Extra  | 
+----+-------------+--------+-------+---------------+-------+---------+------+-------+----------+-------------+ 
| 1 | SIMPLE  | geo_ip | range | start   | start | 8  | NULL | 67005 | 100.00 | Using where | 
+----+-------------+--------+-------+---------------+-------+---------+------+-------+----------+-------------+ 

这需要超过100毫秒即可完成选择:

SELECT geo_ip.id, geo_ip.start_ip, geo_ip.end_ip, 
     geo_ip.start, geo_ip.end, geo_ip.cc, 
     geo_ip.cn 
FROM geo_ip 
WHERE (geo_ip.end >= 2084738290 and geo_ip.start < 2084738290) 
LIMIT 1; 
+-------+--------------+----------------+------------+------------+----+-----------+ 
| id | start_ip  | end_ip   | start  | end  | cc | cn  | 
+-------+--------------+----------------+------------+------------+----+-----------+ 
| 51725 | 124.66.128.0 | 124.66.159.255 | 2084732928 | 2084741119 | SG | Singapore | 
+-------+--------------+----------------+------------+------------+----+-----------+ 
1 row in set (0.18 sec) 

更省钱已经比拥有一个单一的个体指数:

ALTER TABLE geo_ip ADD INDEX (`start`); 
ALTER TABLE geo_ip ADD INDEX (`end`); 
+----+-------------+--------+-------+---------------+-------+---------+------+-------+-------------+ 
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra  | 
+----+-------------+--------+-------+---------------+-------+---------+------+-------+-------------+ 
| 1 | SIMPLE  | geo_ip | range | start,end  | start | 8  | NULL | 68017 | Using where | 
+----+-------------+--------+-------+---------------+-------+---------+------+-------+-------------+ 

需要大约100毫秒来完成这些请求:

SELECT geo_ip.id, geo_ip.start_ip, geo_ip.end_ip, geo_ip.start, geo_ip.end, geo_ip.cc, geo_ip.cn FROM geo_ip 
WHERE (geo_ip.end >= 2084738290 AND geo_ip.start < 2084738290) limit 1; 
+-------+--------------+----------------+------------+------------+----+-----------+ 
| id | start_ip  | end_ip   | start  | end  | cc | cn  | 
+-------+--------------+----------------+------------+------------+----+-----------+ 
| 51725 | 124.66.128.0 | 124.66.159.255 | 2084732928 | 2084741119 | SG | Singapore | 
+-------+--------------+----------------+------------+------------+----+-----------+ 
1 row in set (0.11 sec) 

但是这两种方法都需要太长的方式,是有可能做到这事?

回答

0

时间总是在“where”子句中消耗。

而且由于您正在使用“低于”或“大于”两个不同的字段,它必须读取很多索引才能找出您想要的记录。

我应该做我的表是这样的:与地理索引

select * from table where geo between '2084732927' and '2084732928' 

+-------+-------+----------------+------------+----+-----------+ 
| id | type | ip    | geo  | cc | cn  | 
+-------+-------+----------------+------------+----+-----------+ 
| 51725 | start | 124.66.159.255 | 2084732928 | SG | Singapore | 
+-------+-------+----------------+------------+----+-----------+ 
| 51726 | end | 124.66.159.255 | 2084732928 | SG | Singapore | 
+-------+-------+----------------+------------+----+-----------+ 

,这样我可以选择此项。 应该更快,更快。但抱歉,我没有时间尝试。