Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auth::user()->username and similar are very slow

I am having a werid issue. My Laravel 5 application is very slow, it take 1-3 seconds to completely load, that is something that is not tolerable.

After hours of debugging, I found out that the issue is Auth::user(), more specifically when you try to access something like Auth::user()->username.

What I noticed: Auth::user()->id is blazing fast, while Auth::user()->username takes 1-3 seconds. It does also not to appear to have something todo with the mySQL server, as the exact same queries are being executed, no matter if I use ->id or ->username.

It is not only that slow when using ->username but it appears to be that slow for alsmost everything besides ->id, also when accessing roles like Auth::user()->roles.

In case it matters, I am using Entrust for the permission/role management.

User Model:

<?php namespace App\Models;

use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Zizaco\Entrust\Traits\EntrustUserTrait;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;

class User extends Model implements AuthenticatableContract, CanResetPasswordContract
{
    use Authenticatable, CanResetPassword, EntrustUserTrait, SoftDeletes;

    protected $table = 'users';
    protected $guarded = ['id'];
    protected $fillable = ['username', 'email', 'activation_code'];
    protected $hidden = ['password', 'remember_token'];
    protected $dates = ['deleted_at'];

    public function personalData()
    {
        return $this->hasOne(UserPersonalData::class);
    }

    public function banned()
    {
        return $this->hasOne(BanUser::class);
    }

}

Even if I remove the Entrust Service Provider and therefore also the EntrustUserTrait, it is still as slow as before.

SHOW CREATE TABLE users as requested

CREATE TABLE `users` (
 `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
 `username` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
 `email` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
 `password` varchar(60) COLLATE utf8_unicode_ci NOT NULL,
 `activated` tinyint(1) NOT NULL DEFAULT '0',
 `activation_code` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
 `remember_token` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
 `created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
 `updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
 `deleted_at` timestamp NULL DEFAULT NULL,
 PRIMARY KEY (`id`),
 UNIQUE KEY `users_username_unique` (`username`),
 UNIQUE KEY `users_email_unique` (`email`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci

Any idea why this is happening?

Thanks!

like image 473
TheNiceGuy Avatar asked Jan 13 '16 17:01

TheNiceGuy


2 Answers

Use xdebug to generate a profile of the execution. Then download that file and open it in Wincachegrind or Kcachegrind (depending on what OS you're running on your desktop)

In those programs you can drill down and see what's taking the extra time by double-clicking and looking at the columns to see how much time something has taken.

like image 91
sa289 Avatar answered Nov 07 '22 12:11

sa289


Use method instead of Facade.

Use:
auth()->user()->username. 

Avoid use of:
Auth::user()->username.
like image 38
Afraz Ahmad Avatar answered Nov 07 '22 14:11

Afraz Ahmad