简体   繁体   中英

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. Here are the results,

MySQL on Localhost and Eloquent: ~30ms

MySQL on Remote Server and Eloquent: ~2.7 seconds

MySQL on Localhost and PHP: ~10ms

MySQL on Remote Server and PHP: ~850ms

The average ping from my computer to the the remote server ip is about 150ms.

Here is the PHP script I used,

// 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) .

I still haven't tried running eloquent on a server, but is this normal? Should I steer away from using eloquent if I am going to be using remote mysql databases on different servers?

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.

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

Eloquent VS MySQLi Native RAW Execution

Eloquent ORM is slower than Native MySQLi Execution because it has a lot of built-in features. BUT eloquent will make your code clean and easy. And providing better security .

Localhost VS Remote Host

When we talk about local server , The performance is depending on your local environment . BUT When you use Remote Host , The performance will change because of following reasons.

  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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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