postgresql - query is slower after adding index -


i have books table in postgres database has user_id column. when there no index, querying books user_id took 0.607 ms

lesson4=# select * books user_id = 5;  time: 0.607 ms 

i created index this

lesson4=# create index user_idx on books (user_id); create index time: 43.288 ms 

i ran same query , took more double time

lesson4=# select * books user_id = 5;  time: 1.397 ms 

note, there few rows in database. not sure if has impact.

did create index correctly? why running slower now?

your query little on 3 times slow. here's happening, assuming:

  1. the table 10 pages in length
  2. the table , index both fit in ram
  3. you pulling few records different pages, far less 10% of table.

the planner determines index might help, index scan. requires tree search through index, followed sequential scan through relevant pages.

without index, sequential scan through ten pages, find answer. not second faster, simpler.

this why on postgresql, indexes aren't magic answer performance problems. worth noting performance approaching danger zone before thinking adding index.


Comments

Popular posts from this blog

java - How to specify maven bin in eclipse maven plugin? -

single sign on - Logging into Plone site with credentials passed through HTTP -

php - Why does AJAX not process login form? -