Database common interview questions II:
1. Four characteristics Affairs
- Atomic: either executed or not executed, that is, can not be divided, it has a minimum.
- Isolation: All operations are all performed completely before another session can not see the process.
- Consistency: before and after the transaction, the total amount of data consistency.
- Durability: Once a transaction is committed, the change is permanent data
2. The database isolation level
- Dirty read: A transaction B reads the data transaction has not been submitted
- Non-repeatable read: inconsistent transaction data read twice
- Magic Reading: A modified transaction data, transaction B also modify the data, then the transaction A view, obviously modify the data, Why do not the same
Two kinds of engine difference 3.mysql
Engine Features
MYISAM not support foreign keys, table lock, insert the data, lock the entire table, when the number of rows in look-up table, the table does not require a full scan
time INNODB support foreign keys, line lock, number of rows in look-up table, a full table scan
4. B + index and the index in the hash index
Index difference
Hash hash index, high equivalent query efficiency, can not be sorted, you can not query the range
B + ordered data, range queries
The clustered index and non-clustered index
Index difference
clustered index data stored in index order, neutron node data stored real physical
non-clustered index pointer points to the real data stored in the line
6. The advantages and disadvantages of the index, when to use the index, when the index can not be used
- The biggest advantage is speed up the search index,
- The disadvantage is that the data update rate limitations, because at the same time to update the index
- Frequently queried data into the index, if you want to frequently change the data does not recommend the use of an index.
7.InnoDB index difference and the difference between MyISAM index One of the main index,
InnoDB data file itself is an index file. The MyISAM indexes and data are separated.
Is the difference between two secondary indexes: InnoDB secondary index field stores the corresponding record in the primary data
8. The implementation of the underlying index (B + tree, why not use red-black tree, B tree) Key
Tree distinguish
red-black tree to add, delete, black trees will be frequently adjusted, to ensure that the nature of red-black tree, a waste of time
B-tree is a tree B- B-tree, the query performance is unstable, the query results will not be the height of each the node pointer stored in the real data, compared to the B + tree for each layer of each element stored in the house more, it is a little higher.
B + B + tree Tree tree compared to the other two, it is shorter wider, shallower level query
9.B + realization of the tree, why the use of B + tree
A B + tree of order m has the following characteristics:
1. The intermediate node comprises k sub-tree k elements (B tree is k-1 elements), each element of data is not saved, the index only used, All data is stored in the leaf nodes.
2. All the leaf nodes contains information of all the elements, and these elements contain pointers to records, and the leaf node itself according to the size and large keyword brought sequentially linked.
3. All elements of the intermediate nodes exist in the child node, the child node elements is maximum (or minimum) element
Index lookup process will produce a disk I / O consumption, mainly to see the number of IO, and disk access principles related.
According to the definition of the B-Tree, retrieves the most apparent need to access nodes h. Designers clever use of the database system disk read-ahead principle,
the size of a node is set equal to a page, so that each node requires only one I / O can be fully loaded
locality principle and disk read-ahead
11.sql optimization
1.sql make use of the index, and the query index to go
2. sql statement optimization
subquery into the Join left
limit to optimize distribution, the first positioning usage ID, and then tab
or optimized conditions, or a plurality of conditions may be the result with a union all the combined (union all possible duplicate results)
unnecessary ordering
where instead of having, having finished retrieve all records, before filtering
to avoid nested queries
when a plurality of fields equivalence query, joint index
12. The classification index, and failure condition
Index type concept
general index of the most basic index, there is no limit
unique index and "ordinary index" similar, it is different: the value of the index columns must be unique, but allow free value.
Primary key index It is a special unique index, does not allow nulls.
Full-text index for large data, the full-text index is very time-consuming good space.
In order to further improve the composite index mysql efficiency can create a composite index, follow the "most left-prefix" principle
failure condition
condition is or, if it is wanted or conditions take effect, or each field to add an index
like the query, in% develop
internal function
to calculate the index column
is null will not use, is not null will use
14.varchar and char usage scenarios
Type usage scenarios
varchar character length often become
char with a fixed character length
15. The database three paradigms
1NF property inseparable
2NF non-primary key attribute, key attribute is fully dependent on the primary
3NF no transmission of non-primary key attribute-dependent
16. relational databases and non-relational database difference
Relational Database
advantage
- Easy to understand: a two-dimensional table structure is very close to the concept of a logical world, the relational model mesh relative to other models, such as the level is easier to understand;
- Easy to use: generic SQL relational database language makes the operation very convenient;
- Easy to maintain: a wealth of integrity (entity integrity, referential integrity integrity and user-defined) will greatly reduce the probability of inconsistent data redundancy and data;
- Support for SQL, can be used for complex queries.
- Support Services
Shortcoming
- In order to maintain the consistency of the huge price paid is the relatively poor performance of its read and write;
- A fixed table structure;
- High demand does not support concurrent read and write;
- It does not support the massive data efficiently read and write
Non-relational databases
- Using data stored key-value;
- distributed;
advantage
- Sql without parsed layer, high write performance
- Based on a key pair, there is no data coupling, easily extended
- Storing data format: nosql storage format is key, value form
Shortcoming
- It does not provide support sql
17. What are the lock, how plus exclusive lock select
Lock concept
optimistic locking themselves to achieve, by the version number of
pessimistic locking shared locks, multiple transactions can only be read but not write, plus lock in share mode
exclusive lock on a transaction, can only write, for update
row lock role of data rows in
a table lock as in table
18. how to solve deadlock
Find the process ID, kill process
19. leftmost matching principle
Leftmost matching principle is for the index
, for example: two fields (name, age) to establish a joint index, where age = 12 if this is the case, there is no use to the index,
where we can simply be understood as first on the name field the values are sorted, and then the data sorted age, if the direct age, then, when it is not used to index, the
query where name = 'xxx' and age = xx case, then, on the use of the index, and again Reflections under where age = xx and name = ' xxx' will use the sql index it,
in accordance with normal principles in terms of not to use, but the optimizer to optimize the position of the next exchange. The sql can also use the index to
Reference https://www.cnblogs.com/xiaofengwang/p/11246552.html