English 中文(简体)
Mysql 加入操作器之间不使用索引
原标题:mysql join not use index for between operator

所以我基本上有三张桌子:

CREATE TABLE `cdIPAddressToLocation` (
  `IPADDR_FROM` int(10) unsigned NOT NULL COMMENT  Low end of the IP Address block ,
  `IPADDR_TO` int(10) unsigned NOT NULL COMMENT  High end of the IP Address block ,
  `IPLOCID` int(10) unsigned NOT NULL COMMENT  The Location ID for the IP Address range ,
  PRIMARY KEY  (`IPADDR_TO`),
  KEY `Index_2` USING BTREE (`IPLOCID`),
  KEY `Index_3` USING BTREE (`IPADDR_FROM`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

CREATE TABLE `cdIPLocation` (
  `IPLOCID` int(10) unsigned NOT NULL default  0 ,
  `Country` varchar(4) default NULL,
  `Region` int(10) unsigned default NULL,
  `City` varchar(90) default NULL,
  `PostalCode` varchar(10) default NULL,
  `Latitude` float NOT NULL,
  `Longitude` float NOT NULL,
  `MetroCode` varchar(4) default NULL,
  `AreaCode` varchar(4) default NULL,
  `State` varchar(45) default NULL,
  `Continent` varchar(10) default NULL,
  PRIMARY KEY  (`IPLOCID`)
) ENGINE=MyISAM AUTO_INCREMENT=218611 DEFAULT CHARSET=latin1;

CREATE TABLE  data {
  IP  varchar(50)
  SCORE  int
}

My task is to join these three tables 和 find the location data for given IP address. My query is as follows:

select 
    t.ip,
    l.Country,
    l.State,
    l.City,
    l.PostalCode,
    l.Latitude,
    l.Longitude,
    t.score
from
    (select 
        ip, inet_aton(ip) ipv, score
    from
        data
    order by score desc
    limit 5) t
        join
    cdIPAddressToLocation a ON t.ipv between a.IPADDR_FROM 和 a.IPADDR_TO
        join
    cdIPLocation l ON l.IPLOCID = a.IPLOCID

虽然这个查询很有效,但非常缓慢, 它花了大约100秒的时间才将结果归还给我的Dev盒子。

I m using mysql 5.1, the cdIPAddressToLocation has 5.9 million rows 和 cdIPLocation table has about 0.3 million rows.

When I check the execution plan, I found it s not using any index in the table cdIPAddressToLocation , so for each row in the data table it would do a full table scan against table cdIPAddressToLocation . It is very weird to me. I mean since there are already two indexes in table cdIPAddressToLocation on columns IPADDR_FROM 和 IPADDR_TO , the execution plan should exploit the index to improve the performance, but why it didn t use them.

还是我的查询有问题?

帮帮忙,多谢了

问题回答

您是否尝试过在 cdIP addressTolocation.IPADRD_FROM 栏和 cdIP addressTolocation.IPADRD_TO 栏上使用综合索引?

multiple-column indexes





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签