如果索引了多列,要遵守最左前缀法则。指的是查询从索引的最前列并且不跳过索引中的列。
like模糊查询形如'%AAA%'和'%AAA'将不会使用索引,但是业务上不可避免可能又需要使用到这种形式。
MySQL 是一个关系型数据库管理系统,由瑞典 MySQL AB 公司开发,目前属于 Oracle 公司。MySQL 是一种关联数据库管理系统,关联数据库将数据保存在不同的表中,而不是将所有数据放在一个大仓库内,这样就增加了速度并提高了灵活性。
来源:https://juejin.im/post/6871969929365553165
索引列不独立是指被索引的这列不能是表达式的一部分,不能是函数的参数,比如下面的这种情况 select id,name,age,salary from table_name where salary + 1000 = 6000; salary 列被用户表达式的计算了,这种情况下索引就会失效,解决方式就是提前计算好条件值,不要让索引列参与表达式计算。 索引字段作为函数的参数 select id,name,age,salary from table_name where substring(name,1,3)= 'luc'; 解决方式是什么呢,可以提前计算好条件,不要使用索引,或者可以使用其他的 sql 替换上面的,比如,上面的sql 可以使用 like 来代替 select id,name,age,salary from table_name where name like 'luc%'; 使用了左模糊 select id,name,age,salary from table_name where name like '%lucs%'; 平时尽可能避免用到左模糊,可以这样写 select id,name,age,salary from table_name where name like 'lucs%'; 如果实在避免不了左模糊查询的话,考虑一下搜索引擎 比如 ES or 查询部分字段没有使用索引 select id,name,age,salary from table_name where name ='lucs' and age >25 这种情况,可以为 name 和 age 都建立索引,否则会走全表扫描。 字符串条件没有使用 '' select id,name,age,salary from table_name where phone=13088772233 上面的这条 sql phone 字段类型是 字符串类型的,但是没有使用 '13088772233 ', SQL 就全表扫描了,所以字符串索引要使用 ‘’ select id,name,age,salary from table_name where phone='13088772233 ' 不符合最左前缀原则的查询 例如有这样一个组合索引 index(a,b,c) select * from table_name where b='1'and c='2' select * from table_name where c='2' // 上面这两条 SQL 都是无法走索引执行的 最左原则,就是要最左边的优先存在,我不在的话,你们自己就玩不动了,除非你自己单独创立一个索引,下面这几条 SQL 就可以走索引执行 select * from table_name where a = 'asaa' and b='1'and c='2' select * from table_name where a = 'asda' and b='1231' // 上面这两条是走索引的,但是下面这条你觉得索引应该怎么走,是全部走,还是部分走索引? select * from table_name where a = 'asda' and c='dsfsdafsfsd' 索引字段没有添加 not null 约束 select * from table_name where a is null; // 这条sql就无法走索引执行了,is null 条件 不能使用索引,只能全表扫描了 // mysql 官方建议是把字段设置为 not null 所以针对这个情况,在mysql 创建表字段的时候,可以将需要索引的字符串设置为 not null default '' 默认空字符串即可 隐式转换 关联表的两个字段类型不一致会发生隐式转换 select * from table_name t1 left join table_name2 t2 on t1.id=t2.tid; // 上面这条语句里,如果 t1 表的id 类型和 t2 表的tid 类型不一致的时候,就无法 // 按索引执行了。 // 解决方式就是统一设置字段类型。 END
不管是啥业务,最终数据都要落地,数据库这一环是肯定少不了的。随着业务发展,并发越来越高,数据库很容易成为整个链路的短板。这也是大厂面试中比较常被问到的。而调优的第一步,都是从sql语句、索引入手。先得保证单个数据库执行没问题,才会有更高层次的分库分表、弹性、容灾等等。
今天在说Mysql查询优化之前,我先说一个常见的面试题,并带着问题深入探讨研究。这样会让大家有更深入的理解。
关于MySQL 的 join,大家一定了解过很多它的“轶事趣闻”,比如两表 join 要小表驱动大表,阿里开发者规范禁止三张表以上的 join 操作,MySQL 的 join 功能弱爆了等等。这些规范或者言论亦真亦假,时对时错,需要大家自己对 join 有深入的了解后才能清楚地理解。
+ where子句类似程序语言中if条件,根据mysql表中的字段值来进行数据的过滤
三歪最近发现我一直在写MySQL的文章,然后就跟我说他有sql用到like的时候就没办法用到索引了,问我怎么办。
缓存 show variables可以查看我们mysql的许多配置,我们查一些需要的参数可以使用类似于模糊匹配的方式如下:
3、所有表必须使用Innodb存储引擎 没有特殊要求(即Innodb无法满足的功能如:列存储,存储空间数据等)的情况下,所有表必须使用Innodb存储引擎(mysql5.5之前默认使用Myisam,5.6以后默认的为Innodb)。 Innodb 支持事务,支持行级锁,更好的恢复性,高并发下性能更好。 4、每个Innodb表必须有个主键 Innodb是一种索引组织表:数据的存储的逻辑顺序和索引的顺序是相同的。每个表都可以有多个索引,但是表的存储顺序只能有一种。 Innodb是按照主键索引的顺序来组织表的
3)尽量避免NULL:很多表都包含可为NULL(空值)的列,通常情况下最好指定为NOT NULL。因为如果查询中包含可为NULL的列,对于Mysql来说更难优化。
本文主要讨论MySQL索引的部分知识。将会从MySQL索引基础、索引优化实战和数据库索引背后的数据结构三部分相关内容,下面一一展开(本文图片可点开放大)。
主键索引:每个表只有⼀个主键索引,b+树结构,叶⼦节点同时保存了主键的值也数据记录,其他节点只存储主键的值。
比如,存储字符串“101”,对于char(10),表示你存储的字符将占10个字节(包括7个空字符),在数据库中它是以空格占位的,而同样的varchar2(10)则只占用3个字节的长度,10只是最大值,当你存储的字符小于10时,按实际长度存储。
也没想好说点什么,就随便聊聊了。 这已经是随便谈一谈第三弹了,哈哈! 恰好想起来前几天的数据交流群里有人问数据匹配的问题,今天就以此为例,来聊一聊数据之间的融合。
select * from users where age >= 22 and age <= 25;
打个比方,如果我知道我管理的1000个数据库每天发生了多少张表的变更,哪些是人工触发的,哪些是程序触发的,如果我们知道,那么我们处理问题的时候会更加主动,而绝大多数情况下,其实我们是不知道的,或者说我们觉得不需要关注这些。
上篇文章中介绍了索引的基本内容,这篇文章我们继续介绍索引优化实战。在介绍索引优化实战之前,首先要介绍两个与索引相关的重要概念,这两个概念对于索引优化至关重要。
我们都知道,在做系统设计时,考虑到系统的安全性,需要对用户的一些个人隐私信息,比如:登录密码、身份证号、银行卡号、手机号等,做加密处理,防止用户的个人信息被泄露。
2、语法:select distinct from 表名; 去掉重复项,对应的字段前加符号表达:
数据库的操作越来越成为整个应用的性能瓶颈,这对于Web应用尤其明显。关于数据库的性能,这并不只是DBA需要关心的,而更是后端开发需要去关注的事情。
一、建表规约 1、表达是与否概念的字段,必须使用 is_xxx 的方式命名,数据类型是 unsigned tinyint(1 表示是, 0 表示否) 。 2、表名、字段名必须使用小写字母或数字, 禁止出现数字开头,禁止两个下划线中间只出现数字。数据库字段名的修改代价很大,因为无法进行预发布,所以字段名称需要慎重考虑。 3、表名不使用复数名词。 4、主键索引名为 pk_字段名; 唯一索引名为 uk_字段名; 普通索引名则为 idx_字段名。 5、小数类型为 decimal,禁止使用 float 和 doubl
学习mysql必备工具即安装mysql客户端;mysql安装教程在网上有很多,在此处就不在仔细说明;
📷 1. 构建搜索器参数 2. 根据时间范围筛选 3. 根据 nickname 字段模糊查询 4. hasWhere 根据关联条件来查询当前模型对象数据 1. 构建搜索器参数 ---- /** * 模型搜索器参数构造函数 * * @param array $keys * @param array $where */ function where_filter(&$keys, array $where) { // 去掉数组里的空字符串和null $where = array_fil
本文主要讨论MySQL索引的部分知识。将会从MySQL索引基础、索引优化实战和数据库索引背后的数据结构三部分相关内容,下面一一展开。
作者:哪 吒 来源:blog.csdn.net/guorui_java/article/details/12654200 一、查询SQL尽量不要使用select *,而是具体字段 1、反例 SELECT * FROM user 2、正例 SELECT id,username,tel FROM user 3、理由 节省资源、减少网络开销。 可能用到覆盖索引,减少回表,提高查询效率。 注意:为节省时间,下面的样例字段都用*代替了。 二、避免在where子句中使用 or 来连接条件 1、反例 SELECT
关于SQL优化相关的问题,相信很多同学在面试过程中都有被问到过,要么不知道,要么回答不清楚。见于此情况,勇哥今天有空,就和大家聊聊这个相关的话题。
在 DB2 数据库中索引采用的是 B+ 树的结构,索引的叶子节点上包含索引键的值和一个指向数据地址的指针。DB2 先查询索引,然后通过索引里记录的指针,直接访问表的数据页。
概念 1.普通索引:最基本的索引,它没有任何限制 2.唯一索引:索引列的值必须唯一,且不能为空,如果是组合索引,则列值的组合必须唯一。 3.主键索引:特殊的索引,唯一的标识一条记录,不能为空,一般用primary key来约束。 4.联合索引:在多个字段上建立索引,能够加速查询到速度 5.Union:对两个结果集进行并集操作,不包括重复的行,同时进行默认规则排序 6.Union all:对两个结果集进行并集操作,包括重复行,不进行排序
一、实践中如何优化mysql 1) SQL语句及索引的优化 2) 数据库表结构的优化 3) 系统配置的优化 4) 硬件优化 二、索引的底层实现原理和优化 2.1 底层实现 在DB2数据库中索引采用的是B+树的结构,索引的叶子节点上包含索引键的值和一个指向数据地址的指针。DB2先查询索引,然后通过索引里记录的指针,直接访问表的数据页。 B+树是应数据库所需而出现的一种B树的变形树。 B+树的特点: (1)所有叶节点包含全部关键字及指向相应记录的指针,而且叶节点中将关键字按大小顺序排列
假如有联合索引 (emp_no 、title、from_date ),那么下面的 SQL 中 emp_no 可以用到索引,而title 和 from_date 则使用不到索引。
注意:索引的数目不是越多越好。每个索引都需要占用磁盘空间,索引越多,需要的磁盘空间就越大。修改表时,对索引的重构和更新很麻烦。越多的索引,会使更新表变得很浪费时间。
学习索引,主要是写出更快的sql,当我们写sql的时候,需要明确的知道sql为什么会走索引?为什么有些sql不走索引?sql会走那些索引,为什么会这么走?我们需要了解其原理,了解内部具体过程,这样使用起来才能更顺手,才可以写出更高效的sql。本篇我们就是搞懂这些问题。
本栏目Java开发岗高频面试题主要出自以下各技术栈:Java基础知识、集合容器、并发编程、JVM、Spring全家桶、MyBatis等ORMapping框架、MySQL数据库、Redis缓存、RabbitMQ消息队列、Linux操作技巧等。
辅助记忆,诗曰: 全值匹配我最爱, 最左前缀要遵守; 带头大哥不能死, 中间兄弟不能断; 索引列上少计算, 范围之后全失效; 模糊百分写最右, 覆盖索引不写星; 不等空值还有或, 索引失效要少用; 字符引号不可丢, 牢记以上就无忧。
领取专属 10元无门槛券
手把手带您无忧上云