Developing an API that must handle a high-performance website. By doing a few tests, I noticed that Eloquent usage is lowering the performance of requests in the MySQL database in 51.39% (more than half). The 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 Cities CitiesController controller:
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 the Model Citie:
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Citie extends Model
{
protected $guarded=['id'];
}
I'm using POSTMAN to do the 10 requisitions 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):
No Eloquent:
1975
1698
1809
1817
1918
1841
1897
1689
1854
1958
média - 1845.6
With Eloquent:
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 requisite performance by 51.39% compared to the scenario without Eloquent.
Here is an example of the output:
Andthesqlofcreatingthecities:
---------------------------------------------------------Table'cities'-------------------------------------------------------CREATETABLEIFNOTEXISTS'cities'('fk_province'INTNOTNULL,'id'INTNOTNULLAUTO_INCREMENT,'name'VARCHAR(45)NOTNULL,'geocodeBr'VARCHAR(50)NULLDEFAULT'null','lat'DECIMAL(10,8)NULLDEFAULTNULL,'long'DECIMAL(11,8)NULLDEFAULTNULL,'created_at'TIMESTAMPNULL,'updated_at'TIMESTAMPNOTNULLDEFAULTCURRENT_TIMESTAMP,PRIMARYKEY('id'),INDEX'fk_city_province_idx'('fk_province'ASC),CONSTRAINT'fk_city_province'FOREIGNKEY('fk_province')REFERENCES'provinces'('id')ONDELETENOACTIONONUPDATENOACTION)ENGINE=InnoDBDEFAULTCHARACTERSET=utf8;
SothequestionishowtouseEloquentsothatitreachestheaverageofthetestresultswithoutusingEloquent,inthecase-1845.6ms,thatis,withoutlosingthatperformanceofjustover50%?>
Anyconfigurationthatismissing?
Note:ThisisafreshinstallofLumen5.4.IonlyremovedcommentsfromFacadeandEloquentinbootstrap/app.php
assuggestedby official documentation .