Job Road - Database

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/saber_wtq/article/details/96974696

Database lock can generally be divided into two categories, one is pessimistic lock, a lock is optimistic.

Optimistic locking latch mechanism generally refers to a user's own implementation, will not create a conflict under the assumption that general data, so be time to submit updated before formal conflict or not the data is detected in the data, if the conflict found , then let users return an error message, allowing users to decide what to do. Optimistic lock implementations typically include a version number and time stamp.

Pessimistic locking in general is what we usually say that database lock mechanism, the following discussion is based on pessimistic locking.

Pessimistic locking the main table locks, row locks, page locks. In only used in MyISAM table lock, there will be no deadlock, the lock overhead is very small, but very poor respective concurrent capacity. innodb implements row-level locks and table locks, lock granularity smaller, concurrent capacity strong, but the corresponding lock overhead increases, is likely to deadlock. Meanwhile inodb need to reconcile these two locks, the algorithm becomes complicated. InnoDB row lock is an index to retrieve the data entry lock on the index achieved only through index conditions through, only InnoDB row-level locking, otherwise, would use the InnoDB table lock.

Table and row locks are divided into shared locks and exclusive locks (exclusive lock), and update locks to solve the row lock promotion (shared lock to an exclusive lock) is the deadlock.

 

Subdivision row lock
shared lock
to lock and unlock: When a transaction execute select statement, the database system will allocate a shared lock to lock the data being queried for this transaction. By default, data is read, the database system shared lock is released immediately. For example, when a transaction executes a query "SELECT * FROM accounts" statement, the database system first lock the first row, after reading, unlock the first row, and then lock the second row. Thus, during a read operation in a transaction, while allowing other transactions to update accounts table rows not locked.

Compatibility: If you place a shared lock on the data resources, can then place shared locks and update locks.

Concurrent performance: good concurrent performance, when the data shared lock is placed, can then place a shared or update locks. So good concurrent performance.

Exclusive lock
to lock and unlock: When a transaction execute insert, update or delete statement, the database system will automatically SQL statements to manipulate data resources using exclusive lock. If the data resource has other locks (no lock) exists, it can not then place an exclusive lock.

Compatibility: exclusive locks and other locks can not be compatible, if the data resource has been added exclusive lock, you can not place the other locked. Similarly, if the data resources already placed other locks, then it can no longer be placed exclusively locked.

Concurrent performance: the worst. Only allows a transaction to access data locked, if other things are also required to access the data, you have to wait.

Update locks
update lock to lock in the initial stage of the resources it may have to be modified, which avoids the use of a shared lock deadlock caused. For example, for the following update statement:

UPDATE accounts SET balance=900 WHERE id=1

Update operation requires two steps: reading accounts table record id - 1> update operation is performed.

If you use a shared lock in the first step, then the second step to upgrade the lock to an exclusive lock on the deadlock may occur. For example: both transactions acquire a shared lock same data resource, and then must lock to an exclusive lock, but need to wait for another transaction before releasing the shared lock to an exclusive lock, which resulted in a deadlock.

Update lock has the following characteristics:

Lock and Unlock: When a transaction update statement, the database system will be assigned an update lock for the transaction. When reading data is completed, the updating operation is executed will update lock in an exclusive lock.

Compatibility: update locks and shared locks are compatible, that is to say, a resource can be placed simultaneously update locks and shared locks, but the place up an update lock. Thus, when multiple transactions update the same data, only one transaction can obtain an update lock, and then update lock to an exclusive lock, other transactions must wait until the end of the previous transaction to acquire update locks too, which avoids deadlock.

Concurrent performance: Allows multiple transactions simultaneously read a locked resource, but does not allow other transactions from modifying it.
 

 

index:

General index and unique index. MySQL is a general index of the basic index type, allows repeated insertion and null values ​​in the column definition index. The only index is a value index columns must be unique, but allow free value. If it is a combination of the index, the column value must be unique. Primary key index is a special unique index, does not allow nulls.

A separate index and combination index. It refers to a single column index contains only a single column index, a table can have multiple separate index. Composite index refers to the index created on multiple fields combination table, and only used the left of these fields in the query conditions, the index will be used. To follow most left-prefix set of index when used in combination.

Full-text indexing. Refers to a full-text index text search index defined in the column support value, which allows the insertion of duplicate values ​​and null values ​​indexed columns. Full-text index can be created on CHAR, VARCHAR, TEXT type column. Only MySQL MyISAM storage engine supports full-text indexing.

Spatial index. Spatial index is an index of spatial data types of fields established. MySQL spatial data types of four kinds, namely GEOMETRY, POINT, LINESTRING and POLYGON. MySQL uses SPATIL keyword expansion, making it possible to use a similar syntax to create regular indexes create a spatial index. You must create spatial index column declaration only create a spatial index in MyISAM storage engine table as NOT NULL.
 

 

SQL (struct query language Structured Query Language)

Function: data query, data definition, data manipulation, data control

 

Functional dependency:

  • Fully functional dependency: R & lt hypothesis (U) on a relational model is a conditional attribute set, X and Y is a subset of U. If X-> Y, and for any subset of X a X ', there are X' -> Y is not established, totally dependent on X called Y, referred to as X ---- F ---> Y
  • Part functional dependency:
  • Dependent transfer function: X-> Z, Z-> Y but Z-> X is not satisfied, then the transmission is dependent on X. Y

