5 ms·
SQLite – Partial Indexes
- omniscient_oce 4y agoI had never heard of these... Thanks for sharing!
- jitl 4y agoPartial indexes are also very handy for wide tables in Postgres.
- remram 4y agoI'm sure this is news to many and I won't claim that this doesn't belong on HN (if it's upvoted then it's useful), but I feel it's important to point out that this is not a recent development: > Partial indexes have been supported in SQLite since version 3.8.0 (2013-08-26).
- pstuart 4y agoSQLite is karma fodder here. I'm a huge fan of it but yes, it's not clear why this is worthy of the front page.
- omniscient_oce 4y agoIt does at least spawn interesting conversation!
- thunderbong 4y agoIt was news to me. Realized that it's also there in Postgres. https://www.postgresql.org/docs/current/indexes-partial.html https://www.postgresql.org/docs/current/indexes-partial.html
- mceachen 4y agoAh! I use these all the time with PhotoStructure--it's great for "work-in-progress" rows that shouldn't be shown to the user quite yet (but I need the row added as I need to populate join tables with the primary key), or rows that are no longer relevant (due to files disappearing or becoming unreadable), but might return, so I'm not wanting to delete it quite yet. The syntax looks like: CREATE INDEX ON Asset (capturedAt) WHERE visible = 1; And (like with any index and any RDBMS), you have to remember to include that column in any relevant WHERE clause for the index to be considered relevant.
- didgetmaster 4y agoAny ideas why an index in SQLite would make a query slower? I have a 22 column table created with the city of Chicago crime data loaded (7.6M rows). There is a block column that has the block within the city where the crime occurred (about 58K unique block names). If I want to find all the blocks that start with a 1, I use 'SELECT block FROM crimes WHERE block LIKE '1%'; which returns about 370K rows in about 1600 ms. If I then create an index on the block column, the same query takes about 1800 ms (about 200 milliseconds longer). I ran each query 5 times and the queries with the index are always slower. I am much more familiar with Postgres and creating an index on the same table there always speeds things up quite a bit.
- yread 4y agoSometimes SCAN is faster than SEARCH. For example if the query returns a significant percentage of all values.
- didgetmaster 4y agoI am aware of that, but in this case the query returns less than 5% of the total rows and I think the database should choose to ignore the index if a scan is faster.
- skissane 4y agoCould it have something to do with the data type of your block column? (Check with typeof() function.) Just guessing, but I would expect LIKE ‘1%’ to perform very differently for string vs numeric data. Originally SQLite had no types for (most) columns - but it still had types for the values. Now with STRICT tables the columns can have types too.
- didgetmaster 4y agoThe block column is of type TEXT which should be fastest for LIKE '1%'
- prirun 4y ago