CREATE INDEX - define a new index

SYNOPSIS

 

CREATE [ UNIQUE ] INDEX name ON table [ USING method ]
    ( { column | ( expression ) } [ opclass ] [, ...] )
    [ WHERE predicate ]

DESCRIPTION Description

CREATE INDEX construct an index named index_name on the specified table. Indexes are primarily used to enhance database performance. But if the inappropriate use will result in slower performance.


 The index key field by way of field names declared or optional in a written inside the parentheses of expression. If the index method supports multiple field index, then we can declare multiple fields.


 Value of one or more fields of a field index may be used a table rows are calculated expression. Characteristics may be used to obtain the entire quick access to certain basic data distortion. For example, a function index on upper (col) would allow the clause WHERE upper (col) = 'JIM' to use an index.

PostgreSQL is provided from the B-tree index, R-tree, hash (hash) and GiST index method. B-tree index is a Lehman-Yao concurrency achieve high of B-trees. R-tree index method implements standard R-trees with the secondary Guttman split algorithm. hash (hash) index is a linear Litwin implemented hashing. Users can also define their own index methods, but that is fairly complicated.


 If the WHERE clause appears, it creates a partial index. Contains only a part of the index is part of the recording of the index table, this table is usually the most interesting part. For example, if you have a table, which contains the order has the account and not on the account, not on account of orders only a small part of the table and yet that is the most common part, then you can pass only in this section create an index to improve performance. Another possible use is a mandatory table WHERE with UNIQUE and uniqueness of a subset.


 In the WHERE clause with an expression can only refer to fields of the underlying table (but it can use all fields, not just the ones being indexed). Presently, subqueries and aggregate expressions are also forbidden in WHERE.


 All indexes defined in the functions and operators must be the immutable, (constant) That is, they must rely on the results of their input parameters, and must not rely on any external influences (such as a table of additional content or the current time). This restriction ensures that the behavior of the index is to define complete. To use a user-defined function in an index, remember to create it when you mark it as a function of the immutable.

PARAMETERS Parameters

UNIQUE

 So that when the system detects (if data already exist) and each time data is added duplicate values ​​in the table when the index was created. If the inserted or updated values ​​will generate an error will result in duplicate entries.
name

 Index name to be created. Schema name can be included here; the index is always in the same schema as its parent table.
table

 To index the table name (possibly schema-qualified).
method

 Name of the method for indexing. Optional name is btree, hash, rtree, and gist. The default method is btree.
column

 Column / field names of the table.
expression

 An expression of one or more fields based on the table. This expression usually must be written with surrounding parentheses, such as syntax display that. However, if there is an expression in the form of a function call, then the parentheses can be omitted.
opclass

 A table associated with the operator. See below for details.
predicate

 The constraint expression as a part of the index.

NOTES Note


 See `` Indexes '' for information about when to use an index, when not to use an index, and under what circumstances is useful information.


 Currently, only the B-tree and gist index method supports multi-column indexes. Up to 32 keys can declare default (this limit can be altered when building PostgreSQL). Only B-tree currently supports unique indexes.


 An operator may for each column the index / field declaration. The operator class identifies the index to be used for the operator of the column / field. For example, B-tree index of a four-byte integers would use int4_ops class; this operator includes comparison functions four-byte integers. In fact, the data type of the default operator of the field, will usually suffice. There are some data types operator classes is that they may be more than one meaningful order. For example, we might want to sort a complex type absolute value or by real part. We two operator for the data type definitions, and then select the appropriate table when the index is achieved. For additional information, operator table in `` Operator Classes '' and `` Interfacing Extensions to Indexes '' li.


 INDEX Use DROP [ drop_index (7)] to remove an index.

EXAMPLES Examples


 title field on the table films to create a B-tree index:

 

CREATE UNIQUE INDEX title_idx ON films (title);

Guess you like

Origin www.cnblogs.com/fanweisheng/p/11077536.html