Fix very slow table display when filled with ugly data
This commit is contained in:
+23
-10
@@ -31,17 +31,30 @@ class Row extends BaseModel
|
||||
public function scopeSortByCol(Builder $query, Column $column, $dir="asc")
|
||||
{
|
||||
$q = "data->>'".$column->id."'";
|
||||
if ($column->type == 'int' || $column->type == 'float') {
|
||||
$q = "CAST($q AS INTEGER)";
|
||||
}
|
||||
else if ($column->type == 'float') {
|
||||
$q = "CAST($q AS FLOAT)";
|
||||
}
|
||||
|
||||
if ($dir == 'asc') {
|
||||
$q .= ' ASC';
|
||||
} else {
|
||||
$q .= ' DESC';
|
||||
$dir = strtoupper($dir);
|
||||
|
||||
switch ($column->type) {
|
||||
case 'int':
|
||||
$q = "CAST($q AS INTEGER) $dir";
|
||||
break;
|
||||
|
||||
case 'float':
|
||||
$q = "CAST($q AS FLOAT) $dir";
|
||||
break;
|
||||
|
||||
case 'string':
|
||||
// Without a size restriction and a cast, this query becomes VERY slow
|
||||
$q = "CAST($q AS CHAR(25)) $dir, data->>'_id' $dir";
|
||||
// added _id as a fallback to ensure the order is the same between revisions
|
||||
break;
|
||||
|
||||
case 'bool':
|
||||
$q = "CAST($q AS boolean) $dir";
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new \LogicException("Missing SQL cast for column type $column->type, performance will suffer.");
|
||||
}
|
||||
|
||||
return $query->orderByRaw($q);
|
||||
|
||||
Reference in New Issue
Block a user