Skip to content

Commit

Permalink
Update count in datatables.php
Browse files Browse the repository at this point in the history
Updated count, so that the query only runs once when you dont have a search action and stripping out the select statements to make the query blazing fast. In my setup i went from 300ms back to 1.6 so im happy!
  • Loading branch information
reneweteling committed Mar 25, 2014
1 parent a1f3c41 commit 3b0aa0a
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions src/Bllim/Datatables/Datatables.php
Original file line number Diff line number Diff line change
Expand Up @@ -471,10 +471,10 @@ public function database_prefix() {
* @param string $count variable to store to 'count_all' for iTotalRecords, 'display_all' for iTotalDisplayRecords
* @return null
*/
private function count($count = 'count_all')
{
//Get columns to temp var.
private function count($count = 'count_all')
{

//Get columns to temp var.
if($this->query_type == 'eloquent') {
$query = $this->query->getQuery();
$connection = $this->query->getModel()->getConnection()->getName();
Expand All @@ -483,11 +483,19 @@ private function count($count = 'count_all')
$query = $this->query;
$connection = $query->getConnection()->getName();
}
//Count the number of rows in the select with the proper connection

// if its a normal query ( no union ) replace the slect with static text to improve performance
$myQuery = clone $query;
if( !preg_match( '/UNION/i', $myQuery->toSql() ) ){
$myQuery->select( DB::Raw("'1' as row") );
}


$this->$count = DB::connection($connection)
->table(DB::raw('('.$query->toSql().') AS count_row_table'))
->setBindings($query->getBindings())->count();
}
->table(DB::raw('('.$myQuery->toSql().') AS count_row_table'))
->setBindings($myQuery->getBindings())->remember(1)->count();

}

/**
* Returns column name from <table>.<column>
Expand Down

0 comments on commit 3b0aa0a

Please sign in to comment.