A Index
1. What is an index?
An index is a structure that sorts the values of one or more columns in a database table.
In a relational database, an index is a database structure associated with a table, which is a fact. It allows for faster operation of the table's select and so on, which is equivalent to a book's directory.
For a table, if we want to find a record of a column that matches a particular value, the first method is to search the whole table, match, and then list all the records that match, but doing so consumes a lot of database system time and creates a lot of disk I/O operations, and the second is indexing the table. The index values that match the query criteria are then found in the index, and the corresponding records in the table are found quickly by ROWID (equivalent page numbers) saved in the index.
An index is a separate, physical database structure that is a collection of one or more column values in a table and a logical pointer list corresponding to the data pages that physically identify those values in the table. This means that the index consumes the database space.
Not all databases use the index in the same way. As a general rule, you need to create an index on a table only when you frequently query the data in an indexed column. Indexes consume disk space and reduce the speed at which rows are added, deleted, and updated. In most cases, the speed advantage of indexing for data retrieval greatly outweighs its shortcomings. However, if your application updates data very frequently or if disk space is limited, you may want to limit the number of indexes.
You can use a single column as an index, or you can use a multiple-column union as an index.
2. Advantages and disadvantages of indexes
Advantages:
(1) Greatly accelerate the retrieval speed of data;
(2) Create a unique index to ensure the uniqueness of each row of data in the database table;
(3) Accelerating the connection between the table and the table;
(4) When using grouping and sorting clauses for data retrieval, you can significantly reduce the time to group and sort in a query.
Disadvantages:
(1) The index needs to occupy the physical space.
(2) When the data in the table is added, deleted and modified, the index should be maintained dynamically, reducing the maintenance speed of the data.
Considerations when building an index:
(1) Consider the number of indexes that have been created on the table. It's best to avoid having a lot of indexes on a single table
(2) Check the definition of an index that has been created on the table. It is best to avoid overlapping indexes that contain shared columns
(3) Check the number of unique data values in a column and compare the number to the number of rows in the table. For example, if there is a 1000w record, a field is gender, only male, female. That means that half of the records are duplicated, so consider whether it is necessary to index them.
3. Some index categories
(1) General index
This is the index without any restrictions.
(2) Unique index
An index that does not allow rows with the same index value, thereby prohibiting duplicate indexes or key values. When the index is created, the system checks for duplicate key values and checks each time the INSERT or UPDATE statement is used to add data.
(3) Primary key index
Database tables often have one column or column combination whose values uniquely identify each row in the table. This column is called the primary key of the table. Its commonality with a unique index is that it is not allowed to have duplicate records, except that a unique index is not limited to NULL, that is, it can have more than one null value inserted, but the primary key cannot be empty.
(4) clustered index and non-clustered index
A clustered index is also called a cluster index, which is a sort of re-organization of the actual data on disk to be sorted by the value of one or more columns specified. Because the index page pointer to the clustered index points to the data page, finding data using a clustered index is almost always faster than using a nonclustered index. Only one clustered index can be built per table, and a clustered index requires at least the additional space of the table 120% to hold a copy of the table and an intermediate page of the index.
Clustering is to find the physical storage location of data according to the code value, so as to achieve the purpose of retrieving data quickly. The order of the Oracle clustered index is the physical storage order of the data, and the leaf node is the data node. The order of non-clustered indexes is independent of the physical order of the data, and the leaf nodes are still index nodes, but there is a pointer to the corresponding data block. Because clustered indexes are sorted by index, a table can have at most one clustered index, but multiple columns are available.
A clustered table in Oracle refers to two tables with a field that is exactly the same, and the two tables are often connected by this field in the business, when the cluster table is established,
Two tables common one field, can reduce occupy space, and can significantly improve connection query speed.
There are practical examples of these two articles, which are not discussed in depth.
The idea of building clustered index
1, most tables should have clustered index or use partition to reduce the competition for the end of the table, in a high transaction environment, the last page of the blockade severely affect the throughput of the system.
2, in the clustered index, the data is physically ranked on the data page, duplicate values are also grouped together, so that in those containing the scope of the check (between, <, <=, <>, >=) or use group by or by the order of the query, Once a row with the first key value in the range is found, rows with subsequent index values are guaranteed to be physically contiguous together without further searching, avoiding a wide range of scans, which can greatly improve query speed.
3. When a clustered index is established on a table with frequent insert operations, do not build on a column with a monotonically rising value (such as identity), or it will often cause a blocking conflict.
4. Do not include frequently modified columns in the clustered index, because the data rows must be moved to a new location after the code value has been modified.
5. Select the clustered index should be based on the WHERE clause and the type of the join operation.
For specific clustered indexes, refer to the following articles:
Http://blog.sina.com.cn/s/blog_607b68cc0100f5jo.html
http://space.itpub.net/9778796/viewspace-660186
Two. Key
1. What is a key?
A key, also known as a keyword, in a database is an important concept in a relational model, which is a logical structure, not a physical part of a database.
2. Unique key
Unique key, which is one or a set of columns with no duplicate records. A record can be uniquely marked.
3. Primary Key
is a unique key, which is a special unique key. The difference is that the primary key cannot be empty.
4. Foreign KEY
If the Public keyword is the primary key in a relationship, then this common keyword is called the foreign key of the other relationship. Thus, the foreign key represents the connection between the two relationships. A table with a foreign key of another relationship as the primary key is called the primary table, and a table with a key in it is called the primary table from the table. Foreign keys are also referred to as foreign keywords. In other words, if a property set in relationship mode R is not the primary key of R, but the primary key of another relationship R1, the attribute set is the foreign key of the relational mode R, which is usually abbreviated as FK in the database design.
foreign keys are basically not used in development, mainly the database is used to guarantee the integrity of the data
for a simple example
Table A has columns C1
Table B has columns C2
Point C2 foreign key to C1
So when you insert data into table B, The contents of C2 must be one of the
in C1 and a few constraints require you to set
How to handle the associated data in table B when you delete or modify a field in Table A
5. Parent key
for 2 tables with foreign key relationships, tables referenced by foreign key tables are called primary tables, while tables with foreign keys are called from tables.
Three Constraints
Like the primary key, unique and so on is actually a kind of constraint.
Where the primary KEY constraint is more restrictive than the unique constraint and cannot be null.
Four The difference between keys, indexes, and constraints
Generally, we see the term "index" and "key" exchanged for use, but actually these two are different. An index is a physical structure stored in a database, and a key is purely a logical concept. Keys represent the integrity constraints created to enforce business rules. The confusion of indexes and keys is usually due to the fact that the database uses indexes to enforce integrity constraints.
(1) What is the relationship between primary key index and primary key?
The primary key index is the index that the system automatically creates when the primary key is created, the primary key requirement is not duplicated, not empty, but how does he determine the efficiency? Of course it's indexed, and it's not crazy to walk through the table all the time.
Therefore, establishing the primary key will automatically establish the primary key index.
(2) The difference between a primary key and a unique key is that the unique key can be null and the primary key cannot be
(3) What is the difference between establishing a unique constraint and a unique index?
Similarly, when you create a unique constraint, you automatically create a unique index. Establishing a unique index can be said to be the only constraint of a means.
Basically, there is no difference in implementation. If you can't understand it, just be nice.
(4) What is the difference between clustered and non-clustered indexes?
This has been made up in detail, and there are two articles, this is not said.
(5) What is the difference between a constraint and a primary key?
Constraints generally have primary key constraints, FOREIGN KEY constraints, unique constraints, and so on.
Primary Key,foreign Key,unique, respectively, where the primary KEY constraint is only one of the constraints.
In fact, they are different concepts of things.
Reprint Address: http://blog.csdn.net/dlodj/article/details/7017331
indexes, keys, and constraints in a database