From 9fa374d8c5bccd092e7c49f4cdc3a16710867a89 Mon Sep 17 00:00:00 2001 From: Dan Gowans Date: Wed, 22 Feb 2023 14:32:12 -0500 Subject: [PATCH] track used search terms to reduce slow downs --- helpers/functions.sqlFilters.js | 23 ++++++++++++----------- helpers/functions.sqlFilters.ts | 28 +++++++++++++++++----------- 2 files changed, 29 insertions(+), 22 deletions(-) diff --git a/helpers/functions.sqlFilters.js b/helpers/functions.sqlFilters.js index 883b8c47..73ca4fbc 100644 --- a/helpers/functions.sqlFilters.js +++ b/helpers/functions.sqlFilters.js @@ -15,13 +15,14 @@ export function getLotNameWhereClause(lotName = '', lotNameSearchType, lotsTable break; } default: { + const usedPieces = new Set(); const lotNamePieces = lotName.toLowerCase().split(' '); for (const lotNamePiece of lotNamePieces) { - if (lotNamePiece === '') { + if (lotNamePiece === '' || usedPieces.has(lotNamePiece)) { continue; } - sqlWhereClause += - ' and instr(lower(' + lotsTableAlias + '.lotName), ?)'; + usedPieces.add(lotNamePiece); + sqlWhereClause += ` and instr(lower(${lotsTableAlias}.lotName), ?)`; sqlParameters.push(lotNamePiece); } } @@ -70,15 +71,15 @@ export function getOccupancyTimeWhereClause(occupancyTime, lotOccupanciesTableAl export function getOccupantNameWhereClause(occupantName = '', tableAlias = 'o') { let sqlWhereClause = ''; const sqlParameters = []; - if (occupantName !== '') { - const occupantNamePieces = occupantName.toLowerCase().split(' '); - for (const occupantNamePiece of occupantNamePieces) { - if (occupantNamePiece === '') { - continue; - } - sqlWhereClause += ` and (instr(lower(${tableAlias}.occupantName), ?) or instr(lower(${tableAlias}.occupantFamilyName), ?))`; - sqlParameters.push(occupantNamePiece, occupantNamePiece); + const usedPieces = new Set(); + const occupantNamePieces = occupantName.toLowerCase().split(' '); + for (const occupantNamePiece of occupantNamePieces) { + if (occupantNamePiece === '' || usedPieces.has(occupantNamePiece)) { + continue; } + usedPieces.add(occupantNamePiece); + sqlWhereClause += ` and (instr(lower(${tableAlias}.occupantName), ?) or instr(lower(${tableAlias}.occupantFamilyName), ?))`; + sqlParameters.push(occupantNamePiece, occupantNamePiece); } return { sqlWhereClause, diff --git a/helpers/functions.sqlFilters.ts b/helpers/functions.sqlFilters.ts index 356f7ae1..d801111f 100644 --- a/helpers/functions.sqlFilters.ts +++ b/helpers/functions.sqlFilters.ts @@ -28,14 +28,18 @@ export function getLotNameWhereClause( break } default: { + const usedPieces = new Set() + const lotNamePieces = lotName.toLowerCase().split(' ') + for (const lotNamePiece of lotNamePieces) { - if (lotNamePiece === '') { + if (lotNamePiece === '' || usedPieces.has(lotNamePiece)) { continue } - sqlWhereClause += - ' and instr(lower(' + lotsTableAlias + '.lotName), ?)' + usedPieces.add(lotNamePiece) + + sqlWhereClause += ` and instr(lower(${lotsTableAlias}.lotName), ?)` sqlParameters.push(lotNamePiece) } } @@ -101,16 +105,18 @@ export function getOccupantNameWhereClause( let sqlWhereClause = '' const sqlParameters: unknown[] = [] - if (occupantName !== '') { - const occupantNamePieces = occupantName.toLowerCase().split(' ') - for (const occupantNamePiece of occupantNamePieces) { - if (occupantNamePiece === '') { - continue - } + const usedPieces = new Set() - sqlWhereClause += ` and (instr(lower(${tableAlias}.occupantName), ?) or instr(lower(${tableAlias}.occupantFamilyName), ?))` - sqlParameters.push(occupantNamePiece, occupantNamePiece) + const occupantNamePieces = occupantName.toLowerCase().split(' ') + for (const occupantNamePiece of occupantNamePieces) { + if (occupantNamePiece === '' || usedPieces.has(occupantNamePiece)) { + continue } + + usedPieces.add(occupantNamePiece) + + sqlWhereClause += ` and (instr(lower(${tableAlias}.occupantName), ?) or instr(lower(${tableAlias}.occupantFamilyName), ?))` + sqlParameters.push(occupantNamePiece, occupantNamePiece) } return {