最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 科技 - 知识百科 - 正文

mysqlor条件可以使用索引而避免全表扫描_MySQL

来源:动视网 责编:小采 时间:2020-11-09 18:12:23
文档

mysqlor条件可以使用索引而避免全表扫描_MySQL

mysqlor条件可以使用索引而避免全表扫描_MySQL:bitsCN.com 在某些情况下,or条件可以避免全表扫描的。 1 .where 语句里面如果带有or条件, myisam表能用到索引,innodb不行。 1)myisam表: CREATE TABLE IF NOT EXISTS `a` ( `id` int(1) NOT NULL AUTO_INCREM
推荐度:
导读mysqlor条件可以使用索引而避免全表扫描_MySQL:bitsCN.com 在某些情况下,or条件可以避免全表扫描的。 1 .where 语句里面如果带有or条件, myisam表能用到索引,innodb不行。 1)myisam表: CREATE TABLE IF NOT EXISTS `a` ( `id` int(1) NOT NULL AUTO_INCREM


bitsCN.com

在某些情况下,or条件可以避免全表扫描的。

1 .where 语句里面如果带有or条件, myisam表能用到索引,innodb不行。

1)myisam表:

CREATE TABLE IF NOT EXISTS `a` (

`id` int(1) NOT NULL AUTO_INCREMENT,

`uid` int(11) NOT NULL,

`aNum` char(20) DEFAULT NULL,

PRIMARY KEY (`id`),

KEY `uid` (`uid`)

) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;

mysql> explain select * from a where id=1 or uid =2;

+----+-------------+-------+-------------+---------------+-------------+---------+------+------+---------------------------------------+

| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |

+----+-------------+-------+-------------+---------------+-------------+---------+------+------+---------------------------------------+

| 1 | SIMPLE | a | index_merge | PRIMARY,uid | PRIMARY,uid | 4,4 | NULL | 2 | Using union(PRIMARY,uid); Using where |

+----+-------------+-------+-------------+---------------+-------------+---------+------+------+---------------------------------------+

1 row in set (0.00 sec)

2)innodb表:

CREATE TABLE IF NOT EXISTS `a` (

`id` int(1) NOT NULL AUTO_INCREMENT,

`uid` int(11) NOT NULL,

`aNum` char(20) DEFAULT NULL,

PRIMARY KEY (`id`),

KEY `uid` (`uid`)

) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;

mysql> explain select * from a where id=1 or uid =2;

+----+-------------+-------+------+---------------+------+---------+------+------+-------------+

| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |

+----+-------------+-------+------+---------------+------+---------+------+------+-------------+

| 1 | SIMPLE | a | ALL | PRIMARY,uid | NULL | NULL | NULL | 5 | Using where |

+----+-------------+-------+------+---------------+------+---------+------+------+-------------+

1 row in set (0.00 sec)

2 .必须所有的or条件都必须是独立索引:

+-------+----------------------------------------------------------------------------------------------------------------------

| Table | Create Table

+-------+----------------------------------------------------------------------------------------------------------------------

| a | CREATE TABLE `a` (

`id` int(1) NOT NULL AUTO_INCREMENT,

`uid` int(11) NOT NULL,

`aNum` char(20) DEFAULT NULL,

PRIMARY KEY (`id`)

) ENGINE=MyISAM AUTO_INCREMENT=6 DEFAULT CHARSET=latin1 |

+-------+----------------------------------------------------------------------------------------------------------------------

1 row in set (0.00 sec)

explain查看:

mysql> explain select * from a where id=1 or uid =2;

+----+-------------+-------+------+---------------+------+---------+------+------+-------------+

| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |

+----+-------------+-------+------+---------------+------+---------+------+------+-------------+

| 1 | SIMPLE | a | ALL | PRIMARY | NULL | NULL | NULL | 5 | Using where |

+----+-------------+-------+------+---------------+------+---------+------+------+-------------+

1 row in set (0.00 sec)

全表扫描了。

摘自 hguisu的专栏

bitsCN.com

文档

mysqlor条件可以使用索引而避免全表扫描_MySQL

mysqlor条件可以使用索引而避免全表扫描_MySQL:bitsCN.com 在某些情况下,or条件可以避免全表扫描的。 1 .where 语句里面如果带有or条件, myisam表能用到索引,innodb不行。 1)myisam表: CREATE TABLE IF NOT EXISTS `a` ( `id` int(1) NOT NULL AUTO_INCREM
推荐度:
标签: 可以用 扫描 mysql
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top