Full-text search runs slow on single rare word but runs fast when same word repeated?
I have a table with a GIN index similar to:
CREATE INDEX my_index ON my_table USING GIN (to_tsvector('english', lower(
(jsondoc->>'StreetAddress')
|| ' ' || (jsondoc->>'Neighborhood')
|| ' ' || (jsondoc->>'Area')
|| ' ' || (jsondoc->>'CrossStreet')
|| ' ' || (jsondoc->>'Community')
|| ' ' || (jsondoc->>'Complex') )))
where source = 'x';
My table has 2.5M rows.
Suppose I want to query for the word banana, which is not a very common word in this dataset. In fact, it appears exactly 8 times.
Now here's the weird part.
This query takes a repeatable 3 full seconds to execute:
select * from my_table where
(source='x' AND plainto_tsquery('english', 'banana') @@ to_tsvector('english', lower(
(jsondoc->>'StreetAddress')
|| ' ' || (jsondoc->>'Neighborhood')
|| ' ' || (jsondoc->>'Area')
|| ' ' || (jsondoc->>'CrossStreet')
|| ' ' || (jsondoc->>'Community')
|| ' ' || (jsondoc->>'Complex') )) AND (jsondoc->>'L_AskingPrice')::real>=250000 AND ((jsondoc->>'L_AskingPrice')::real<=2500000 OR (jsondoc->>'L_asking_price_low')::real<=2500000) AND jsondoc->>
'L_Status' IN ('ACTIVE','BACK ON MARKET') AND iscurrent='true')
while this executes reliably in just under 50 milliseconds:
select * from my_table where
(source='x' AND plainto_tsquery('english', 'banana banana') @@ to_tsvector('english', lower(
(jsondoc->>'StreetAddress')
|| ' ' || (jsondoc->>'Neighborhood')
|| ' ' || (jsondoc->>'Area')
|| ' ' || (jsondoc->>'CrossStreet')
|| ' ' || (jsondoc->>'Community')
|| ' ' || (jsondoc->>'Complex') )) AND (jsondoc->>'L_AskingPrice')::real>=250000 AND ((jsondoc->>'L_AskingPrice')::real<=2500000 OR (jsondoc->>'L_asking_price_low')::real<=2500000) AND jsondoc->>
'L_Status' IN ('ACTIVE','BACK ON MARKET') AND iscurrent='true')
Can you spot the difference?
Yes: the word banana is repeated in the second query. Now, this is just an example. Through additional testing, I found that essentially any single word takes a long time (~3 seconds), while any double-word search (including, 2 different words) runs fast (~50ms).
PostgreSQL version is the latest (v11).
What could cause this?
Query Plans
Slow (one word)

Fast (one word repeated twice)

