Reputation: 3
I am experiencing an extremely slow query when calling Auth::user() in Laravel. The query takes around 2.7 seconds, which significantly slows down my application.
I logged the database queries using DB::listen() in AppServiceProvider.php:
DB::listen(function ($query) {
Log::info("Query: {$query->sql} | Bindings: " . implode(', ', $query->bindings) . " | Time: {$query->time}ms ");
});
Here is the output:
[2025-02-27 15:54:26] local.INFO: Query: select * from `users` where `id` = ? limit 1 | Bindings: 1 | Time: 2695.51ms
Tested query directly in MySQL the result is instantaneous.
This is the output of SHOW CREATE TABLE users:
CREATE TABLE `users` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`email` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`email_verified_at` timestamp NULL DEFAULT NULL,
`email_2` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`email_3` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`password` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`remember_token` varchar(100) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`is_active` tinyint(1) NOT NULL DEFAULT 1,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `users_email_unique` (`email`)
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
Those are the indexes in the table users:
users 0 PRIMARY 1 id A 4 BTREE
users 0 users_email_unique 1 email A 4 BTREE
Thanks
Upvotes: 0
Views: 63