博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SQL优化
阅读量:4622 次
发布时间:2019-06-09

本文共 2408 字,大约阅读时间需要 8 分钟。

  本文分析Mysql中简单查询,通过explain语句进行分析,并给出在Navicat中可使用的完整示例

一、explain介绍

  对sql语句执行explain(explain select t.mc,t.cj from test2 t where t.id =1)返回结果:

  table |  type | possible_keys | key | key_len  | ref | rows | Extra

  这里只介绍两个结果,type和rows

  type:(第一判断标准)

    显示连接使用了何种类型
    从最好到最差的连接类型为:system、const、eg_reg、ref、ref_or_null、 range、indexhe、 ALL
  rows:(为了获取结果扫描的行数,越接近结果行数越好)
    MYSQL认为必须检查的用来返回请求数据的行数

二、sql优化

1.索引问题(索引列失效)

法则:不要在建立的索引的数据列上进行下列操作:

避免对索引字段进行计算操作(包括函数)

避免在索引字段上使用not,<>,!=
避免索引列用 or 连接,这样导致索引列不可用
避免对索引列以%开头的模糊查询
避免在索引列上使用IS NULL和IS NOT NULL
避免在索引列上出现数据类型转换
避免建立索引的列中使用空值

2.其他查询

避免在WHERE子句中使用in,not in,or 或者having(可以使用 exist 和not exist代替 in和not in)

能用union all就不用union,union有个去重的过程
限制使用select * from table

三、示例

1.创建示例表

DROP TABLE IF EXISTS `test2`;CREATE TABLE `test2` (  `id` tinyint(4) NOT NULL,  `mc` varchar(11) NOT NULL,  `bj` tinyint(4) NOT NULL,  `cj` varchar(4) NOT NULL,  PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=gbk;INSERT INTO `test2` VALUES ('1', 'z3', '1', '01');INSERT INTO `test2` VALUES ('2', 'l4', '2', 'Z');INSERT INTO `test2` VALUES ('3', 'w5', '1', '03');INSERT INTO `test2` VALUES ('4', 'm6', '2', '04');INSERT INTO `test2` VALUES ('5', 'z7', '1', 'A');INSERT INTO `test2` VALUES ('6', 'w8', '2', '06');

2.索引问题

explain select t.mc,t.cj from test2 t where t.id =1;-- type:const-- 对索引字段计算或者使用函数,索引失效explain select t.mc,t.cj from test2 t where t.id*2 = 2;-- type:ALL(全局扫描)explain select t.mc,t.cj from test2 t where sqrt(t.id) = 2;-- type:ALLexplain select t.mc,t.cj from test2 t where CAST(t.id AS char) = '2';-- type:ALL-- 索引字段使用not,<>,!=索引失效explain select t.mc,t.cj from test2 t where t.id !=2;-- type:rangeexplain select t.mc,t.cj from test2 t where t.id <>2;-- type:range-- 验证模糊查询 以%开头 索引失效explain select t.mc,t.cj from test2 t where t.mc like 'z%';-- type:ALL-- 对名称创建索引CREATE INDEX index_name ON test2 (mc);explain select t.mc,t.cj from test2 t where t.mc = 'z3';-- type:refexplain select t.mc,t.cj from test2 t where t.mc like 'z%';-- type:rangeexplain select t.mc,t.cj from test2 t where t.mc like '%3';-- type:ALL

3.其他查询

-- in 查询,采用exist或者union allexplain select t.mc,t.cj from test2 t where t.id in (1,2);-- type:rangeexplain select t.mc,t.cj from test2 t where t.id=1 union all select t.mc,t.cj from test2 t where t.id=2-- or 查询explain select t.mc,t.cj from test2 t where t.id =1 or t.mc='l4';-- type:ALL

转载于:https://www.cnblogs.com/wangqiang4518/p/6973220.html

你可能感兴趣的文章
[周记]8.7~8.16
查看>>
递归定义
查看>>
kindeditor 代码高亮设置
查看>>
图的邻接表存储
查看>>
2018 leetcode
查看>>
PHP中获取当前页面的完整URL
查看>>
所谓输入掩码技术,即只有数字键起作用
查看>>
Display对象,Displayable对象
查看>>
安装oracle11G,10G时都会出现:注册ocx时出现OLE初始化错误或ocx装载错误对话框
查看>>
生产环境下正则的应用实例(一)
查看>>
在CentOS7命令行模式下安装虚拟机
查看>>
Arduino可穿戴开发入门教程Arduino开发环境介绍
查看>>
Windows平台flex+gcc词法分析实验工具包
查看>>
3.Python基础 序列sequence
查看>>
Chapter 4 Syntax Analysis
查看>>
vi/vim使用
查看>>
讨论Spring整合Mybatis时一级缓存失效得问题
查看>>
Maven私服配置Setting和Pom文件
查看>>
Linux搭建Nexus3.X构建maven私服
查看>>
NPOI 操作Excel
查看>>