如果索引了多列,要遵守最左前缀法则。指的是查询从索引的最前列并且不跳过索引中的列。
📷 1. 构建搜索器参数 2. 根据时间范围筛选 3. 根据 nickname 字段模糊查询 4. hasWhere 根据关联条件来查询当前模型对象数据 1. 构建搜索器参数 ---- /** * 模型搜索器参数构造函数 * * @param array $keys * @param array $where */ function where_filter(&$keys, array $where) { // 去掉数组里的空字符串和null $where = array_fil
为了验证 MySQL 中哪些情况下会导致索引失效,我们可以借助 explain 执行计划来分析索引失效的具体场景。
like模糊查询形如'%AAA%'和'%AAA'将不会使用索引,但是业务上不可避免可能又需要使用到这种形式。
不管是啥业务,最终数据都要落地,数据库这一环是肯定少不了的。随着业务发展,并发越来越高,数据库很容易成为整个链路的短板。这也是大厂面试中比较常被问到的。而调优的第一步,都是从sql语句、索引入手。先得保证单个数据库执行没问题,才会有更高层次的分库分表、弹性、容灾等等。
MySQL 是一个关系型数据库管理系统,由瑞典 MySQL AB 公司开发,目前属于 Oracle 公司。MySQL 是一种关联数据库管理系统,关联数据库将数据保存在不同的表中,而不是将所有数据放在一个大仓库内,这样就增加了速度并提高了灵活性。
👋 你好,我是 Lorin 洛林,一位 Java 后端技术开发者!座右铭:Technology has the power to make the world a better place.
今天在说Mysql查询优化之前,我先说一个常见的面试题,并带着问题深入探讨研究。这样会让大家有更深入的理解。
目前有许许多多的ORM,但是目前最为流行的依然是sequelize,所以这里总结写之前自己写自己的博客所涉及到的点,分享给大家,让大家也可以少踩坑,更快入门。
来源:https://juejin.im/post/6871969929365553165
三歪最近发现我一直在写MySQL的文章,然后就跟我说他有sql用到like的时候就没办法用到索引了,问我怎么办。
阿里编码规范要求:至少要达到 range 级别,要求是 ref 级别,如果可以是 consts 最好
辅助记忆,诗曰: 全值匹配我最爱, 最左前缀要遵守; 带头大哥不能死, 中间兄弟不能断; 索引列上少计算, 范围之后全失效; 模糊百分写最右, 覆盖索引不写星; 不等空值还有或, 索引失效要少用; 字符引号不可丢, 牢记以上就无忧。
注意:索引的数目不是越多越好。每个索引都需要占用磁盘空间,索引越多,需要的磁盘空间就越大。修改表时,对索引的重构和更新很麻烦。越多的索引,会使更新表变得很浪费时间。
优化方式:用代码拼装sql时进行判断,没 where 条件就去掉 where,有where条件就加 and。
在mysql中,有时我们在做数据库查询时,需要得到某字段中包含某个值的记录,但是它也不是用like能解决的,使用like可能查到我们不想要的记录,它比like更精准,这时候mysql的FIND_IN_SET函数就派上用场了,下面来具体了解一下。
索引列不独立是指被索引的这列不能是表达式的一部分,不能是函数的参数,比如下面的这种情况 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
3)尽量避免NULL:很多表都包含可为NULL(空值)的列,通常情况下最好指定为NOT NULL。因为如果查询中包含可为NULL的列,对于Mysql来说更难优化。
记得那是一条查询SQL,数据量万级时还保持在0.2秒内,随着某一段时间数据猛增,耗时一度达到了2-3秒!没有命中索引,导致全表扫描。explain 中extra显示:Using where; Using temporary; Using filesort,被迫使用了临时表排序,由于是高频查询,并发一起来很快就把DB线程池打满了,导致大量查询请求堆积,DB服务器cpu长时间100%+,大量请求timeout。。最终系统崩溃。老板登场~
最近工作上需要实现搜索功能,尝试了几种方案。虽然最终线上部署的还是最low的方案,但是中间的过程还是比较有意思的。业务上根据关键字查找内容。关键字的出处多来源于标题,文章描述等。主要实现方式有一些几种,各个方式各有利弊,需要权衡。
所有的数据库对象名称必须使用小写字母并用下划线分割(MySQL大小写敏感,名称要见名知意,最好不超过32字符) 所有的数据库对象名称禁止使用MySQL保留关键字(如 desc、range、match、delayed 等,请参考 MySQL官方保留字 【https://dev.mysql.com/doc/refman/5.7/en/keywords.html】 ) 临时库表必须以tmp为前缀并以日期为后缀(tmp_) 备份库和库必须以bak为前缀并以日期为后缀(bak_) 所有存储相同数据的
所有的数据库对象名称必须使用小写字母并用下划线分割(MySQL大小写敏感,名称要见名知意,最好不超过32字符) 禁止在数据中存储图片,文件二进制数据(使用文件服务器)
Sequelize 是 Node 的一个 ORM(Object-Relational Mapping) 框架,用来方便数据库操作。
步骤 3:指定记录慢查询日志 SQL 执行时间的阈值(long_query_time 单位:秒,默认 10 秒)。
MySQL已经成为世界上最受欢迎的数据库管理系统之一,无论是用在小型开发项目上,还是用在构建那较大型的网站,MySQL都用实力证明了自己是一个稳定、可靠、快速、可信的系统,足以胜任任何数据存储业务的需要。
【强制】业务上具有唯一特性的字段,即使是多个字段的组合,也必须建成唯一索引。 说明:不要以为唯一索引影响了insert速度,这个速度损耗可以忽略,但提高查找速度是明显的;另外,即使在应用层做了非常完善的校验控制,只要没有唯一索引,根据墨菲定律,必然有脏数据产生。
对于MySQL,如果一个SQL没有指定order by 列名,此时的查询结果集的数据顺序是不可预期的。
其实对于上面的观点一定程度上是正确的,但不是完全正确。但之所以流传这么广,主要还是没有搞清楚实际状态,而根据实际使用中总结出来的一些模糊规律。只有了解的MySQL的Join实际执行方式,就会知道上面2种观点是一种模糊的规律,这种规律并不能指导我们实际开发。下面就说说MySQL的实际join执行方式。
我们知道加密后的数据对模糊查询不是很友好,本篇就针对加密数据模糊查询这个问题来展开讲一讲实现的思路,希望对大家有所启发。
数据库的操作越来越成为整个应用的性能瓶颈,这对于Web应用尤其明显。关于数据库的性能,这并不只是DBA需要关心的,而更是后端开发需要去关注的事情。
MySQL语法总结 查询相关: 查看数据库: show databases; 查看数据库信息: show create database 数据库名; 查看表信息: show create table 表名; 查看当前数据库版本号: select version(); 查看系统当前时间: select new(); 查看当前用户: select user(); 查看当前所在的数据库: select database(); 查看MySQL数据库状态: status; 查看表的结构
大多数的MySQL服务器都开启了查询缓存。这是提高性能最有效的方法之一,而且这是被MySQL的数据库引擎处理的。当有很多相同的查询被执行了多次的时候,这些查询结果会被放到一个缓存中,这样,后续的相同的查询就不用操作表而直接访问缓存结构了。
比如,存储字符串“101”,对于char(10),表示你存储的字符将占10个字节(包括7个空字符),在数据库中它是以空格占位的,而同样的varchar2(10)则只占用3个字节的长度,10只是最大值,当你存储的字符小于10时,按实际长度存储。
前言 这里筑梦师,是一名正在努力学习的iOS开发工程师,目前致力于全栈方向的学习,希望可以和大家一起交流技术,共同进步,用简书记录下自己的学习历程. 全栈开发自学笔记 已开源 深入浅出后端开发(SQL指令笔记) 本文阅读建议 1.一定要辩证的看待本文. 2.本文并不会涉及到MySQL详细知识点,只陈述在学习MySQL过程中遇到的关键点. 3.MySQL依照其开发应用分为'基础篇,高级篇,实际应用',根据本人学习进度更新. 4.本文只阐述学习路线和学习当中的重点问题.需要读者自己使用百度进行拓展学习.
我们都知道,在做系统设计时,考虑到系统的安全性,需要对用户的一些个人隐私信息,比如:登录密码、身份证号、银行卡号、手机号等,做加密处理,防止用户的个人信息被泄露。
本文主要讨论MySQL索引的部分知识。将会从MySQL索引基础、索引优化实战和数据库索引背后的数据结构三部分相关内容,下面一一展开(本文图片可点开放大)。
3、所有表必须使用Innodb存储引擎 没有特殊要求(即Innodb无法满足的功能如:列存储,存储空间数据等)的情况下,所有表必须使用Innodb存储引擎(mysql5.5之前默认使用Myisam,5.6以后默认的为Innodb)。 Innodb 支持事务,支持行级锁,更好的恢复性,高并发下性能更好。 4、每个Innodb表必须有个主键 Innodb是一种索引组织表:数据的存储的逻辑顺序和索引的顺序是相同的。每个表都可以有多个索引,但是表的存储顺序只能有一种。 Innodb是按照主键索引的顺序来组织表的
+ where子句类似程序语言中if条件,根据mysql表中的字段值来进行数据的过滤
规范业务系统对MySQL数据库在设计、开发、运维等阶段所必须遵循的原则,旨在控制对数据库的滥用,收敛不合理的使用形式,保障数据库安全、稳定、高效的运行以及业务运营的稳定性。
高并发大数据的互联网业务,架构设计思路是“解放数据库 CPU,将计算转移到服务层”,并发量大的情况下,这些功能很可能将数据库拖死,业务逻辑放到服务层具备更好的扩展性,能够轻易实现“增机器就加性能”。数据库擅长存储与索引,CPU 计算尽量挪到上层
一、建表规约 1、表达是与否概念的字段,必须使用 is_xxx 的方式命名,数据类型是 unsigned tinyint(1 表示是, 0 表示否) 。 2、表名、字段名必须使用小写字母或数字, 禁止出现数字开头,禁止两个下划线中间只出现数字。数据库字段名的修改代价很大,因为无法进行预发布,所以字段名称需要慎重考虑。 3、表名不使用复数名词。 4、主键索引名为 pk_字段名; 唯一索引名为 uk_字段名; 普通索引名则为 idx_字段名。 5、小数类型为 decimal,禁止使用 float 和 doubl
领取专属 10元无门槛券
手把手带您无忧上云