mysql慢查询优化之从理论和实践说明limit的优点

 更新时间:2019年04月04日 09:24:12   作者:stpeace  
今天小编就为大家分享一篇关于mysql慢查询优化之从理论和实践说明limit的优点,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧

很多时候, 我们预期查询的结果最多是1条记录数据, 那么这个时候, 最好用上limit 1,  当查到这条数据后, mysql会立即终止继续查询, 不进行更多的无用查询, 从而提升了效率。

我们来实际测试一下, 在一个拥有10万的mysql表中, 查找lily的分数(假设系统中只有1个lily, 而我们预期也只需要这条数据)。为了显示出时间的差别, 我并不对表的name字段建索引。

先看看表结构:

mysql> show create table tb_province;
+-------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table    | Create Table                                                                                                                                                                                                                                                                                                                           |
+-------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| tb_province | CREATE TABLE `tb_province` (
 `id` bigint(10) unsigned NOT NULL AUTO_INCREMENT,
 `name` varchar(32) NOT NULL,
 `score` int(10) unsigned DEFAULT '0',
 `x` int(10) unsigned DEFAULT '0',
 `x1` int(10) unsigned DEFAULT '0',
 `x2` int(10) unsigned DEFAULT '0',
 `x3` int(10) unsigned DEFAULT '0',
 `x4` int(10) unsigned DEFAULT '0',
 `x5` int(10) unsigned DEFAULT '0',
 `x6` int(10) unsigned DEFAULT '0',
 `x7` int(10) unsigned DEFAULT '0',
 `x8` int(10) unsigned DEFAULT '0',
 `x9` int(10) unsigned DEFAULT '0',
 `x10` int(10) unsigned DEFAULT '0',
 PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=124178 DEFAULT CHARSET=latin1 |
+-------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

我们打开set profiling=1;的开关,执行mysql语句来对比:

mysql> select score from tb_province where name='lily';
+-------+
| score |
+-------+
|  100 |
+-------+
1 row in set (0.03 sec)

mysql> select score from tb_province where name='lily';
+-------+
| score |
+-------+
|  100 |
+-------+
1 row in set (0.03 sec)

mysql> select score from tb_province where name='lily';
+-------+
| score |
+-------+
|  100 |
+-------+
1 row in set (0.04 sec)

mysql> select score from tb_province where name='lily';
+-------+
| score |
+-------+
|  100 |
+-------+
1 row in set (0.02 sec)

mysql> select score from tb_province where name='lily';
+-------+
| score |
+-------+
|  100 |
+-------+
1 row in set (0.03 sec)

mysql> select score from tb_province where name='lily' limit 1;
+-------+
| score |
+-------+
|  100 |
+-------+
1 row in set (0.00 sec)

mysql> select score from tb_province where name='lily' limit 1;
+-------+
| score |
+-------+
|  100 |
+-------+
1 row in set (0.00 sec)

mysql> select score from tb_province where name='lily' limit 1;
+-------+
| score |
+-------+
|  100 |
+-------+
1 row in set (0.00 sec)

mysql> select score from tb_province where name='lily' limit 1;
+-------+
| score |
+-------+
|  100 |
+-------+
1 row in set (0.01 sec)

mysql> select score from tb_province where name='lily' limit 1;
+-------+
| score |
+-------+
|  100 |
+-------+
1 row in set (0.00 sec)

可见,我们针对是否采用limit 1进行了5次对比测试, 来看看结果吧:

mysql> show profiles;
+----------+------------+---------------------------------------------------------+
| Query_ID | Duration  | Query                          |
+----------+------------+---------------------------------------------------------+
|    5 | 0.02686000 | select score from tb_province where name='lily'     |
|    6 | 0.02649050 | select score from tb_province where name='lily'     |
|    7 | 0.03413500 | select score from tb_province where name='lily'     |
|    8 | 0.02601350 | select score from tb_province where name='lily'     |
|    9 | 0.02785775 | select score from tb_province where name='lily'     |
|    10 | 0.00042300 | select score from tb_province where name='lily' limit 1 |
|    11 | 0.00043250 | select score from tb_province where name='lily' limit 1 |
|    12 | 0.00044350 | select score from tb_province where name='lily' limit 1 |
|    13 | 0.00053200 | select score from tb_province where name='lily' limit 1 |
|    14 | 0.00043250 | select score from tb_province where name='lily' limit 1 |
+----------+------------+---------------------------------------------------------+
14 rows in set, 1 warning (0.00 sec)

可见,采用limit 1后, mysql语句的效率确实提升很多。 当表更大时, 效率提升会更加明显。 

我们已经从理论和实践的脚本都说明了limit的优点, 所以, 建议是:在可用limit的时候要用limit (当然, 如果结果是多个,肯定不能limit 1啊)

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对脚本之家的支持。如果你想了解更多相关内容请查看下面相关链接

相关文章

  • 解决hibernate+mysql写入数据库乱码

    解决hibernate+mysql写入数据库乱码

    初次没习hibernate,其中遇到问题在网上找的答案与大家共同分享!
    2009-07-07
  • 详解用SELECT命令在MySQL执行查询操作的教程

    详解用SELECT命令在MySQL执行查询操作的教程

    这篇文章主要介绍了详解用SELECT命令在MySQL执行查询操作的教程,本文中还给出了基于PHP脚本的操作演示,需要的朋友可以参考下
    2015-05-05
  • 一文教你快速学会使用DDL对数据库和表的操作

    一文教你快速学会使用DDL对数据库和表的操作

    SQL是一种操作关系型数据库的结构化查询语言,今天这篇文章将详细讲述数据定义语言DDL对数据库和表的相关操作,有感兴趣的同学跟着小编一起来学习吧
    2023-07-07
  • MySQL合并查询结果的实现

    MySQL合并查询结果的实现

    本文主要介绍了MySQL合并查询结果的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-03-03
  • 使用MYSQL TIMESTAMP字段进行时间加减运算问题

    使用MYSQL TIMESTAMP字段进行时间加减运算问题

    这篇文章主要介绍了使用MYSQL TIMESTAMP字段进行时间加减运算问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-02-02
  • mysql的存储过程、游标 、事务实例详解

    mysql的存储过程、游标 、事务实例详解

    这篇文章主要介绍了mysql的存储过程、游标 、事务实例详解的相关资料,这里举实例说明MySQL 存储过程与游标和事务,需要的朋友可以参考下
    2017-08-08
  • Mysql连接join查询原理知识点

    Mysql连接join查询原理知识点

    在本文里我们给大家整理了一篇关于Mysql连接join查询原理知识点文章,对此感兴趣的朋友们可以学习下。
    2019-02-02
  • MySQL如何快速创建800w条测试数据表

    MySQL如何快速创建800w条测试数据表

    这篇文章主要介绍了MySQL如何快速创建800w条测试数据表,下面文章围绕MySQL创建测试数据表的相关资料展开详细内容,具有一的的参考价值,需要的小伙伴可以参考一下
    2022-03-03
  • MySQL 存储过程的基本用法介绍

    MySQL 存储过程的基本用法介绍

    我们大家都知道MySQL 存储过程是从 MySQL 5.0 开始逐渐增加新的功能。存储过程在实际应用中也是优点大于缺点。不过最主要的还是执行效率和SQL 代码封装。特别是 SQL 代码封装功能,如果没有存储过程。
    2010-12-12
  • MySQL索引操作命令详解

    MySQL索引操作命令详解

    这篇文章主要介绍了MySQL索引操作命令详解,需要的朋友可以参考下
    2017-01-01

最新评论