Originally posted by csn
Can you give some examples of when you'd use partial indexes?
With MySQL, I think you can create indexes on x number of chars in a column. I think it applies to char types only, and I'm pretty sure MySQL doesn't have partial indexes.
Sure, a common example is in a system where 99% of all the content is approved, and 1% isn't. I.e. there's a boolean flag called approved, and with 150,000 online documents, maybe 100 or so are unapproved. In normal operation you'd put a bool in a small table and join it to whatever you need. If the average width of records in the table was say 10 or 20 bytes, then it'd be just about as fast to brute force it as it would be to read in an index on all those elements if it's not a partial.
create table test (article text, <bunches of other fields>, id serial);
create table test2 (status bool, tid int8 refernces test (id));
create index test2_status on test2 (status);
This would be a waste, as it would be not much faster than just scanning the small test2 table. but, this one would make a much smaller index:
create index test2_status on test2 (status) where status is false;
Since it will only have pointers to the false fields, it will be a fraction of the size of the index on every row in the column.
It's also useful if you have timely versus non-timely data. I.e. data that is in the archive state is a. I.e. if 90% of all you access are on data that is less than a week old, then you can create an index every night that only has the latest weeks or two worth of articles. so you could store 15,000,000 articles, but only index some tiny percentage that get hit the most. Heck you could even analyze frequencies of where clauses in the logs and see which things need partial indexing.