1
Developing an API that should handle a high performance website. Doing some tests I verified that the use of Eloquent is downloading the performance of the requests in the database Mysql in 51.39% (more than half). OS is Ubuntu 16.04 and the framework is Lumen 5.4. The query selects all cities from the Cities table (5565 city).
//Rota
$app->get('cities', 'CitiesController@index');
This is controller Cities Citiescontroller:
namespace App\Http\Controllers;
use Illuminate\Support\Facades\DB;
use App\Models\Citie;
class CitiesController extends Controller
{
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
//
}
public function index()
{
return DB::select("SELECT * FROM cities");
//return Citie::all();
}
}
This is Model Citie:
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Citie extends Model
{
protected $guarded=['id'];
}
I’m using POSTMAN to make the 10 requests for each approach (With and without Eloquent).
Com Eloquente - > return Citie::all();
Sem Eloquente - > return DB::select("SELECT * FROM cities");
And these are the results in milliseconds(ms):
Eloquently:
1975
1698
1809
1817
1918
1841
1897
1689
1854
1958
média - 1845.6
Eloquently:
3449
3684
3654
3667
3546
3491
3462
3531
3614
3815
média - 3591.3
The result is that in this test the use of Eloquent is decreasing the performance of the request by 51.39 % compared to the scenario without Eloquent.
And the sql of Cities table creation:
-- -----------------------------------------------------
-- Table `cities`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `cities` (
`fk_province` INT NOT NULL,
`id` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(45) NOT NULL,
`geocodeBr` VARCHAR(50) NULL DEFAULT 'null',
`lat` DECIMAL(10,8) NULL DEFAULT NULL,
`long` DECIMAL(11,8) NULL DEFAULT NULL,
`created_at` TIMESTAMP NULL,
`updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
INDEX `fk_city_province_idx` (`fk_province` ASC),
CONSTRAINT `fk_city_province`
FOREIGN KEY (`fk_province`)
REFERENCES `provinces` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
So the question is how to use the Eloquent so that it reaches the average test results without using Eloquent, in case - 1845.6 ms, ie without losing this performance of just over 50%?
Some configuration that is missing?
Note:
This is a fresh installation of Lumen 5.4. I only drew comments from Facade and Eloquent on bootstrap/app.php
as suggested by official documentation.
I did not perform test on the Lumen, but, anti hand the
DB::select
will always be faster than classORM Eloquent
, theDB::select
returns aarray
ofstdClass
that is, simplearray
already theORM Eloquent
will return aCollection
of the class type, each item is a complex structure, attached to a context and each item can manipulate its data to change and even delete! then they are different things and by default Lumen disables the use of Eloquent, even because of the performance. I mean, that’s just the way it is.– novic
I was already suspicious of the vagueness of the use of the Eloquent because it carries other classes and follows other paths to return the answer. Just did not imagine that it was so much ( more than 50%). So this is the price considered acceptable to work with Models and Eloquent?
– zwitterion
Is not slow the
Eloquent
(some will say this guy is crazy, kkk) this is how it was done and most of theFULL ORM
are thus, in the specific case is generating a list of values and the architecture of theEloquent
it does not carry a simple datum, but rather a Collection (which is a class) and within it a collection of items of a certain class imagine you that each item of that class can be changed and saved, whereas in the other being a pure SQL it does not have these functionalities is a dry datum, If you ask the bank for the result it returns you, Eloquent transforms you, there’s the difference.– novic
Have you thought about using HTTP cache and possibly a "back-end cache"? Here’s what I posted: https://answall.com/a/166747/3635
– Guilherme Nascimento