简体   繁体   English

Eloquent 使用远程时非常慢 MySQL

[英]Eloquent Very slow when using remote MySQL

I recently had to transfer my database to a remote server.我最近不得不将我的数据库转移到远程服务器。

I used Postman to connect to PHP running on localhost to make the same requests.我使用 Postman 连接到在 localhost 上运行的 PHP 以发出相同的请求。 Here are the results,这是结果,

MySQL on Localhost and Eloquent: ~30ms Localhost 和 Eloquent 上的 MySQL:~30ms

MySQL on Remote Server and Eloquent: ~2.7 seconds远程服务器上的 MySQL 和 Eloquent:~2.7 秒

MySQL on Localhost and PHP: ~10ms Localhost 和 PHP 上的 MySQL:~10ms

MySQL on Remote Server and PHP: ~850ms远程服务器上的 MySQL 和 PHP:~850ms

The average ping from my computer to the the remote server ip is about 150ms.从我的计算机到远程服务器 ip 的平均 ping 时间约为 150 毫秒。

Here is the PHP script I used,这是我使用的 PHP 脚本,

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$sql = "select * from `online` where `online`.`id` = 1 limit 1";
$result = $conn->query($sql);

var_dump($result);

For eloquent, I just used Online::find(1) .对于 eloquent,我只使用了Online::find(1)

I still haven't tried running eloquent on a server, but is this normal?我还没有尝试在服务器上运行 eloquent,但这正常吗? Should I steer away from using eloquent if I am going to be using remote mysql databases on different servers?如果我要在不同的服务器上使用远程 mysql 数据库,我应该避免使用 eloquent 吗?

EDIT:编辑:

I was changing some values, and when I removed the charset and collation value from eloquent config, the response time improved to 1.7s which is 1s faster.我正在更改一些值,当我从 eloquent 配置中删除字符集和排序规则值时,响应时间提高到 1.7 秒,快了 1 秒。

'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',

Eloquent VS MySQLi Native RAW Execution Eloquent VS MySQLi 原生 RAW 执行

Eloquent ORM is slower than Native MySQLi Execution because it has a lot of built-in features. Eloquent ORMNative MySQLi Execution要慢,因为它有很多内置功能。 BUT eloquent will make your code clean and easy.但是eloquent 将使您的代码简洁明了。 And providing better security .并提供更好的security

Localhost VS Remote Host本地主机VS远程主机

When we talk about local server , The performance is depending on your local environment .当我们谈论local server时,性能取决于您的local environment BUT When you use Remote Host , The performance will change because of following reasons.但是当您使用Remote Host时,性能会因为以下原因而改变。

  1. The network connectivity.网络连通性。
  2. The location of your remote host.远程主机的位置。
  3. The performance of your remote host.远程主机的性能。

Best Practice / Architecture最佳实践/架构

When deploy an application to the production, Usually we don't keep database and application in same server.将应用程序部署到生产环境时,通常我们不会将数据库和应用程序保存在同一台服务器上。 In production, Experts do following things to increase the database performance.在生产中,专家会做以下事情来提高数据库性能。

  1. Using Database Replication (across zones).使用数据库复制(跨区域)。
  2. Indexing.索引。
  3. Using Caches.使用缓存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM