Performance of like 'query%' on multimillion rows, postgresql -
we have table 10 million rows. need find first few rows 'user%' .
this query fast if matches @ least 2 rows (it returns results in 0.5 sec). if doesn't find 2 rows matching criteria, taking @ least 10 sec. 10 secs huge (since using auto suggestions, users not wait long see suggestions.)
query: select distinct(name) user_sessions name 'user%' limit 2;
in above query, name column of type citext , indexed.
whenever you're working on performance, start explaining query. that'll show the query optimizer's plan, , can sense of how long it's spending doing various pieces. in particular, check full table scans, mean database examining every row in table.
since query fast when finds , slow when doesn't, sounds indeed hitting full table scan. believe it's indexed, since you're doing like, standard string index can't used efficiently. you'll want check out varchar_pattern_ops (or text_pattern_ops, depending on column type of name). create way:
create index on pattern_index_on_users_name on users (name varchar_pattern_ops) after creating index, check explain query make sure it's being used. text_pattern_ops doesn't work citext extension, in case you'll have index , search lower(name) case-insensitive performance:
create index on pattern_index_on_users_name on users (lower(name) text_pattern_ops) select * users lower(name) 'user%' limit 2
Comments
Post a Comment