Posts

Showing posts with the label composite index

Composite Index

Image
Introduction A Composite Index is an index type that contains two or more columns on the same table. It includes the following characteristics A Composite Index can be created for a maximum of 32 columns This is the default configuration according to the INDEX_MAX_KEYS constant of Postgres In practice, you should not create an index with more than 3 to 4 columns because it increases the index size, slowing down INSERT/UPDATE/DELETE operations The column order is sorted ascendingly by default However, unlike a standard index where you can sort ascending or descending at will, when using a Composite Index, you can only sort the columns all ascending, all descending or in the exact order specified at creation time For example, when using ON table (c1, c2) (default is all ascending), then You can query ORDER BY c1 ASC, c2 ASC You can query ORDER BY c1 DESC, c2 DESC But the index will not work with the query ORDER BY c1 ASC, c2 DESC Operation Order When using a Composite Index, you must ...

Partial Index

Image
Introduction Partial Index is an extremely powerful feature that allows the creation of an index on only a subset of a table's data, instead of indexing the entire table. This subset is defined by a filtering condition in a WHERE clause when creating the index. In essence, a Partial Index just adds a condition during index creation, so it can be used with all index types (such as B-Tree , Hash , GIN , GiST , ...) and supports all data types. Additionally, a Partial Index can also be used in combination with a Constraint , Expression Index and Composite Index . Advantages Space-saving: The index size is much smaller than a Full Index, which saves RAM and disk space. Increased Write performance (INSERT/UPDATE/DELETE): When adding or modifying data that does not satisfy the index condition, Postgres does not need to update the index tree. Disadvantages Queries must match the condition: The Postgres Query Planner only uses this index if the SELECT query has a WHERE clause that exactl...