select * 为什么不好? limit 1 为什么好? --mysql SQL语句优化
问题一:
Select * from student; 这种语句不好
我的理解:根据Innode存储引擎以及网上的各种资料所说的innodb的B+树索引结构可以分析出,当在非聚集索引列上搜索若用select * 会发生索引覆盖的问题。下面请看演示:
首先我们的表中的数据是:

表的结构是:

我们可以看到:表里有三个索引,primary 索引,name列的name_index索引,以及age列的age_index索引。
然后我们进行搜索:


我们发现分别搜索*,id,age三个列进行了搜索,我们预计的结果是*不使用索引,id和age会使用age_index索引,但是呈现的是三个都使用了索引。然后我们在以name作为where的判定条件进行select * 搜索。我们预计的是也不使用name_index,但explain现实的依旧是使用索引,如下:

这是由于我们的表中的数据量太小,当表中的数据量较小时,存储引擎的优化器依旧会使用非聚集索引然后再进行一次书签查找。
索引我们给表中在增加几条数据,如下:

然后再进行刚才的以name为判定条件进行select*查找。


我们发现此时,select * 是没有使用非聚集索引name_index,而id是使用到了索引的。
所以,以上理论成立!
总结如下:

问题二:limit 1 为什么效率高
网上有一篇文章说的比较清楚:http://www.linuxidc.com/Linux/2013-03/81974.htm
大概意思是因为加上LIMIT 1,只要找到了对应的一条记录,就不会继续向下扫描了,效率会大大提高。
由于mysql自带的explain和profiles 并不能检测搜索了多少条语句,所以并不能直观的验证,可通过cpu使用率间接分析,但完整的测试需要用到大量非重复数据,所以没有直观的截图来验证。这里来引用mysql官方文档来作为例证:
Mysql官方文档对于limit的定义:
7.3.1.10 LIMIT Optimization
In some cases, MySQL handles a query differently when you are using LIMIT row_count and not using HAVING:
If you are selecting only a few rows with LIMIT, MySQL uses indexes in some cases when normally it would prefer to do a full table scan.
If you use LIMIT row_count with ORDER BY, MySQL ends the sorting as soon as it has found the firstrow_count rows of the sorted result, rather than sorting the entire result. If ordering is done by using an index, this is very fast. If a filesort must be done, all rows that match the query without the LIMIT clause must be selected, and most or all of them must be sorted, before it can be ascertained that the first row_count rows have been found. In either case, after the initial rows have been found, there is no need to sort any remainder of the result set, and MySQL does not do so.
When combining LIMIT row_count with DISTINCT, MySQL stops as soon as it finds row_count unique rows.
In some cases, a GROUP BY can be resolved by reading the key in order (or doing a sort on the key) and then calculating summaries until the key value changes. In this case, LIMIT row_count does not calculate any unnecessary GROUP BY values.
As soon as MySQL has sent the required number of rows to the client, it aborts the query unless you are usingSQL_CALC_FOUND_ROWS.
LIMIT 0 quickly returns an empty set. This can be useful for checking the validity of a query. When using one of the MySQL APIs, it can also be employed for obtaining the types of the result columns. (This trick does not work in the MySQL Monitor (the mysql program), which merely displays Empty set in such cases; you should instead use SHOW COLUMNS or DESCRIBE for this purpose.)
When the server uses temporary tables to resolve the query, it uses the LIMIT row_count clause to calculate how much space is required.
从上面加大的字体可以看出,mysql官方文档又说到:
When combining MySQL stops as soon as it finds
所以,例证成
select * 为什么不好? limit 1 为什么好? --mysql SQL语句优化的更多相关文章
- MYSQL SQL语句优化
1.EXPLAIN 做MySQL优化,我们要善用EXPLAIN查看SQL执行计划. 下面来个简单的示例,标注(1.2.3.4.5)我们要重点关注的数据: type列,连接类型.一个好的SQL语句至少要 ...
- 自制小工具大大加速MySQL SQL语句优化(附源码)
引言 优化SQL,是DBA常见的工作之一.如何高效.快速地优化一条语句,是每个DBA经常要面对的一个问题.在日常的优化工作中,我发现有很多操作是在优化过程中必不可少的步骤.然而这些步骤重复性的执行,又 ...
- MySQL - SQL语句优化方法
1.使用 show status 了解各种 SQL 的执行频率 mysql> show status like 'Com%'; 该命令可以查询 sql 命令的执行次数. 2.定位执行效率较低的 ...
- 重新学习MySQL数据库12:从实践sql语句优化开始
版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/a724888/article/details/79394168 本文不堆叠网上海量的sql优化技巧或 ...
- MYSQL查询语句优化
mysql的性能优化包罗甚广: 索引优化,查询优化,查询缓存,服务器设置优化,操作系统和硬件优化,应用层面优化(web服务器,缓存)等等.这里的记录的优化技巧更适用于开发人员,都是从网络上收集和自己整 ...
- mysql sql语句大全(转载)
1.说明:创建数据库 CREATE DATABASE database-name 2.说明:删除数据库 drop database dbname 3.说明:备份sql server --- 创建 ...
- MYSQL SQL语句技巧初探(一)
MYSQL SQL语句技巧初探(一) 本文是我最近了解到的sql某些方法()组合实现一些功能的总结以后还会更新: rand与rand(n)实现提取随机行及order by原理的探讨. Bit_and, ...
- MySQL常用SQL语句优化
推荐阅读这篇博文,索引说的非常详细到位:http://blog.linezing.com/?p=798#nav-3-2 在数据库日常维护中,最常做的事情就是SQL语句优化,因为这个才是影响性能的最主要 ...
- MySQL基础操作&&常用的SQL技巧&&SQL语句优化
基础操作 一:MySQL基础操作 1:MySQL表复制 复制表结构 + 复制表数据 create table t3 like t ...
随机推荐
- shader 编程入门(一)
本系列文章由@浅墨_毛星云 出品,转载请注明出处. 文章链接:http://blog.csdn.net/poem_qianmo/article/details/40723789 作者:毛星云(浅墨) ...
- go context包的WithTimeout和WithCancel的使用
1.WaitGroup 它是一种控制并发的方式,它的这种方式是控制多个goroutine同时完成. func main() { var wg sync.WaitGroup wg.Add(2) go f ...
- Python中集合类型(set)学习小结
set 是一个无序的元素集合,支持并.交.差及对称差等数学运算, 但由于 set 不记录元素位置,因此不支持索引.分片等类序列的操作. 初始化 复制代码代码如下: s0 = set()d0 = {}s ...
- 【Python】分享使用的插件文件链接(实时更新)
链接:https://pan.baidu.com/s/1o7AgHtw Python工具实时更新.
- 如何 “解决” WPF中空域问题(Airspace issuse)
空域问题是由于Winform与WPF在底层渲染机制上有所区别而导致的.多数情况下,开发者为了实现不规则的窗体并承载Winform控件时,遇到此类问题.当WPF窗体设置为允许透明(也就是AllowsTr ...
- JQ 时间插件
<script type="text/javascript" charset="utf-8" src="__PUBLIC__/ueditor/u ...
- cobbler setting dnsmasq
Currently cobbler can help generate a ISC DHCP configuration (package name: dhcpd) it can also alter ...
- SQLServer中Partition By
http://www.cnblogs.com/vincentonline/p/4999225.html 今天群里看到一个问题,在这里概述下:查询出不同分类下的最新记录.一看这不是很简单的么,要分类那就 ...
- Error in registration. Error: Error Domain=NSCocoaErrorDomain Code=3000 "未找到应用程序的“aps-environment”的授
本文转载至 http://blog.csdn.net/woaifen3344/article/details/41311023 Code3000极光推送erroryour certificate n ...
- IOS开发报错之Undefined symbols for architecture armv6
本文转载至 http://blog.csdn.net/sanpintian/article/details/7575434 今天在项目中引入SVSegmentedControl.h/.my以及SVS ...