Indexing and Transactions of Database Notes

Index:
Before the index is created, the data in the database is stored according to the heap. When we use the index keyword to create an index, it will be stored in the memory as a B-tree. The default type of index created is a non-focused index, when the keyword clustered is used to create a focused index. A table can have only one focused index and can have multiple non-focused indexes.
The clustered index determines the physical storage order of table data, that is to say, the physical storage of the table is stored sequentially according to the clustered index structure, so a table can only have one clustered index.
Indexes other than clustered indexes are called non-clustered indexes. Non-clustered indexes are generally created to optimize specific query efficiency.


The difference between an index and a primary key: 1: The primary key is used to identify the uniqueness of database records, duplicate records are not allowed, and the key value cannot be empty, and the primary key is also a special index. 2: Only one primary key is allowed in the data table, but there can be multiple 3. Using the primary key, the database will automatically create a primary key index (the default is a focused index), or you can create an index on a non-primary key to facilitate query efficiency.
You can set it as a non-clustered index on the primary key, and on the remaining columns Set to clustered index.
A unique index means that no duplicate values ​​can be stored in that column.

An index is a special query table that a database search engine can use to speed up data retrieval. The index is very similar to the directory of a book in real life, and you can find the desired data without querying the entire book content. The downside is that it slows down data entry and also increases the size of the database.

Transactions
Transactions are groups of SQL statements that are bound together as a logical unit of work. If any statement operation fails, the entire operation fails, and subsequent operations will be rolled back to the state before the operation, or the previous node. Four properties of transactions: ACID. Namely atomicity, consistency, isolation and durability.
Dirty read: update table set X = X + 10 where col = A;
update table set X = X - 10 where col = B;
Suppose the first sql statement is executed at this time, and the database is updated at this time. The user can see the effect, but for some reason, the execution of the second SQL statement fails, and the transaction is rolled back. At this point, the user will see the original effect, which is dirty reading.
The READ COMMITED in the transaction isolation level can solve the dirty read phenomenon (using shared locks). According to its name, it can be seen that
there are three other isolation levels after committing the read transaction, which will not be discussed here.


What is the granularity of locks?
Database lock: locks the entire database, which usually occurs when the entire database schema is changed.
Table lock: Locks the entire table, which contains all data-related objects associated with the table, including the actual data row (each row) and keys in all indexes associated with the table.
Section lock: locks the entire section, because a section consists of 8 pages, so the section lock means that the lock controls the section, controls the 8 data or index pages in the section and all the data in these 8 pages Row.
Page lock: Locks all data or index keys in the page.
Row or row identifier: While technically a lock is placed on a row identifier, in essence, it locks the entire row of data.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325178082&siteId=291194637