Paradigm:

  • The first paradigm 1NF: minimum data unit of each of the properties are in the relationship can not be divided. That property can not be divided.
  • The second paradigm 2NF: a first paradigm under the premise of meeting, and each non-primary attributes are totally dependent on the code R (any non-primary property does not depend on the code portion).
  • Third Normal 3NF: relation pattern R∈2NF, and each non-primary property is not dependent on the transmitted code of R.

 

Four characteristics of database transactions (ACID):

  • Atomic: (Atomicity): all operations of the firm include either all succeed or fail rolled back.
  • Consistency: (Consistency) transaction must convert the database from one consistent state to another consistent state. That is to say before and after executing a transaction execution must be in a consistent state. Take transfers, if both user A and user B money add up to a total of 5,000, no matter how transfers between A and B, turn several accounts, after the end of the transaction the money add up to two users should have 5000, which is transactional consistency.
  • Isolated: (Isolation): a firm to make changes before final submission, other transactions are missing. When a plurality of users concurrent access to the database, such as operating with a table, a database for each user transaction open, operation not being disturbed by other transactions, for isolation between a plurality of concurrent transactions.
  • Persistence: (Durability): Persistence means that once a transaction is committed, then changes to the data in the database is permanent, commit the transaction will not be lost even in the case of a database system experienced a failure of the operation .

Isolation level:

 

  • Read Uncommitted (read uncommitted [add Chinese Interpretation] content): the lowest level of isolation, nothing needs to be done, a transaction can read the results of another uncommitted transactions. All problems will occur concurrent transactions.
  • Read Committed (read submission): only after the transaction is committed, it will update the results seen by other transactions. You can solve the problem of dirty read.
  • Repeated Read (Repeatable read): in one transaction, read the results for the same data is always the same, regardless of whether there are other matters to this data whether the operation, as well as the transaction commits. Can solve dirty reads, non-repeatable reads.
  • Serialization (serialization): Transaction serialization execution, isolation highest level, at the expense of concurrency system. You can solve all the problems of concurrent transactions.

Concurrency problems caused by:

  • Lost update (lost update): Transaction T1, T2 update the same data. T2 in T1 will be updated before the data is written back, read data, will result in lost T2 T1 update the data. Concurrent write collision caused become write - write conflicts (write-write conflict)
  • Reads dirty data (dirty read): Transaction T1 T2 a read transaction data updated thereafter T1 for some reason has been revoked, the data is updated restore the original value T1, T2 at this time the read data in the database data inconsistencies. Read dirty data is a transaction reads another transaction have not yet submitted data caused, also known as read - write conflicts.
  • Reading irreproducibility (unrepeatable read): Transaction T1 read the same data twice, during the two reading, transaction T2 perform the update operation, so that T1 can not be reproduced before a read result. Also due to read - write conflicts arising. 

blockade:

  1. Exclusive lock (write lock locks X): Before a data object of the transaction T R plus X lock, only read and modify T R, unlock the data object R in T, then the transaction is not allowed to the other data object Add any type of lock. Exclusive: exclusive
  2. Shared lock (read lock locks S): a data object of the transaction T R plus S locks, read only T R R but not modified, other transactions can lock the R plus S, S in all but the R No other matters and then the X plus R lock before the lock is released.

Locking protocol:

  1. A locking protocol: to solve the problem of lost updates
  2. Two locking protocol: read dirty data to solve problems
  3. Three locking protocol: to reproduce the problem can not be solved reading

Problems caused by the blockade:

  • Livelock: distribution system lead to a transaction is always in a wait state, the opportunity to get blocked, solution: first come, first served, change the priority
  • Deadlock:

Deadlock Prevention:

 

Sql must know the language will be

  • Analyzing empty or not, and generally with IS NULL and IS NOT NULL == NULL,! = NULL like the result is always returned rows 0, and does not report a syntax error. Since NULL means Unknow so for arithmetic operators always returns false.
  • group by clause in the select list of all columns in any one of a non-polymeric group by expression should be included in the class table, or group by expression selection list must match the expression.
  • Where clause with the difference that the role of the object having clauses are not the same, where clause applied to the base table or view, select tuples satisfying the condition. having applied to the phrase group, choose tuples satisfying the condition.

 

Index (only discuss the MySql database):

  • :( clustered index primary key index) data line and the adjacent key (index) is stored close together and find the key (index) will find the data.
    Because there is no data rows are stored in two locations, so a table can have only one clustered index.
     
  • Non-clustered index :( unique index, full-text index, the general index) data storage and indexing stored separately from the leaf nodes of the index usually only keep the primary key. The need for secondary search.

Respective advantages:

  • Because the clustered index data are stored together, to reduce the IO operations
  •  

Query using aggregation index more efficient than non-clustered index higher efficiency, but if you need to change frequently clustered index value, write performance is not high because of the need to physically move the corresponding data position.

How to solve the problem of secondary non-clustered index query

Composite Index (covering index)

The establishment of more than two of the index, you can query the data in the columns of a composite index without the need for secondary queries back to the table, such as index (col1, col2), execute the following statement

MySQL in the primary key is the clustered index

To pay attention to the use of a composite index required to meet the principles of the leftmost index, that is, when queried if the conditions where there is no one to many of the leftmost column, the index will not work.

Guess you like

Origin blog.csdn.net/saber_wtq/article/details/96974696