3 ms·
This entirely depends on what kind of index is used. A sorted index, such as a B or B+ tree (used in many SQL databases), will allow for fast point/range lookup
by pushrax 5y ago
This entirely depends on what kind of index is used. A sorted index, such as a B or B+ tree (used in many SQL databases), will allow for fast point/range lookups in a continuous value space. A typical inverted index or hash based index only allows point lookups of specific values in a discrete value space.
- jugg1es 5y agoIt was a sorted BTREE index in MySQL 5.x. I agree that its supposed to be fast but it just wasn't for some reason.
- pushrax 5y agoAre you sure it was actually using the index you expected? There are subtleties in index field order that can prevent the query optimizer from using an index that you might think it should be using. One common misstep is having a table with columns like (id, user, date), with an index on (user, id) and on (date, id), then issuing a query like "... WHERE user = 1 and date > ...". There is no optimal index for that query, so the optimizer will have to guess which one is better, or try to intersect them. In this example, it might use only the (user, id) index, and scan the date for all records with that user. A better index for this query would be (user, date, id).
- Izkata 5y agoOne of the more bizarre things we'd found in MySQL 5.something was that accidentally creating two identical indexes significantly slowed down queries that used it. I wouldn't be surprised if you hit some sort of similar strange bug.
- whoknowswhat11 5y agoI was just going to make this comment. Postgres as far as I know uses B-tree by default. You can switch sort order I think for this as well, so "most recent" becomes more efficient. Multi-column indexes also work, if you are just searching for first column postgres can still use multi-column index.