MariaDB社区

标题: 这个查询语句如何优化 [打印本页]

作者: sissiyam    时间: 2011-4-28 20:24
标题: 这个查询语句如何优化
本帖最后由 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
没人气,我自己顶
作者: kider    时间: 2011-4-29 13:53
貌似KEY `auxSearch` (`ipfrom`,`ipto`)这个索引没有用
而其后的desc排序字段,因为是limit1,看看是否可以用max()代替,或简化实际应用场景
需要根据你的实际情况,以实际运行情况来看...
作者: sissiyam    时间: 2011-5-5 13:56
回复 kider 的帖子

谢谢

作者: sissiyam    时间: 2011-5-5 18:26
回复 kider 的帖子

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

作者: kider    时间: 2011-5-6 11:59
explain下
作者: sissiyam    时间: 2011-5-6 16:52
回复 kider 的帖子

看我帖子里面show create table 有创建联合索引(ipfrom,ipto),而explain显示实际上却只用了ipto这个索引





欢迎光临 MariaDB社区 (http://123.56.88.72/) Powered by Discuz! X3.2