BTree索引使用技巧

简介: BTree索引使用技巧

explain是解释计划,说明SQL的执行情况


explain select * from t_content where content_id = 17076710;
drop index idx_uid on t_content;
create index idx_uid on t_content(uid);
explain select * from t_content where uid=16940130;
drop index idx_uid on t_content;
create index idx_uid on t_content(uid);


精准匹配,允许使用btree索引


explain select * from t_content where uid = 16940130;


范围匹配,允许使用btree索引


explain select * from t_content where uid > 1260000 and uid < 12610000;


查询优化器会自动进行类型转换,但仍然建议使用与定义相符的类型


explain select * from t_content where uid like '1694%';
drop index idx_share_url on t_content;
create index idx_share_url on t_content(share_url);


字符串字段btree索引允许进行"前缀查询"


explain select * from t_content where share_url like 'http://a.f.budejie.com/share/%';


后缀查询与模糊匹配btree均不支持


explain select * from t_content where share_url like '%http://a.f.budejie.com/share/17076710';
explain select * from t_content where share_url like '%http://a.f.budejie.com/share/17076710%';
drop index idx_uid_sid on t_content;
create index idx_uid_sid on t_content( uid , source_id  );


复合索引查询条件必须包含左侧列


EXPLAIN select * from t_content where  uid = 14206986 ;


直接书写右侧列将导致数据无法查询,如果书写右侧列,必须要把左侧列书写上


EXPLAIN select * from t_content where uid=14206986 and  source_id = 13054 ;


<>与not in会导致不使用索引


EXPLAIN select * from t_content where  source_id <> 13054 ;
EXPLAIN select * from t_content where source_id <=13053 or source_id >=13055;



相关文章
|
5月前
|
存储 关系型数据库 索引
MyISAM主键索引树和二级索引树
MyISAM主键索引树和二级索引树
56 0
MyISAM主键索引树和二级索引树
|
5月前
|
存储 数据处理 数据库
Btree详解
Btree详解
76 0
|
5月前
|
存储 SQL 关系型数据库
InnoDB主键索引树和二级索引树
InnoDB主键索引树和二级索引树
79 0
InnoDB主键索引树和二级索引树
|
存储 SQL 缓存
MyISAM索引和InnoDB索引
MyISAM索引和InnoDB索引
|
存储 Oracle 关系型数据库
主键索引是聚集索引还是非聚集索引
在聚簇索引中,主键索引的叶子节点存储的就是数据行本身,因此主键索引也被称为聚簇索引。在这种情况下,主键索引的物理顺序与数据行的物理顺序是一致的,这样可以提高查询性能和范围查询的效率。
125 0
|
关系型数据库 数据库 索引
主键和唯一索引的区别
主键和唯一索引的区别
153 0
|
存储 NoSQL 关系型数据库
MySQL-Btree索引和Hash索引初探
MySQL-Btree索引和Hash索引初探
60 0
|
存储 SQL 关系型数据库
mysql索引(六)主键索引
主键索引(PRIMARY):它是一种特殊的唯一索引,不允许有空值。 主键索引,简称主键,原文是PRIMARY KEY,由一个或多个列组成,用于唯一性标识数据表中的某一条记录。一个表可以没有主键,但最多只能有一个主键,并且主键值不能包含NULL。
1397 0
mysql索引(六)主键索引
|
存储 关系型数据库 索引
InnoDB表主键的选择
InnoDB表都是有主键的,如果没有显示定义主键,则InnoDB首先判断表中是否有非空的唯一索引,如果有,该列即为主键。如果有多个单一列唯一索引,则按照唯一索引顺序,排在前的为主键。
1529 0
|
存储 数据库 索引