title: Laravel application very slow when connecting to the database
body: <p><em>Note: Now that I know where the issue comes from, I modified the question. It now contains only the information needed.</em></p>
  
  <p>I'm new to the Laravel PHP framework.</p>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>I realized that using <code>Auth::check()</code> in the view is what is causing the issue. I have around 4 <code>Auth::check()</code> 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.</p>
  
  <p>I dived into Laravel's code and I found out that when <code>Auth::check()</code> 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.</p>
  
  <p><strong>New information:</strong> 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.</p>
  
  <p>Any idea so far?</p>
  
  <p>Thanks in advance.</p>
  
  <h2>Notes</h2>
  
  <ul>
  <li>The fact that <code>Auth::check()</code> is in the view doesn't change anything.</li>
  <li>Using another method like <code>Auth::guest()</code> doesn't solve the issue.</li>
  <li><strong>New:</strong> Connecting to the database is what is slow.</li>
  </ul>