Fix very slow table display when filled with ugly data

master
Ondřej Hruška 6 years ago
parent 577a5349c4
commit b716260a53
Signed by: MightyPork
GPG Key ID: 2C5FD5035250423D
  1. 33
      app/Models/Row.php

@ -31,17 +31,30 @@ class Row extends BaseModel
public function scopeSortByCol(Builder $query, Column $column, $dir="asc") public function scopeSortByCol(Builder $query, Column $column, $dir="asc")
{ {
$q = "data->>'".$column->id."'"; $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') { $dir = strtoupper($dir);
$q .= ' ASC';
} else { switch ($column->type) {
$q .= ' DESC'; 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); return $query->orderByRaw($q);

Loading…
Cancel
Save