MariaDB社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 4034|回复: 6
打印 上一主题 下一主题

这个查询语句如何优化

[复制链接]
跳转到指定楼层
1#
发表于 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)

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 转播转播 分享分享 分享淘帖 顶 踩
7#
 楼主| 发表于 2011-5-6 16:52:35 | 只看该作者
回复 kider 的帖子

看我帖子里面show create table 有创建联合索引(ipfrom,ipto),而explain显示实际上却只用了ipto这个索引
6#
发表于 2011-5-6 11:59:32 | 只看该作者
explain下
5#
 楼主| 发表于 2011-5-5 18:26:06 | 只看该作者
回复 kider 的帖子

貌似使用了between and就不能用索引,是不?
4#
 楼主| 发表于 2011-5-5 13:56:24 | 只看该作者
回复 kider 的帖子

谢谢
3#
发表于 2011-4-29 13:53:28 | 只看该作者
貌似KEY `auxSearch` (`ipfrom`,`ipto`)这个索引没有用
而其后的desc排序字段,因为是limit1,看看是否可以用max()代替,或简化实际应用场景
需要根据你的实际情况,以实际运行情况来看...
2#
 楼主| 发表于 2011-4-29 12:26:21 | 只看该作者
没人气,我自己顶
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|Archiver|小黑屋|手机版|MariaDB社区 ( 京ICP备07012489号    |
业务联系: QQ:48474881; 邮箱: 48474881@qq.com; 电话:13911732319
声明:本站部分文章是网友转载,若未经作者同意或署名有误,请联系网站管理员。

GMT+8, 2024-9-21 11:07 , Processed in 0.062966 second(s), 20 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表