Skip to content

Commit

Permalink
feat(perf): rewrite query for performance
Browse files Browse the repository at this point in the history
  • Loading branch information
missinglink committed Dec 6, 2021
1 parent 7fa36b1 commit 495b74f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 51 deletions.
8 changes: 4 additions & 4 deletions lib/Queries.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,9 @@ module.exports.matchSubjectObjectGeomIntersects = function( subject, object, cb
if( '' === object.trim() ){ return cb( null, [] ); }

this._queryAll(
this.prepare( query.match_subject_object_geom_intersects_autocomplete ),
this.prepare( query.match_subject_object_geom_intersects ),
{
subject: subject,
subject: `"${subject}"`,
object: `"${object}" OR "${object}"*`,
threshold: RTREE_THRESHOLD,
limit: MAX_RESULTS
Expand All @@ -163,9 +163,9 @@ module.exports.matchSubjectObjectGeomIntersects = function( subject, object, cb
);
} else {
this._queryAll(
this.prepare( query.match_subject_object_geom_intersects_autocomplete ),
this.prepare( query.match_subject_object_geom_intersects ),
{
subject: subject,
subject: `"${subject}"`,
object: `"${object}"`,
threshold: RTREE_THRESHOLD,
limit: MAX_RESULTS
Expand Down
47 changes: 25 additions & 22 deletions query/match_subject_object_geom_intersects.sql
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
SELECT t1.id AS subjectId, t2.id as objectId
FROM rtree AS r1, rtree AS r2
JOIN tokens AS t1 ON t1.id = r1.id
JOIN tokens AS t2 ON t2.id = r2.id
WHERE t1.token = $subject
AND t2.token = $object
AND (
t1.lang = t2.lang OR
t1.lang IN ( 'eng', 'und' ) OR
t2.lang IN ( 'eng', 'und' )
)
-- https://silentmatt.com/rectangle-intersection/
AND (
r1.maxZ < r2.minZ AND
r1.minX - $threshold < r2.maxX AND
r1.maxX + $threshold > r2.minX AND
r1.minY - $threshold < r2.maxY AND
r1.maxY + $threshold > r2.minY
)
-- AND t1.tag NOT IN ( 'colloquial' )
-- AND t2.tag NOT IN ( 'colloquial' )
SELECT
t1.id AS subjectId,
t2.id as objectId
FROM fulltext f1
JOIN tokens t1 ON f1.rowid = t1.rowid
JOIN rtree AS r1 ON t1.id = r1.id
JOIN rtree AS r2 ON (
r1.maxZ < r2.minZ AND
(r1.minX - $threshold) < r2.maxX AND
(r1.maxX + $threshold) > r2.minX AND
(r1.minY - $threshold) < r2.maxY AND
(r1.maxY + $threshold) > r2.minY
)
JOIN fulltext AS f2 ON f2.fulltext MATCH $object
JOIN tokens t2 ON (
f2.rowid = t2.rowid
AND r2.id = t2.id
AND (
t1.lang = t2.lang OR
t1.lang IN ('eng', 'und') OR
t2.lang IN ('eng', 'und')
)
)
WHERE f1.fulltext MATCH $subject
GROUP BY t1.id, t2.id
ORDER BY t1.id ASC, t2.id ASC
LIMIT $limit
LIMIT $limit
25 changes: 0 additions & 25 deletions query/match_subject_object_geom_intersects_autocomplete.sql

This file was deleted.

0 comments on commit 495b74f

Please sign in to comment.