Full-text search runs slow on single rare word but runs fast when same word repeated?












2














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)
Slow



Fast (one word repeated twice)
Fast










share|improve this question




















  • 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
















2














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)
Slow



Fast (one word repeated twice)
Fast










share|improve this question




















  • 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














2












2








2







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)
Slow



Fast (one word repeated twice)
Fast










share|improve this question















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)
Slow



Fast (one word repeated twice)
Fast







postgresql query-optimization






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 from EXPLAIN (ANALYZE, BUFFERS) SELECT... in pgAdmin3, pgAdmin4 or psql (or any other client).
    – Erwin Brandstetter
    Nov 17 '18 at 18:02














  • 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








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












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
});


}
});














draft saved

draft discarded


















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
















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

How to send String Array data to Server using php in android

Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

Is anime1.com a legal site for watching anime?