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 ...
随机推荐
- 三星正在改善1Gb MRAM寿命问题
据报道三星已经成功研发出有望替代嵌入式闪存存储器(eFlash)的嵌入式磁阻随机访问内存(eMRAM),容量为1Gb,测试芯片的优良率已达90%. 随着5G物联网时代的来临,存储器领域发展快速,而在这 ...
- jdk8中接口中的特性
jdk8中可以定义静态方法(public static)和默认方法(public default),public 可以省略 调用接口中的静态方法时:只能通过接口本身来调用,不能被该接口的实现类来调 调 ...
- UESTC 1324 卿学姐与公主 分块板子
#include<iostream> #include<cmath> using namespace std; ; //表示当前数在哪一块里面 int belong[maxn] ...
- C#实现图片文件到数据流,再到图片文件的转换
//----引入必要的命名空间 using System.IO; using System.Drawing.Imaging; //----代码部分----// private byte[] photo ...
- WPF 控件功能重写(ComboBox回车搜索)
前言:在我们日常使用软件的时候,Combobox会让用户很方便的选择出需要的东西,但是ComboBox中的下拉行数过多时就不那么好用了. 如果在项目中有很多这样的ComboBox控件的话,我们可以考虑 ...
- Selenium实战(四)——unittest单元测试框架1
Python中的单元测试框架包含:doctest.unittest.pyttest.nose等,使用unittest单元测试框架不需要自行定义断言失败的提示,并且当一个测试函数执行失败后,后面的测试函 ...
- Bazinga HDU - 5510【技巧暴力+字符串】
题目:https://vjudge.net/problem/HDU-5510 $2015ACM/ICPC$ 亚洲区沈阳站 题目大意: 输入$t$(表示样例个数) 如何每个样例一个 $n$,表示字符串的 ...
- BZOJ 4247: 挂饰 动态规划
按照挂件数量排序,然后做一个 DP 就好了. code: #include <bits/stdc++.h> #define ll long long #define N 2003 #def ...
- 解决ios手机中input输入框光标过长的问题
修改前css部分代码: .receiving-info .receiving-info-list input { display: inline-block; width: 70%; font-siz ...
- tmp = 2/4;竟然没有发现的
我还纠结着单目运算符和双目运算符和乘除的一些优先级什么事情. #include "common.h" #include <stdio.h> #include <s ...