postgresql query-optimization
|
show 4 more comments
I have a table with a GIN index similar to:
CREATE INDEX my_index ON my_table USING GIN (to_tsvector('english', lower(
(jsondoc->>'StreetAddress')
|| ' ' || (jsondoc->>'Neighborhood')
|| ' ' || (jsondoc->>'Area')
|| ' ' || (jsondoc->>'CrossStreet')
|| ' ' || (jsondoc->>'Community')
|| ' ' || (jsondoc->>'Complex') )))
where source = 'x';
My table has 2.5M rows.
Suppose I want to query for the word banana, which is not a very common word in this dataset. In fact, it appears exactly 8 times.
Now here's the weird part.
This query takes a repeatable 3 full seconds to execute:
select * from my_table where
(source='x' AND plainto_tsquery('english', 'banana') @@ to_tsvector('english', lower(
(jsondoc->>'StreetAddress')
|| ' ' || (jsondoc->>'Neighborhood')
|| ' ' || (jsondoc->>'Area')
|| ' ' || (jsondoc->>'CrossStreet')
|| ' ' || (jsondoc->>'Community')
|| ' ' || (jsondoc->>'Complex') )) AND (jsondoc->>'L_AskingPrice')::real>=250000 AND ((jsondoc->>'L_AskingPrice')::real<=2500000 OR (jsondoc->>'L_asking_price_low')::real<=2500000) AND jsondoc->>
'L_Status' IN ('ACTIVE','BACK ON MARKET') AND iscurrent='true')
while this executes reliably in just under 50 milliseconds:
select * from my_table where
(source='x' AND plainto_tsquery('english', 'banana banana') @@ to_tsvector('english', lower(
(jsondoc->>'StreetAddress')
|| ' ' || (jsondoc->>'Neighborhood')
|| ' ' || (jsondoc->>'Area')
|| ' ' || (jsondoc->>'CrossStreet')
|| ' ' || (jsondoc->>'Community')
|| ' ' || (jsondoc->>'Complex') )) AND (jsondoc->>'L_AskingPrice')::real>=250000 AND ((jsondoc->>'L_AskingPrice')::real<=2500000 OR (jsondoc->>'L_asking_price_low')::real<=2500000) AND jsondoc->>
'L_Status' IN ('ACTIVE','BACK ON MARKET') AND iscurrent='true')
Can you spot the difference?
Yes: the word banana is repeated in the second query. Now, this is just an example. Through additional testing, I found that essentially any single word takes a long time (~3 seconds), while any double-word search (including, 2 different words) runs fast (~50ms).
PostgreSQL version is the latest (v11).
What could cause this?
Query Plans
Slow (one word)

Fast (one word repeated twice)

postgresql query-optimization
2
Difficult to say since you've kept the EXPLAIN ANALYSE output hidden from everyone.
– Richard Huxton
Nov 17 '18 at 11:08
@RichardHuxton see updates
– Alex R
Nov 17 '18 at 16:53
Excellent question. But plaease provide data (incl. query plans) as text, never as image. The lines are truncated, too.
– Erwin Brandstetter
Nov 17 '18 at 17:19
How? I’m using pgAdmin, there is no option to copy as text.
– Alex R
Nov 17 '18 at 17:20
You can just select and copy the output fromEXPLAIN (ANALYZE, BUFFERS) SELECT...in pgAdmin3, pgAdmin4 or psql (or any other client).
– Erwin Brandstetter
Nov 17 '18 at 18:02
|
show 4 more comments
I have a table with a GIN index similar to:
CREATE INDEX my_index ON my_table USING GIN (to_tsvector('english', lower(
(jsondoc->>'StreetAddress')
|| ' ' || (jsondoc->>'Neighborhood')
|| ' ' || (jsondoc->>'Area')
|| ' ' || (jsondoc->>'CrossStreet')
|| ' ' || (jsondoc->>'Community')
|| ' ' || (jsondoc->>'Complex') )))
where source = 'x';
My table has 2.5M rows.
Suppose I want to query for the word banana, which is not a very common word in this dataset. In fact, it appears exactly 8 times.
Now here's the weird part.
This query takes a repeatable 3 full seconds to execute:
select * from my_table where
(source='x' AND plainto_tsquery('english', 'banana') @@ to_tsvector('english', lower(
(jsondoc->>'StreetAddress')
|| ' ' || (jsondoc->>'Neighborhood')
|| ' ' || (jsondoc->>'Area')
|| ' ' || (jsondoc->>'CrossStreet')
|| ' ' || (jsondoc->>'Community')
|| ' ' || (jsondoc->>'Complex') )) AND (jsondoc->>'L_AskingPrice')::real>=250000 AND ((jsondoc->>'L_AskingPrice')::real<=2500000 OR (jsondoc->>'L_asking_price_low')::real<=2500000) AND jsondoc->>
'L_Status' IN ('ACTIVE','BACK ON MARKET') AND iscurrent='true')
while this executes reliably in just under 50 milliseconds:
select * from my_table where
(source='x' AND plainto_tsquery('english', 'banana banana') @@ to_tsvector('english', lower(
(jsondoc->>'StreetAddress')
|| ' ' || (jsondoc->>'Neighborhood')
|| ' ' || (jsondoc->>'Area')
|| ' ' || (jsondoc->>'CrossStreet')
|| ' ' || (jsondoc->>'Community')
|| ' ' || (jsondoc->>'Complex') )) AND (jsondoc->>'L_AskingPrice')::real>=250000 AND ((jsondoc->>'L_AskingPrice')::real<=2500000 OR (jsondoc->>'L_asking_price_low')::real<=2500000) AND jsondoc->>
'L_Status' IN ('ACTIVE','BACK ON MARKET') AND iscurrent='true')
Can you spot the difference?
Yes: the word banana is repeated in the second query. Now, this is just an example. Through additional testing, I found that essentially any single word takes a long time (~3 seconds), while any double-word search (including, 2 different words) runs fast (~50ms).
PostgreSQL version is the latest (v11).
What could cause this?
Query Plans
Slow (one word)

