Skip to content

Commit 0faf857

Browse files
authored
Merge pull request #702 from cosmocode/optimize-queries
Optimize queries
2 parents 67f70d5 + 52637a9 commit 0faf857

File tree

5 files changed

+55
-2
lines changed

5 files changed

+55
-2
lines changed

action/migration.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,33 @@ protected function migration19($sqlite)
340340
return $ok;
341341
}
342342

343+
/**
344+
* Executes Migration 20
345+
*
346+
* Adds indexes on "latest" and "published".
347+
* Those fields are not part of (autoindexed) primary key, but are used in many queries.
348+
*
349+
* @param SQLiteDB $sqlite
350+
* @return bool
351+
*/
352+
protected function migration20($sqlite)
353+
{
354+
$ok = true;
355+
356+
/** @noinspection SqlResolve */
357+
$sql = "SELECT name FROM sqlite_master WHERE type = 'table' AND (name LIKE 'data_%' OR name LIKE 'multi_%')";
358+
$tables = $sqlite->queryAll($sql);
359+
360+
foreach ($tables as $row) {
361+
$table = $row['name']; // no escaping needed, it's our own tables
362+
$sql = "CREATE INDEX idx_$table" . "_latest ON $table(latest);";
363+
$ok = $ok && $sqlite->query($sql);
364+
$sql = "CREATE INDEX idx_$table" . "_published ON $table(published);";
365+
$ok = $ok && $sqlite->query($sql);
366+
}
367+
return $ok;
368+
}
369+
343370

344371
/**
345372
* Returns a select statement to fetch Lookup columns in the current schema

db/latest.version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
19
1+
20

db/update0020.sql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
-- this migration is handled in action/migrations.php in migration20()
2+
--
3+
-- it adds indexes on fields that are not automatically indexed (latest, published)

helper/db.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ protected function init()
2929
// register our JSON function with variable parameters
3030
$this->sqlite->getPdo()->sqliteCreateFunction('STRUCT_JSON', [$this, 'STRUCT_JSON'], -1);
3131

32+
// register our JSON decode function with variable parameters
33+
$this->sqlite->getPdo()->sqliteCreateFunction('STRUCT_LOOKUP', [$this, 'STRUCT_LOOKUP'], -1);
34+
3235
// this function is meant to be overwritten by plugins
3336
$this->sqlite->getPdo()->sqliteCreateFunction('IS_PUBLISHER', [$this, 'IS_PUBLISHER'], -1);
3437
}
@@ -83,6 +86,25 @@ public function STRUCT_JSON(...$args) // phpcs:ignore PSR1.Methods.CamelCapsMeth
8386
return json_encode($args, JSON_THROW_ON_ERROR);
8487
}
8588

89+
/**
90+
* Decodes a struct JSON structure and returns the requested value
91+
*
92+
* @param ...$args
93+
* @return mixed|null
94+
*/
95+
public function STRUCT_LOOKUP(...$args) // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps
96+
{
97+
$json = $args[0];
98+
$field = $args[1];
99+
100+
try {
101+
$vals = json_decode($json, true, 512, JSON_THROW_ON_ERROR);
102+
} catch (\JsonException $exception) {
103+
return null;
104+
}
105+
return $vals[$field];
106+
}
107+
86108
/**
87109
* This dummy implementation can be overwritten by a plugin
88110
*

types/Lookup.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,8 @@ public function select(QueryBuilder $QB, $tablealias, $colname, $alias)
245245
$tablealias,
246246
$schema,
247247
$rightalias,
248-
"$tablealias.$colname = STRUCT_JSON($rightalias.pid, CAST($rightalias.rid AS DECIMAL)) " .
248+
"STRUCT_LOOKUP($tablealias.$colname, 0) = $rightalias.pid " .
249+
"AND STRUCT_LOOKUP($tablealias.$colname, 1) = $rightalias.rid " .
249250
"AND $rightalias.latest = 1"
250251
);
251252
$column->getType()->select($QB, $rightalias, $field, $alias);

0 commit comments

Comments
 (0)