Laravel 应用程序连接数据库时速度很慢
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12832207/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel application very slow when connecting to the database
提问by Marc-Fran?ois
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.
我是 Laravel PHP 框架的新手。
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.
我有一个非常小的应用程序在我的电脑上运行。它连接到一个 MySQL 数据库并且有一个 User 模型。我使用 Auth 类来登录和注销。
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.
通过使用内置分析器,我意识到了两个问题。首先,就像我说的,加载一个页面需要 1000 多毫秒。其次,每次我登录时加载页面时,框架都会生成一个 SQL。该查询搜索具有特定 id(我的 id)的用户。我想它是为了获取有关登录用户的信息。但不应该有某种缓存。如果我的网站每秒必须管理许多请求,这会成为问题吗?
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.
我意识到Auth::check()
在视图中使用是导致问题的原因。我有大约 4 个Auth::check()
是我的 Blade 视图。当我没有时,它会很快。如果我有一个,它很慢。然后,无论我有多少,它都不会变慢。就像 Auth 类的初始化需要太多时间或类似的东西。我想它解释了为什么它只在我登录时发生。
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.
我深入研究了 Laravel 的代码,发现当Auth::check()
第一次被调用时,Auth 类需要通过从数据库中检索用户信息来“激活”我的会话。这解释了每个页面请求正在执行的查询。但是由于分析器说查询甚至不需要一毫秒来执行,我仍然不知道为什么它会减慢应用程序的速度。
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.
Auth::check()
视图中的事实不会改变任何东西。- 使用另一种方法
Auth::guest()
不能解决问题。 - 新:连接到数据库很慢。
回答by Marc-Fran?ois
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.1because locahostneeds an extra DNS lookup.
在许多论坛上阅读有关 XAMPP、MySQL 和 PHP 的一些帖子时,我在某处读到最好使用127.0.0.1,因为locahost需要额外的 DNS 查找。
In the database configuration file, I simply changed locahostfor 127.0.0.1.
在数据库配置文件中,我只是将locahost更改为127.0.0.1。
Now everything is fast.
现在一切都很快。
I find this really strange. Using locahostin the configuration file used to make the database connection take more than a second!
我觉得这真的很奇怪。在配置文件中使用locahost用于使数据库连接花费一秒钟以上!
回答by Oddman
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.
我不同意 Hammo 的例子。在会话中拥有除 ID 之外的任何用户信息都是一种安全风险,这就是大多数框架采用这种方式的原因。除了查询他们的记录外,用户登录时是否还有其他运行?绝对不是这会减慢您的应用程序的速度。