Fast (one word repeated twice)

postgresql query-optimization
I have a table with a GIN index similar to:
CREATE INDEX my_index ON my_table USING GIN (to_tsvector('english', lower(
(jsondoc->>'StreetAddress')
|| ' ' || (jsondoc->>'Neighborhood')
|| ' ' || (jsondoc->>'Area')
|| ' ' || (jsondoc->>'CrossStreet')
|| ' ' || (jsondoc->>'Community')
|| ' ' || (jsondoc->>'Complex') )))
where source = 'x';
My table has 2.5M rows.
Suppose I want to query for the word banana, which is not a very common word in this dataset. In fact, it appears exactly 8 times.
Now here's the weird part.
This query takes a repeatable 3 full seconds to execute:
select * from my_table where
(source='x' AND plainto_tsquery('english', 'banana') @@ to_tsvector('english', lower(
(jsondoc->>'StreetAddress')
|| ' ' || (jsondoc->>'Neighborhood')
|| ' ' || (jsondoc->>'Area')
|| ' ' || (jsondoc->>'CrossStreet')
|| ' ' || (jsondoc->>'Community')
|| ' ' || (jsondoc->>'Complex') )) AND (jsondoc->>'L_AskingPrice')::real>=250000 AND ((jsondoc->>'L_AskingPrice')::real<=2500000 OR (jsondoc->>'L_asking_price_low')::real<=2500000) AND jsondoc->>
'L_Status' IN ('ACTIVE','BACK ON MARKET') AND iscurrent='true')
while this executes reliably in just under 50 milliseconds:
select * from my_table where
(source='x' AND plainto_tsquery('english', 'banana banana') @@ to_tsvector('english', lower(
(jsondoc->>'StreetAddress')
|| ' ' || (jsondoc->>'Neighborhood')
|| ' ' || (jsondoc->>'Area')
|| ' ' || (jsondoc->>'CrossStreet')
|| ' ' || (jsondoc->>'Community')
|| ' ' || (jsondoc->>'Complex') )) AND (jsondoc->>'L_AskingPrice')::real>=250000 AND ((jsondoc->>'L_AskingPrice')::real<=2500000 OR (jsondoc->>'L_asking_price_low')::real<=2500000) AND jsondoc->>
'L_Status' IN ('ACTIVE','BACK ON MARKET') AND iscurrent='true')
Can you spot the difference?
Yes: the word banana is repeated in the second query. Now, this is just an example. Through additional testing, I found that essentially any single word takes a long time (~3 seconds), while any double-word search (including, 2 different words) runs fast (~50ms).
PostgreSQL version is the latest (v11).
What could cause this?
Query Plans
Slow (one word)

Fast (one word repeated twice)

