首页 > 其他分享 >比较下 count(*) 和 limit 1

比较下 count(*) 和 limit 1

时间:2025-01-14 17:32:06浏览次数:1  
标签:count id records limit mysql sec 比较 SELECT subject

比较下 count(*) 和 limit 1

场景:在删除科目ID的时候,想检查下科目ID是否有产生记录,比较下 count(*) 和 limit 1 的执行效率;

数据库版本:

mysql> SELECT @@VERSION;
+-----------+
| @@VERSION |
+-----------+
| 8.0.35    |
+-----------+
1 row in set (0.00 sec)

mysql>

创建测试表

CREATE TABLE `records` (
  `id` int unsigned NOT NULL AUTO_INCREMENT COMMENT '主键',
  `subject_id` int NOT NULL COMMENT '科目ID',
  `amount` decimal(15,2) DEFAULT NULL COMMENT '金额',
  PRIMARY KEY (`id`) USING BTREE,
  KEY `record_subject_id_index` (`subject_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci

定义生成数据的存储过程:

mysql> DELIMITER $$
mysql> CREATE PROCEDURE records_procedure(IN total INT)
    -> BEGIN
    ->  DECLARE j INT DEFAULT 1;
    ->   DECLARE subject_id INT DEFAULT 1;
    ->   DECLARE amount INT DEFAULT 1;
    ->  SET @i = (SELECT IFNULL(MAX(id), 0) FROM records);
    ->  SET AUTOCOMMIT = 0;
    ->  WHILE (j <= total) DO
    ->          SET @val = @i + j;
    ->     SELECT RAND()*100 INTO subject_id;
    ->     SELECT RAND()*10000 INTO amount;
    ->          INSERT INTO records(`id`, `subject_id`, `amount`) VALUES(@val, subject_id, amount);
    ->          SET j=j+1;
    ->  END WHILE;
    ->  SET AUTOCOMMIT = 1;
    -> END$$
Query OK, 0 rows affected (0.01 sec)

mysql> DELIMITER ;
mysql>

生成 100 万条数据:

mysql> CALL records_procedure(1000000);
Query OK, 0 rows affected (31.63 sec)

mysql>
mysql> SELECT COUNT(*) FROM records;
+----------+
| COUNT(*) |
+----------+
|  1000000 |
+----------+
1 row in set (0.03 sec)

mysql>

查出 subject_id 最多的前 10 个:

mysql> SELECT subject_id, COUNT(subject_id) AS si FROM records GROUP BY subject_id ORDER BY si DESC LIMIT 10;
+------------+-------+
| subject_id | si    |
+------------+-------+
|         63 | 10323 |
|         96 | 10249 |
|         67 | 10235 |
|         73 | 10235 |
|         13 | 10212 |
|         97 | 10167 |
|         34 | 10166 |
|         11 | 10145 |
|         88 | 10138 |
|         52 | 10137 |
+------------+-------+
10 rows in set (0.13 sec)

mysql>

统计下 count 和 limit 1 的执行时间:

mysql> set profiling=1;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> SELECT COUNT(*) FROM records;
+----------+
| COUNT(*) |
+----------+
|  1000000 |
+----------+
1 row in set (0.03 sec)

mysql> SELECT COUNT(*) FROM records WHERE subject_id = 63;
+----------+
| COUNT(*) |
+----------+
|    10323 |
+----------+
1 row in set (0.00 sec)

mysql> SELECT * FROM records WHERE subject_id = 63 LIMIT 1;
+----+------------+---------+
| id | subject_id | amount  |
+----+------------+---------+
| 54 |         63 | 7113.00 |
+----+------------+---------+
1 row in set (0.00 sec)

mysql> set profiling=0;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql>

看下 SQL 的执行时间:

mysql> show profiles;
+----------+------------+-----------------------------------------------------+
| Query_ID | Duration   | Query                                               |
+----------+------------+-----------------------------------------------------+
|        1 | 0.02733025 | SELECT COUNT(*) FROM records                        |
|        2 | 0.00225250 | SELECT COUNT(*) FROM records WHERE subject_id = 63  |
|        3 | 0.00065800 | SELECT * FROM records WHERE subject_id = 63 LIMIT 1 |
+----------+------------+-----------------------------------------------------+
3 rows in set, 1 warning (0.00 sec)

mysql>

再插入 100 万条数据:

mysql> CALL records_procedure(1000000);
Query OK, 0 rows affected (31.31 sec)

mysql>

统计下SQL执行时间:

mysql> set profiling=1;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> SELECT COUNT(*) FROM records;
+----------+
| COUNT(*) |
+----------+
|  2000000 |
+----------+
1 row in set (0.05 sec)

mysql> SELECT subject_id, COUNT(subject_id) AS si FROM records GROUP BY subject_id ORDER BY si DESC LIMIT 10;
+------------+-------+
| subject_id | si    |
+------------+-------+
|         52 | 20382 |
|         97 | 20332 |
|         96 | 20299 |
|         67 | 20277 |
|         39 | 20264 |
|         30 | 20226 |
|         25 | 20224 |
|         61 | 20223 |
|         95 | 20193 |
|         31 | 20190 |
+------------+-------+
10 rows in set (0.30 sec)

mysql> SELECT COUNT(*) FROM records WHERE subject_id = 52;
+----------+
| COUNT(*) |
+----------+
|    20382 |
+----------+
1 row in set (0.00 sec)

mysql> SELECT * FROM records WHERE subject_id = 52 LIMIT 1;
+----+------------+---------+
| id | subject_id | amount  |
+----+------------+---------+
|  5 |         52 | 5956.00 |
+----+------------+---------+
1 row in set (0.00 sec)

mysql> set profiling=0;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> 

再看下 SQL 的执行时间:

mysql> show profiles;
+----------+------------+-------------------------------------------------------------------------------------------------------+
| Query_ID | Duration   | Query                                                                                                 |
+----------+------------+-------------------------------------------------------------------------------------------------------+
|        1 | 0.02733025 | SELECT COUNT(*) FROM records                                                                          |
|        2 | 0.00225250 | SELECT COUNT(*) FROM records WHERE subject_id = 63                                                    |
|        3 | 0.00065800 | SELECT * FROM records WHERE subject_id = 63 LIMIT 1                                                   |
|        4 | 0.05361075 | SELECT COUNT(*) FROM records                                                                          |
|        5 | 0.30070025 | SELECT subject_id, COUNT(subject_id) AS si FROM records GROUP BY subject_id ORDER BY si DESC LIMIT 10 |
|        6 | 0.00319900 | SELECT COUNT(*) FROM records WHERE subject_id = 52                                                    |
|        7 | 0.00067850 | SELECT * FROM records WHERE subject_id = 52 LIMIT 1                                                   |
+----------+------------+-------------------------------------------------------------------------------------------------------+
7 rows in set, 1 warning (0.00 sec)

mysql>

标签:count,id,records,limit,mysql,sec,比较,SELECT,subject
From: https://www.cnblogs.com/zhpj/p/18671268/compare-the-count-and-limit-1-ltkow

相关文章

  • mysql使用count()执行select报错:ERROR 1140 (42000) In aggregated query without GRO
    1原因mysql的sql_mode默认开启了only_full_group_by模式2解决办法2.1命令解决(临时生效)查看sql_modeshowvariableslike'%sql_mode';showsessionvariableslike'%sql_mode';showglobalvariableslike'%sql_mode';修改sql_modesetglobalsql_......
  • R语言caret包的resamples函数比较在同一数据集上多个机器学习模型的比较结果实战、sum
    R语言caret包的resamples函数比较在同一数据集上多个机器学习模型的比较结果实战、使用summary函数比较模型的汇总信息、使用lattice包的bwplot函数使用箱图对比多个模型在多个指标上的性能差异目录R语言使用caret包的resamples函数比较在同一数据集上多个机器学习模型的比......
  • HDLBits-Verilog:Counter 1000
    从1000Hz时钟中,得出一个1Hz信号,称为 OneHertz,该信号可用于驱动一组小时/分钟/秒计数器的启用信号,以创建数字挂钟。由于我们希望clock每秒计数一次,因此 OneHertz 信号必须每秒正好置位一个周期。使用modulo-10(BCD)计数器和尽可能少的其他门构建分频器。此外,还输出......
  • Java算法 数据结构 栈 队列 优先队列 比较器
    目录栈Stack性质构造方法代码示例队列Queue性质构造方法代码示例优先队列PriorityQueue性质构造方法代码示例比较器1.Comparator接口的方法2.常见的内置比较器1.自然排序比较器(naturalOrder())2.逆序排序比较器(reverseOrder())3.nullsFirst()......
  • 深入探讨聚合函数(COUNT, SUM, AVG, MAX, MIN):分析和总结数据的新视野
    title:深入探讨聚合函数(COUNT,SUM,AVG,MAX,MIN):分析和总结数据的新视野date:2025/1/13updated:2025/1/13author:cmdragonexcerpt:在数据分析和数据库管理领域,聚合函数(AggregateFunctions)是获取数据总结和统计信息的关键工具。聚合函数如COUNT、SUM、AVG、M......
  • 深入探讨聚合函数(COUNT, SUM, AVG, MAX, MIN):分析和总结数据的新视野
    title:深入探讨聚合函数(COUNT,SUM,AVG,MAX,MIN):分析和总结数据的新视野date:2025/1/13updated:2025/1/13author:cmdragonexcerpt:在数据分析和数据库管理领域,聚合函数(AggregateFunctions)是获取数据总结和统计信息的关键工具。聚合函数如COUNT、SUM、AVG、M......
  • svn检出has encountered a problem cannot checkout
    报错信息:"svn检出hasencounteredaproblemCannotcheckout"表示Subversion(SVN)在尝试检出(即下载)版本控制仓库的时候遇到了问题。解释:这个错误通常意味着SVN客户端无法完成检出操作,可能的原因有多种,包括网络问题、权限问题、URL错误、服务器不可达或者客户端与服务器版本不......
  • 【C++】string的关系运算与比较分析
    博客主页:[小ᶻ☡꙳ᵃⁱᵍᶜ꙳]本文专栏:C++文章目录......
  • Atcoder ABC387F Count Arrays 题解 [ 绿 ] [ 基环树 ] [ 树形 dp ] [ 前缀和优化 ]
    CountArrays:一眼秒的计数题。思路显然,把小于等于的条件化为大的向小的连单向边,每个数的入度都是\(1\),就会形成一个基环树森林。那么考虑这个环上能填什么数。因为所有数都小于等于他后面的数,所以所有数都只能相等。这就启发我们在基环树上缩点之后再进行计数。那么当缩完点......
  • cursor试用出现:Too many free trial accounts used on this machine 的解决方法
    文章精选推荐1JetBrainsAiassistant编程工具让你的工作效率翻倍2ExtraIcons:JetBrainsIDE的图标增强神器3IDEA插件推荐-SequenceDiagram,自动生成时序图4BashSupportPro这个ides插件主要是用来干嘛的?5IDEA必装的插件:SpringBootHelper的使用与功能特点6A......