7

I have a table containing one column and 100 million rows of text. The text is simple 1 - 5 words sentences.

My goal is to have an ultra-fast query which could be used with an autocomplete. So the user types and I get results as fast as possible.

I tried many different indexes and the tsvector feature.

  1. What is a good solution/combination for my needs?
  2. What other solutions can i use to achieve my goal and an be setup up relativly quick ?

2 Answers 2

16

If you want to match the complete text with a prefix, the SQL query would be

SELECT words FROM phrases WHERE words LIKE 'user input%';

A regular B-Tree index with the text_pattern_ops operator class should do the trick.

If the phrases are too long to be indexed or you want to save space, index and query just a prefix:

CREATE INDEX ON phrases (substr(words, 1, 50) text_pattern_ops);

Then query with

SELECT words FROM phrases WHERE substr(words, 1, 50) LIKE 'user input%';
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks, Laurenz! Do you have a LinkedIn, Facebook or something else for networking?
I use TCP/IP. You can hire me from cybertec-postgresql.com
See the extended answer.
I was under the impression that OP wanted to autocomplete all words, not just phrases
Easy - the application can decide that it uses only the current word for searching (truncate at the last space before searching).
2

The LIKE operator is pitifully slow. Try using BETWEEN as described in this blog

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.