I have a table of about 700k rows in Postgres 9.3.3, which has the following structure:
The query that I am making has a full text search query and a limit, as follows: When I search for a string which is in my index with a limit and order in the query it is fast:
However if the string is not in the index it takes much longer:
However if I remove the order clause it is fast for either case:
Removing the limit clause has the same effect, although the in index query is noticably slower:
The little I can deduce is that overall, a bitmap index scan+bitmap heap scan is overall better for my queries then an index scan. How can I tell the query planner to do that though? asked Mar 09 '14 at 12:40 Mohan Krishnan |
Accuracy of statisticsFirst of all, I would start off looking at the statistics. It seems that they're not accurate and, perhaps, your indexes are missing them completely. Note, that in all the plans posted for all indexes estimated number of rows is Please, run Still, it may happen that stats will be off for the
Don't set too high value, as it affects planning time. Available memoryThis line
in the first of the plans without
and try your queries again. Why “wrong” index is used?Let's look at the initial plans. Planner expects, that within the first 3573 rows returned by any index, it'll find all 10 required results. Given that we're retrieving rows in the right order straight away ( This happens due to the fact, that planner always assumes uniform value distribution and, as stated above, in the case of inaccurate stats this leads to heavily underestimated costs. In situations when
Total cost is great for In all other situations TL;DRActually, I have a feeling, that initially table had been created, populated and Primary Key had been introduced, then it was analyzed and later GIN index had been created, without refreshing table stats. So my bets are — refreshing stats should help. If not, then you can always force planner to use the right indexes via
Please, give it a try. answered Sep 25 '14 at 22:26 Victor |