mirror of
https://github.com/postgres/postgres.git
synced 2025-04-21 12:05:57 +03:00
Improve GiST index search performance for trigram regex queries.
The initial coding just descended the index if any of the target trigrams were possibly present at the next level down. But actually we can apply trigramsMatchGraph() so as to take advantage of AND requirements when there are some. The input data might contain false positive matches, but that can only result in a false positive result, not false negative, so it's safe to do it this way. Alexander Korotkov
This commit is contained in:
parent
e08fdf1310
commit
410bed2ab8
@ -401,18 +401,24 @@ gtrgm_consistent(PG_FUNCTION_ARGS)
|
|||||||
len = ARRNELEM(qtrg);
|
len = ARRNELEM(qtrg);
|
||||||
trgm *ptr = GETARR(qtrg);
|
trgm *ptr = GETARR(qtrg);
|
||||||
BITVECP sign = GETSIGN(key);
|
BITVECP sign = GETSIGN(key);
|
||||||
|
bool *check;
|
||||||
|
|
||||||
/* descend only if at least one trigram is present */
|
/*
|
||||||
res = false;
|
* GETBIT() tests may give false positives, due to limited
|
||||||
|
* size of the sign array. But since trigramsMatchGraph()
|
||||||
|
* implements a monotone boolean function, false positives
|
||||||
|
* in the check array can't lead to false negative answer.
|
||||||
|
* So we can apply trigramsMatchGraph despite uncertainty,
|
||||||
|
* and that usefully improves the quality of the search.
|
||||||
|
*/
|
||||||
|
check = (bool *) palloc(len * sizeof(bool));
|
||||||
for (k = 0; k < len; k++)
|
for (k = 0; k < len; k++)
|
||||||
{
|
{
|
||||||
CPTRGM(((char *) &tmp), ptr + k);
|
CPTRGM(((char *) &tmp), ptr + k);
|
||||||
if (GETBIT(sign, HASHVAL(tmp)))
|
check[k] = GETBIT(sign, HASHVAL(tmp));
|
||||||
{
|
|
||||||
res = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
res = trigramsMatchGraph(cache->graph, check);
|
||||||
|
pfree(check);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
Loading…
x
Reference in New Issue
Block a user