mysql-sql分析策略及优化
tranlation
事务:
从失败中回复到正常状态的方法
多个应用并发访问数据库时,提供隔离方法
acid
原子性:要么成功、要么失败
一致性:数据保持“合理性”
隔离型:多个事务同时并发执行,每个事务就像各自独立执行一样
持久性:数据持久化到硬盘
使用事务的话表的引擎为innodb引擎
默认是开启自动提交事务
engine:
存储引擎是基于表的
myisam: 不支持事务、表级锁、全文索引、奔溃恢复不好
innodb: 支持事务、行级锁、全文索引(5.6+)、奔溃恢复好
update table set id = 3 where name like 'a%';
更新行数不确定、此时采用表级锁
总结:一般来说myisam是适合不需要事务的时候:做很多count计算
innodb是适合要去事务,可靠性要求高的
推荐使用innodb
index
sql优化方案:
一、开启慢查询日志
第一步:show variables like '%slow%';
第二步:set slow_query_log = on;
slow_query_log_file:慢日志文件,只存放慢查询sql
show variables like '%long%';
long_query_time 10.00000s
第三步:set long_query_time = 0.4; 设置慢查询时间标准
注:重启mysql服务,配置会被恢复到默认。
永久生效方法,要在配置文件my.cnf中进行配置
执行sql,查找slow_query_log_file中执行慢的sql
分析:
mysql> explain select * from test where username = 'user799999';
+----+-------------+-------+------------+------+---------------+------+---------+------+--------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+------+---------------+------+---------+------+--------+----------+-------------+
| 1 | SIMPLE | test | NULL | ALL | NULL | NULL | NULL | NULL | 798401 | 10.00 | Using where |
+----+-------------+-------+------------+------+---------------+------+---------+------+--------+----------+-------------+
mysql> explain select * from test where id = '0799999';
+----+-------------+-------+------------+-------+---------------+---------+---------+-------+------+----------+-------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+-------+---------------+---------+---------+-------+------+----------+-------+
| 1 | SIMPLE | test | NULL | const | PRIMARY | PRIMARY | 4 | const | 1 | 100.00 | NULL |
+----+-------------+-------+------------+-------+---------------+---------+---------+-------+------+----------+-------+
possible_keys:可能用到的索引
key:实际用到的索引
rows:扫描的行数
mysql> show variables like '%profiling%';
+------------------------+-------+
| Variable_name | Value |
+------------------------+-------+
| have_profiling | YES |
| profiling | OFF |
| profiling_history_size | 15 |
+------------------------+-------+
3 rows in set (0.00 sec)
mysql> set profiling = on;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> show profiles;
Empty set, 1 warning (0.00 sec)
mysql> select * from test where username = 'user799999';
+---------+------------+----------+-------------+---------------------+
| id | username | password | servnumber | createtime |
+---------+------------+----------+-------------+---------------------+
| 0799999 | user799999 | | 18111818911 | 2020-01-13 22:31:18 |
+---------+------------+----------+-------------+---------------------+
1 row in set (0.31 sec)
mysql> select * from test where id = '0799999';
+---------+------------+----------+-------------+---------------------+
| id | username | password | servnumber | createtime |
+---------+------------+----------+-------------+---------------------+
| 0799999 | user799999 | | 18111818911 | 2020-01-13 22:31:18 |
+---------+------------+----------+-------------+---------------------+
1 row in set (0.00 sec)
mysql> show profiles;
+----------+------------+--------------------------------------------------+
| Query_ID | Duration | Query |
+----------+------------+--------------------------------------------------+
| 1 | 0.31259775 | select * from test where username = 'user799999' |
| 2 | 0.00039600 | select * from test where id = '0799999' |
+----------+------------+--------------------------------------------------+
2 rows in set, 1 warning (0.00 sec)
mysql> show profile cpu,block io for query 1;
+----------------------+----------+----------+------------+--------------+---------------+
| Status | Duration | CPU_user | CPU_system | Block_ops_in | Block_ops_out |
+----------------------+----------+----------+------------+--------------+---------------+
| starting | 0.000084 | 0.000050 | 0.000027 | 0 | 0 |
| checking permissions | 0.000010 | 0.000006 | 0.000003 | 0 | 0 |
| Opening tables | 0.000029 | 0.000019 | 0.000010 | 0 | 0 |
| init | 0.000035 | 0.000023 | 0.000012 | 0 | 0 |
| System lock | 0.000010 | 0.000006 | 0.000003 | 0 | 0 |
| optimizing | 0.000011 | 0.000008 | 0.000004 | 0 | 0 |
| statistics | 0.000020 | 0.000012 | 0.000007 | 0 | 0 |
| preparing | 0.000018 | 0.000013 | 0.000006 | 0 | 0 |
| executing | 0.000005 | 0.000003 | 0.000002 | 0 | 0 |
| Sending data | 0.312217 | 0.307165 | 0.000000 | 0 | 0 |
| end | 0.000026 | 0.000017 | 0.000000 | 0 | 0 |
| query end | 0.000014 | 0.000013 | 0.000000 | 0 | 0 |
| closing tables | 0.000012 | 0.000012 | 0.000000 | 0 | 0 |
| freeing items | 0.000028 | 0.000028 | 0.000000 | 0 | 0 |
| logging slow query | 0.000064 | 0.000065 | 0.000000 | 0 | 8 |
| cleaning up | 0.000015 | 0.000015 | 0.000000 | 0 | 0 |
+----------------------+----------+----------+------------+--------------+---------------+
查看性能详情是否开启
show variables like '%profiling%';
开启性能记录功能
set profiling = on;
查看性能的记录
show profiles;
show profile cpu,block io for query 4;
优化小建议:
查询* 与查询单个字段 查看性能
1、尽量避免使用select * from,尽量精确到想要的结果字段
查询两个条件 用or连接 查看执行计划
2、尽量避免使用or
3、加上limit 限制行数
4、使用like时,%放在前面是会使索引失效 查看执行计划
查询条件字段类型varchar,但条件值类型int时,会进行类型转换 查看执行计划
5、注意条件字段类型的转换会使索引失效
mysql-sql分析策略及优化的更多相关文章
- mysql索引使用策略及优化
原文地址:http://blog.codinglabs.org/articles/theory-of-mysql-index.html 索引使用策略及优化 MySQL的优化主要分为结构优化(Schem ...
- mysql 索引使用策略及优化
索引使用策略及优化 MySQL的优化主要分为结构优化(Scheme optimization)和查询优化(Query optimization).本章讨论的高性能索引策略主要属于结构优化范畴.本章的内 ...
- MySQL SQL分析(SQL profile)
分析SQL优化运营开销SQL的重要手段.在MySQL数据库.可配置profiling参数启用SQL分析.此参数可以在全局和session水平集.级别则作用于整个MySQL实例,而session级别紧影 ...
- mysql sql 分析
一.SQL 执行时间分析通过找到执行时间长的 SQL 语句,可以直观的发现数据层的效率问题. 1.通过 show processlist 来查看系统的执行情况mysql> show proces ...
- Mysql sql语句技巧与优化
一.常见sql技巧 1.正则表达式的使用 2.巧用RAND()提取随机行 mysql数据库中有一个随机函数rand()是获取一个0-1之间的数,利用这个函数和order by一起能够吧数据随机排序, ...
- MySQL定期分析检查与优化表
定期分析表 ANALYZE [LOCAL | NO_WRITE_TO_BINLOG] TABLE tbl_name [, tbl_name] 本语句用于分析和存储表的关键字分布.在分析期间,使 ...
- mysql sql 百万级数据库优化方案
1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引. 2.应尽量避免在 where 子句中对字段进行 null 值判断,否则将导致引擎放弃使用索 ...
- SQL优化 MySQL版 -分析explain SQL执行计划与笛卡尔积
SQL优化 MySQL版 -分析explain SQL执行计划 作者 Stanley 罗昊 [转载请注明出处和署名,谢谢!] 首先我们先创建一个数据库,数据库中分别写三张表来存储数据; course: ...
- SQL优化 MySQL版 -分析explain SQL执行计划与Type级别详解
type索引类型.类型 作者 : Stanley 罗昊 [转载请注明出处和署名,谢谢!] 注:看此文章前,需要有一定的Mysql基础或观看上一篇文章,该文章传送门: https://www.cnblo ...
随机推荐
- 笔记本磁盘中OEM分区的使用
(1).开机进入系统前,按F8,进入Windows 10的高级启动选项,选择“修复计算机”. (2).选择键盘输入方法. (3).如果有管理员密码,需要输入:如果没有设置密码,直接“确定”即可. (4 ...
- Couchdb垂直权限绕过到命令执行
0x00couchdb简介 Apache CouchDB是一个开源数据库,专注于易用性和成为"完全拥抱web的数据库".它是一个使用JSON作为存储格式,JavaScript作为查 ...
- 全栈之路-小程序API-SpringBoot项目中参数校验机制与LomBok工具集使用
参数校验机制在web开发中是非常重要的,每当看到现在所在公司的校验代码,我都有头疼,每一个接口都是重新写参数的校验,有些复杂的接口,参数的校验甚至占了整个接口代码量的挺大一部分的,看着我都有些头疼,我 ...
- CentOS7安装docker和docker-compose
1.安装docker # 使用yum安装docker yum -y install docker # 启动 systemctl start docker.service # 设置为开机自启动 syst ...
- D - Counting Squares
Your input is a series of rectangles, one per line. Each rectangle is specified as two points(X,Y) t ...
- 在javascript编程语言中,数据类型boolean的相关知识
一. 1.字符串类型: 空字符串返回false,非空字符串均返回true; 2.数值类型: 0或NaN返回false,其他数值返回true; 3.布尔类型: false返回false,true返回tr ...
- SQL Tuning Health-Check Script (SQLHC) (文档 ID 1366133.1)
Login to the database server and set the environment used by the Database Instance Download the &quo ...
- P6070 [RC-02] GCD [杜教筛,莫比乌斯反演]
没啥好说的,杜教筛板子题. \[\sum_{i=1}^{N} \sum_{j=1}^{N}\sum_{p=1}^{\lfloor \frac{N}{j} \rfloor}\sum_{q=1}^{\lf ...
- Linux 基础操作命令
关机和注销 shutdown -h now 立刻关机 shutdown -r now 立刻重启 shutdown -h + 1分钟后关机(重启同样用法) shutdown -h : 11点钟关机(重启 ...
- 在Windows启动pyspark shell:Failed to find Spark jars directory. You need to build Spark before running this program
D:\Develop tools\spark-2.2.0-bin-hadoop2.7\bin>pyspark2.cmd 'tools\spark-2.2.0-bin-hadoop2.7\bin\ ...