That's fantastic, sir, and quite helpful. For those not in IRC, egekhter has done some benchmarking that shows Eloquent to be two orders of magnitude (and change) slower on queries like this than just running a raw SQL query. I'm going to experiment a bit and see what I can come up with. I'll report back.
Most welcome. I'd like to add that there are 5 comparisons: Eloquent vs. Query Builder vs. QueryBuilder optimized, vs. Raw PHP->MySql vs. Raw MySql
Here are the times for each for an arbitrary select statement:
Eloquent = 6400 ms Query Builder = 3700 ms Query Builder optimized = 623 ms Raw PHP->MySql = 162 ms Raw Sql = 35 ms
The optimized method is:
//getOpt() method in Query/Builder.php
public function getOpt()
{
// ~ 36% to compile select statement without bindings
$sql = $this->grammar->compileSelect($this);
// < 1% to get bindings
$bindings = $this->getBindings($this);
// 38% to compile select statement with bindings
foreach ($bindings as $b)
{
$sql = preg_replace('/\?/', $b, $sql, 1);
}
// ~ 25% to execute and return the actual results
return $this->connection->select($sql);
}
If not for the compiling of the select statement and setting the dynamic bindings, this complex query would return in only 162ms.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community