0

There are two sql in a table, one of them is too slow, any one can tell me what's wrong and how can i optimize it?

There is their query explain blow: slow query explain

[---------edit---------]

new query explain

2
  • 1
    I have a gin index on field title and a b-tree index on field city; Commented Oct 27, 2017 at 7:59
  • For the future: please post execution plans as formatted text not screen shots. You can also upload them to explain.depesz.com if you think they are too big to be posted here. Commented Oct 27, 2017 at 11:47

1 Answer 1

3

The bitmap index scan in the second query returns more rows because the condition is less selective.

As a consequence, the bitmap becomes too large to fit into work_mem and PostgreSQL continues by storing one bit per matching block rather than per matching row (those are the lossy entries mentioned).

So many more rows have to be checked during the bitmap heap scan, which causes the longer execution time.

Increase work_mem for better results.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you, it worked! Do you know any other option can optimize the sql? I set work_mem=64M, it still need 300 ms...
I added a picture of new sql explain in question body, thanks~
This is just how long it takes - I don't think it can be optimized any further. It has to crunch a lot of data.
OK,I really appriciate it.

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.