postgresql query-optimization
postgresql query-optimization
edited Nov 17 '18 at 16:53
asked Nov 17 '18 at 10:31
Alex R
4,106104798
4,106104798
2
Difficult to say since you've kept the EXPLAIN ANALYSE output hidden from everyone.
– Richard Huxton
Nov 17 '18 at 11:08
@RichardHuxton see updates
– Alex R
Nov 17 '18 at 16:53
Excellent question. But plaease provide data (incl. query plans) as text, never as image. The lines are truncated, too.
– Erwin Brandstetter
Nov 17 '18 at 17:19
How? I’m using pgAdmin, there is no option to copy as text.
– Alex R
Nov 17 '18 at 17:20
You can just select and copy the output fromEXPLAIN (ANALYZE, BUFFERS) SELECT...in pgAdmin3, pgAdmin4 or psql (or any other client).
– Erwin Brandstetter
Nov 17 '18 at 18:02
|
show 4 more comments
2
Difficult to say since you've kept the EXPLAIN ANALYSE output hidden from everyone.
– Richard Huxton
Nov 17 '18 at 11:08
@RichardHuxton see updates
– Alex R
Nov 17 '18 at 16:53
Excellent question. But plaease provide data (incl. query plans) as text, never as image. The lines are truncated, too.
– Erwin Brandstetter
Nov 17 '18 at 17:19
How? I’m using pgAdmin, there is no option to copy as text.
– Alex R
Nov 17 '18 at 17:20
You can just select and copy the output fromEXPLAIN (ANALYZE, BUFFERS) SELECT...in pgAdmin3, pgAdmin4 or psql (or any other client).
– Erwin Brandstetter
Nov 17 '18 at 18:02
2
2
Difficult to say since you've kept the EXPLAIN ANALYSE output hidden from everyone.
– Richard Huxton
Nov 17 '18 at 11:08
Difficult to say since you've kept the EXPLAIN ANALYSE output hidden from everyone.
– Richard Huxton
Nov 17 '18 at 11:08
@RichardHuxton see updates
– Alex R
Nov 17 '18 at 16:53
@RichardHuxton see updates
– Alex R
Nov 17 '18 at 16:53
Excellent question. But plaease provide data (incl. query plans) as text, never as image. The lines are truncated, too.
– Erwin Brandstetter
Nov 17 '18 at 17:19
Excellent question. But plaease provide data (incl. query plans) as text, never as image. The lines are truncated, too.
– Erwin Brandstetter
Nov 17 '18 at 17:19
How? I’m using pgAdmin, there is no option to copy as text.
– Alex R
Nov 17 '18 at 17:20
How? I’m using pgAdmin, there is no option to copy as text.
– Alex R
Nov 17 '18 at 17:20
You can just select and copy the output from
EXPLAIN (ANALYZE, BUFFERS) SELECT... in pgAdmin3, pgAdmin4 or psql (or any other client).– Erwin Brandstetter
Nov 17 '18 at 18:02
You can just select and copy the output from
EXPLAIN (ANALYZE, BUFFERS) SELECT... in pgAdmin3, pgAdmin4 or psql (or any other client).– Erwin Brandstetter
Nov 17 '18 at 18:02
|
show 4 more comments
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53350359%2ffull-text-search-runs-slow-on-single-rare-word-but-runs-fast-when-same-word-repe%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53350359%2ffull-text-search-runs-slow-on-single-rare-word-but-runs-fast-when-same-word-repe%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
Difficult to say since you've kept the EXPLAIN ANALYSE output hidden from everyone.
– Richard Huxton
Nov 17 '18 at 11:08
@RichardHuxton see updates
– Alex R
Nov 17 '18 at 16:53
Excellent question. But plaease provide data (incl. query plans) as text, never as image. The lines are truncated, too.
– Erwin Brandstetter
Nov 17 '18 at 17:19
How? I’m using pgAdmin, there is no option to copy as text.
– Alex R
Nov 17 '18 at 17:20
You can just select and copy the output from
EXPLAIN (ANALYZE, BUFFERS) SELECT...in pgAdmin3, pgAdmin4 or psql (or any other client).– Erwin Brandstetter
Nov 17 '18 at 18:02