MYSQL数据库规范-sql
来源:互联网 发布:淘宝助理数据包是什么 编辑:程序博客网 时间:2024/06/05 07:06
(1)sql语句尽可能简单,一条sql只能在一个cpu运算大语句拆小语句,减少锁时间,一条大sql可以堵死整个库;
(2)简单的事务,事务时间尽可能短
【bad case】 分布式事务根据根据接口执行提交事务:超时等造成数据死锁、数据不一致等问题
上传图片事务 – 参考阿里和银行接口 对账
(3)避免使用trig/func
触发器、函数不用 客户端程序取而代之
(4)OR改写为IN()
or的效率是n级别 in的消息时log(n)级别 in的个数建议控制在200以内
select id from t where phone=’159′ or phone=’136′;
=>select id from t where phone in (’159′, ’136′);
(5)OR改写为UNION
select id from t where phone = ’159′ or name = ‘john’;
=>select id from t where phone=’159′union select id from t where name=’jonh’
(6)避免负向% Like’ %join’ 索引无效; like ’john%’ 索引无效
(7)慎用count(*)
这中间还存在逻辑的差异,count(*)是查询表记录多少行,count(col)是查询(not null)的个数的
你需要考虑加了where字句的情况
看这个链接: https://www.zhihu.com/question/50171821?from=profile_question_card
count(id)全表扫,困为取了大部分的记录,所以用时少count(*)时走了status的索引,走了索引之后,还要再次回表。所以用的时间长。
(8)limit高效分页limit越大,效率越低select id from t limit 10000, 10;=>select id from t where id > 10000 limit 10;
(9)使用union all替代unionunion有去重开销
(10)请使用同类型比较
(11)使用load data导数据 load data比insert快约20倍;
(12)新能分析工具show profile;mysqlsla;mysqldumpslow;explain;show slow log;show processlist;show query_response_time(percona)
- MYSQL数据库规范-sql
- MySQL数据库SQL开发规范
- Mysql数据库设计规范之三数据库SQL开发规范
- MYSQL数据库规范-字段规范
- 数据库命名规范--通用(sql,mysql,orcle,db)
- 数据库SQL编写规范
- 数据库SQL编写规范
- 数据库SQL开发规范
- SQL Server 数据库规范
- mysql sql规范整
- mysql--12100sql规范
- mysql数据库开发规范
- mysql数据库开发规范
- MySQL数据库开发规范
- MYSQL 数据库总体规范
- MYSQL数据库规范-索引
- MySQL数据库命名规范
- MySQL数据库开发规范
- 由于空格引起的xml错误之The processing instruction target matching "[xX][mM][lL]" is not allowed以及the markup in the document prec
- python 从零学
- Bluetooth 相关
- 无法打开包括文件的解决办法
- spring
- MYSQL数据库规范-sql
- python set集合操作
- node 多重监听
- 在MySQL中创建表。
- 【magento2命令行安装 WINDOWS】Composer异常: Your configuration does not allow connection to xx.,
- Qurrtus ii下载以及SDRAM调试的问题
- 第四章:Dubbo整合maven+spring+springmvc+mybatis之mybatis集成
- orcl数据库记载资料
- LeetCode:Set Matrix Zeroes