sissiyam 发表于 2011-4-28 20:24:04

这个查询语句如何优化

本帖最后由 sissiyam 于 2011-4-28 20:25 编辑

请教如何利用好索引加快查询速度

语句如下:
mysql>select *from table1 where 3393547226 between ipfrom and ipto order by status desc, width desc limit 1;

表定义:
| table1 | CREATE TABLE ` table1` (
`id` int(11) NOT NULL auto_increment,
`ipfrom` int(10) unsigned NOT NULL default '0',
`ipto` int(10) unsigned NOT NULL default '0',
`width` int(10) unsigned NOT NULL default '0',

`status` enum('','aux','usr') NOT NULL,
PRIMARY KEY(`id`),
UNIQUE KEY `ipfrom` (`ipfrom`),
KEY `ipto` (`ipto`),
KEY `auxSearch` (`ipfrom`,`ipto`)
) ENGINE=MyISAM AUTO_INCREMENT=134824497 DEFAULT CHARSET=latin1 |


mysql> explain select *from table1 where 3393547226 between ipfrom and ipto order by status desc, width desc limit 1;
+----+-------------+-------------------+-------+-----------------------+------+---------+------+------+-----------------------------+
| id | select_type | table             | type| possible_keys         | key| key_len | ref| rows | Extra                     |
+----+-------------+-------------------+-------+-----------------------+------+---------+------+------+-----------------------------+
|1 | SIMPLE      | table1 | range | ipfrom,ipto,auxSearch | ipto | 4       | NULL | 9440 | Using where; Using filesort |
+----+-------------+-------------------+-------+-----------------------+------+---------+------+------+-----------------------------+
1 row in set (0.00 sec)

sissiyam 发表于 2011-4-29 12:26:21

没人气,我自己顶

kider 发表于 2011-4-29 13:53:28

貌似KEY `auxSearch` (`ipfrom`,`ipto`)这个索引没有用
而其后的desc排序字段,因为是limit1,看看是否可以用max()代替,或简化实际应用场景
需要根据你的实际情况,以实际运行情况来看...

sissiyam 发表于 2011-5-5 13:56:24

回复 kider 的帖子

谢谢

sissiyam 发表于 2011-5-5 18:26:06

回复 kider 的帖子

貌似使用了between and就不能用索引,是不?

kider 发表于 2011-5-6 11:59:32

explain下

sissiyam 发表于 2011-5-6 16:52:35

回复 kider 的帖子

看我帖子里面show create table 有创建联合索引(ipfrom,ipto),而explain显示实际上却只用了ipto这个索引
页: [1]
查看完整版本: 这个查询语句如何优化