Why is Laravel Query Builder Slower Than Knex.js and Raw Query Analyzer #56004
Replies: 2 comments 5 replies
-
public function index()
{
$data = DB::table('parameter')->get(); // fetches the data from db without populating active records (models)
$count = DB::table('parameter')->count();
// $data = Parameter::all(); // fetches the data from db by populating models and then calling on each of them toArray() which does many things.
// $count = Parameter::count(); // count should have pretty much the same execution time.
return response()->json([
'data' => $data,
'total' => $count
]);
} So, not the query builder is slower even if when building the query it might do more steps vs raw query but the processing of the fetched data by eloquent makes the difference. Update. |
Beta Was this translation helpful? Give feedback.
-
there are a few potential explanations here and i expect in reality there is a bit of each at play my first question is how exactly is the performance being measured in each case? there are a few different stages that need to happen in order for that example andpoint to return data
basically every one of these stages has opportunities to be an issue PHP, atleast when running in its traditional run mode has some characteristics which can impact this, in particular that the application has to be loaded from disk and parsed on every request, which means the speed of the underlying disk plays a large role, and this also has a knock on effect that the DB connection cant be reused from 1 request to the next it has to be reinitialized there are a few projects which aim to solve that such as swoole/openswoole/frankenphp which recent versions of laravel support via generating the query tbh based on your example there is nothing to generate you are just fetching an entire table establishing the DB connection can take a decent amount of time depending on networking / etc waiting for the DB to process the query is where things start to get interesting, there are many factors which will affect how long it takes the DB to process a query, including some which will vary over time and are not consistent, typically RDBMS will have some level of caching of queries so repeating the same query multiple times would be expected to get faster in some cases significantly. the OS and underlying stroage device may also have their own caching, while i dont doubt that some of the other points i mention specific to php/laravel are more significant factors it is almost impossible to completely isolate the DB itself the result data being sent back is going to depend a lot on the amount of data along with networking then the app getting that data into whatever structure it uses is also going to depend on how much data there is along with how much overhead the structure has and then processing the data into the format to respond to user is once again going to depend on how much data and how efficient the processing is those last 3 (and really last 4) are heavily dependand on how much data is being returned from the query that is one of the reasons that ORMs / Active Record implementations have a reputation for being slow, because by default they will load more data than is needed while i expect a bit of all of these factors is at play, as for recommendations
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Laravel Version
8.0
PHP Version
7.4
Database Driver & Version
SQL Server
Description
Hi everyone,
I've encountered a noticeable performance issue when using Laravel's Query Builder with SQL Server. Running the same simple query gives very different execution times depending on the method used:
Laravel Query Builder: ~800–1000ms
Knex.js (Node.js): ~200–300ms
Raw SQL via SQL Server Management Studio (SSMS) Query Analyzer: ~100–150ms
All tests are done using the same SQL Server instance, schema, and indexes.
My questions:
Why does Laravel's Query Builder perform significantly slower with SQL Server in comparison to Knex.js or SSMS?
Does Laravel introduce extra overhead (e.g., parameter binding, internal abstraction, or data casting) that affects performance on SQL Server?
Are there known performance pitfalls specific to Laravel and SQL Server (e.g., driver issues, odbc vs sqlsrv)?
Would using DB::select() with raw SQL be a better choice for high-performance scenarios in Laravel with SQL Server?
Any tips on optimizing Laravel for SQL Server would be greatly appreciated!
Thanks in advance!
Steps To Reproduce
Beta Was this translation helpful? Give feedback.
All reactions