20

Note: Now that I know where the issue comes from, I modified the question. It now contains only the information needed.

I'm new to the Laravel PHP framework.

I have a very small app working on my computer. It is connected to a MySQL database and has a User model. I use the Auth class to log in and out.

Everything works fine, but when I am logged in, loading a page takes about a second which is very slow. When I'm not logged in, it's a matter of milliseconds.

By using the built-in profiler, I realized two problems. First, like I said, loading a page takes a bit more than 1000 milliseconds. Second, the framework makes one SQL every time I load a page when I'm logged in. The query searches a user with a certain id (my id). I guess it is there to get information about the logged in user. But isn't there supposed to be some sort of cache. Will this be a problem if my website will have to manage many requests per seconds.

I realized that using Auth::check() in the view is what is causing the issue. I have around 4 Auth::check() is my Blade view. When I have none, it goes fast. If I have one, it is slow. Then, no matter how many I have, it doesn't get much slower. It's like if the Auth class' initialization takes too much time or something like that. I guess it explains why it only happens when I'm logged in.

I dived into Laravel's code and I found out that when Auth::check() is called for the first time, the Auth class needs to "activate" my Session by retrieving the user's info from the database. That explains the query being executed every page request. But since the profiler says that the query doesn't even take a millisecond to execute, I still don't know why it slows down the app.

New information: Even when I'm not sending a query to the database, the simple act of connecting to it takes almost a second. This is the reason it is slow. I think I'm getting really close to solve the issue.

Any idea so far?

Thanks in advance.

Notes

  • The fact that Auth::check() is in the view doesn't change anything.
  • Using another method like Auth::guest() doesn't solve the issue.
  • New: Connecting to the database is what is slow.
6
  • That's quite interesting. I'm also new to Laravel, and have completed my first complete intranet package for my clients. It uses MySQL as well, but seems quite quick. The speed difference isn't that much on my system. Have you tried using Lavarel's cache yet? Oct 11, 2012 at 4:33
  • No, I don't use any cache. I expect Auth to do it by itself, no? Both the Cache and Session classes are configured to use the file system. If I have to cache my Auth variables manually, how would I do that? Oct 11, 2012 at 4:35
  • Well, it wouldn't be Auth that's taking most of the time. I'm more concerned about caching the results of the database. As it stands now, Auth is quite good, and has no speed issues. Oct 11, 2012 at 4:38
  • And the caching tool is not automatic, so far as I know. Haven't had the need to use it yet. Oct 11, 2012 at 4:41
  • The database queries come from Auth. I don't use anything else. My application is still very simple. Oct 11, 2012 at 4:42

2 Answers 2

40

I finally found a way to fix this.

When reading some posts on many forums about XAMPP, MySQL, and PHP, and I read somewhere that it is preferable to use 127.0.0.1 because locahost needs an extra DNS lookup.

In the database configuration file, I simply changed locahost for 127.0.0.1.

Now everything is fast.

I find this really strange. Using locahost in the configuration file used to make the database connection take more than a second!

8
  • I'd like to know who else may be having the same problem. See, like I said in my last comment, my queries average less then a second, and I use a host-name lookup. In fact, I have a domain manager with plenty local domains. Yet, I haven't tried going back to IP... Oct 12, 2012 at 17:00
  • 7
    If the system is set up properly, there should be no dns lookup for localhost, as it should be in the hosts file on your system. I'd check that - it sounds like the result of an unusual dns setup Oct 12, 2012 at 21:55
  • Thanks! I tried that on Windows using Z-WAMP (application/config/database.php) and there's a huge noticeable difference!
    – jpmonette
    Oct 28, 2012 at 17:58
  • @marc-francois I'm curious about that problem. I assume you were experiencing this on a windows system?
    – dualed
    Jan 18, 2013 at 8:41
  • 2
    Wow! Amazing. There is a huge difference in the project I'm developing after changing "localhost" to "127.0.0.1". When the database config was using "localhost" the time to load the page was 1,092.40ms. Now, after the change, is 58.36ms!
    – Fuhrmann
    Feb 18, 2013 at 4:46
2

I do not agree with Hammo's example. Having any user information other than their ID within the session is a security risk, which is why most frameworks take this route. Is there anything else being run when the user is logged in, apart from the query for their record? It's definitely not that that's slowing your application down.

13
  • Well, except two or three @if (Auth::check()) @include('some.stuff') @endif, I don't know. Oct 11, 2012 at 4:40
  • Are you doing the Auth check within the blade view? Is the include maybe slowing it down?
    – Oddman
    Oct 11, 2012 at 4:41
  • Yes, the Auth check is done within the Blade view. The @include is for a simple menu and some links that I want to show or hide. Oct 11, 2012 at 4:44
  • 1
    I can see where @Oddman is coming from, though I don't think it applies in this instance. None of that would take anything near 1000 milliseconds to render, though I can't confirm that for sure. Perhaps testing your speed by doing a calculation based on LARAVEL_START, and inserting a call to that just after every relevant call in your view should show you what's wrong. Oct 11, 2012 at 4:57
  • Thanks for the idea Michael. Now, I found more information about the issue. I edited the question with this information. Oct 11, 2012 at 5:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.