![]() In any case, non-key columns duplicate data from the index's table and bloat the size of the index, thus potentially slowing searches. If an index tuple exceeds the maximum size allowed for the index type, data insertion will fail. It's wise to be conservative about adding non-key columns to an index, especially wide columns. Thus, addition of non-key columns allows index-only scans to be used for queries that otherwise could not use them. However, an index-only scan can return the contents of non-key columns without having to visit the index's table, since they are available directly from the index entry. A non-key column cannot be used in an index scan search qualification, and it is disregarded for purposes of any uniqueness or exclusion constraint enforced by the index. The optional INCLUDE clause specifies a list of columns which will be included in the index as non-key columns. Index name is required when IF NOT EXISTS is specified. Note that there is no guarantee that the existing index is anything like the one that would have been created. ![]() IF NOT EXISTSĭo not throw an error if a relation with the same name already exists. There are several caveats to be aware of when using this option - see Building Indexes Concurrently below.įor temporary tables, CREATE INDEX is always non-concurrent, as no other session can access them, and non-concurrent index creation is cheaper. When this option is used, PostgreSQL will build the index without taking any locks that prevent concurrent inserts, updates, or deletes on the table whereas a standard index build locks out writes (but not reads) on the table until it's done. Attempts to insert or update data which would result in duplicate entries will generate an error.Īdditional restrictions apply when unique indexes are applied to partitioned tables see CREATE TABLE. To use a user-defined function in an index expression or WHERE clause, remember to mark the function immutable when you create it.Ĭauses the system to check for duplicate values in the table when the index is created (if data already exist) and each time data is added. This restriction ensures that the behavior of the index is well-defined. The same restrictions apply to index fields that are expressions.Īll functions and operators used in an index definition must be “ immutable”, that is, their results must depend only on their arguments and never on any outside influence (such as the contents of another table or the current time). Presently, subqueries and aggregate expressions are also forbidden in WHERE. The expression used in the WHERE clause can refer only to columns of the underlying table, but it can use all columns, not just the ones being indexed. Another possible application is to use WHERE with UNIQUE to enforce uniqueness over a subset of a table. For example, if you have a table that contains both billed and unbilled orders where the unbilled orders take up a small fraction of the total table and yet that is an often used section, you can improve performance by creating an index on just that portion. A partial index is an index that contains entries for only a portion of a table, usually a portion that is more useful for indexing than the rest of the table. When the WHERE clause is present, a partial index is created. Users can also define their own index methods, but that is fairly complicated. PostgreSQL provides the index methods B-tree, hash, GiST, SP-GiST, GIN, and BRIN. ![]() For example, an index computed on upper(col) would allow the clause WHERE upper(col) = 'JIM' to use an index. ![]() This feature can be used to obtain fast access to data based on some transformation of the basic data. Multiple fields can be specified if the index method supports multicolumn indexes.Īn index field can be an expression computed from the values of one or more columns of the table row. The key field(s) for the index are specified as column names, or alternatively as expressions written in parentheses. Indexes are primarily used to enhance database performance (though inappropriate use can result in slower performance). CREATE INDEX constructs an index on the specified column(s) of the specified relation, which can be a table or a materialized view.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |