没有不值得去解决的问题,也没有不值得去学习的技术!

Why the Same count(*) SQL Query Takes Drastically Different Times in Navicat and Laravel 9

The execution time in Laravel 9 is 60317.74ms, which is equivalent to 60 seconds

1. Reference: Analysis and optimization of timeout when querying total table records in MySQL 8.0. The execution time in Navicat is 3.075 seconds. See Figure 1.

The execution time in Navicat is 3.075 seconds
Figure 1
SQL
SELECT
	count(*) AS AGGREGATE 
FROM
	`tables` 
WHERE
	`id` > 0

2. The execution time in Laravel 9 is 60317.74ms, which is equivalent to 60 seconds. See Figure 2.

The execution time in Laravel 9 is 60317.74ms, which is equivalent to 60 seconds
Figure 2

3. I verified another count(*) SQL query, and its execution time in Laravel 9 is 2623.17ms, which is equivalent to 2.6 seconds. Although it returns the same query results as the previous query, its execution time is only 5% of the previous SQL. See Figure 3.

Verified another count(*) SQL query, and its execution time in Laravel 9 is 2623.17ms, which is equivalent to 2.6 seconds. Although it returns the same query results as the previous query, its execution time is only 5% of the previous SQL
Figure 3
SQL
select
  count(*) as aggregate
from
  `tables`
where
  `shipping_at_gmt` >= '2024-01-01 16:00:00'
  and `shipping_at_gmt` <= '2024-05-06 15:59:59'
 

4. Executing the raw SQL via the Tinker command (REPL) inside the container also takes about 3 seconds. See Figure 4.

Executing the raw SQL via the Tinker command (REPL) inside the container also takes about 3 seconds
Figure 4
PHP
$count = DB::select('SELECT count(*) AS AGGREGATE FROM `tables` WHERE `id` > 0');

5. Executing the raw SQL in the program takes about 3 seconds. See Figure 5.

Executing the raw SQL in the program takes about 3 seconds
Figure 5
PHP
$count = \Illuminate\Support\Facades\DB::select('SELECT count(*) AS AGGREGATE FROM `tables` WHERE `id` > 0');
print_r($count);
exit;

6. I finally confirmed that in vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php, the runPaginationCountQuery method took 60 seconds to execute. Since this is a framework-level implementation, it is not easy to fix for now. I decided to use where `shipping_type` in (1, 2). The SQL execution time dropped to about 3 seconds, as expected. Below is the printed log to confirm the execution time of the runPaginationCountQuery method. The time between runPaginationCountQuery2 and $results2 is the execution time of the following code. See Figure 6 and Figure 7.

Finally confirmed that in vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php, the runPaginationCountQuery method took 60 seconds to execute. Since this is a framework-level implementation, it is not easy to fix for now.
Figure 6

 

Below is the printed log to confirm the execution time of the runPaginationCountQuery method. The time between runPaginationCountQuery2 and $results2 is the execution time of the following code.
Figure 7

 

PHP
return $this->cloneWithout($without)
			->cloneWithoutBindings($this->unions ? ['order'] : ['select', 'order'])
			->setAggregate('count', $this->withoutSelectAliases($columns))
			->get()->all();

Plaintext
[2024-05-06 10:14:07] local.INFO: $total1 ["2024-05-06 10:14:07"] 
[2024-05-06 10:14:07] local.INFO: $results1 ["2024-05-06 10:14:07"] 
[2024-05-06 10:14:07] local.INFO: runPaginationCountQuery2 ["2024-05-06 10:14:07"] 
[2024-05-06 10:15:38] local.INFO: $results2 ["2024-05-06 10:15:38"] 
[2024-05-06 10:15:38] local.INFO: $total2 ["2024-05-06 10:15:38"]

关于作者 · 持续学习与技术实践

我是一名拥有 15 年以上开发经验的技术从业者,自 2013 年起持续运营个人技术博客,记录工作与个人项目中遇到的真实问题,以及解决问题过程中的探索、实践和思考。

除了技术写作,我也在持续开发和维护自己的独立项目,探索新技术和新工具在实际场景中的应用。我始终相信,没有不值得去解决的问题,也没有不值得去学习的技术。

如果这篇文章对你有所帮助,欢迎浏览博客中的其他文章,也欢迎交流技术经验与想法。

关于我  ·  GitHub  ·  邮件联系