Some summaries of database basic common knowledge indexes Mysql index optimization analysis

First, the three paradigms of relational databases

The first normal form requires that each column is indivisible, that is, to satisfy atomicity, which is also the most basic requirement in relational database design. The second normal form is based on its requirement that all non-primary key columns fully depend on the primary key columns, not only some of the primary key columns (if this is the case, the table should be split). The third normal form requires that all non-primary key columns must directly depend on the primary key column on the basis of the second normal form, instead of relying on a non-primary key column and then indirectly relying on the primary key column. For example, the user table has user id, user name, organization id, organization The third normal form is not satisfied when the name field is used, because the organization name is directly dependent on the organization id. However, this redundant design is actually used in relational database table design in order to reduce related table queries.

Second, the connection relationship between database tables

Common connection relationships between database tables are divided into inner joins, outer joins, left (right) outer joins, and cross joins. The inner join returns the records that meet the conditions in the two tables, and the outer join (full outer join) returns all the records in the two tables (including the records that meet the conditions, other left table records + right table fields are empty, other right table records + The left table field is empty), the left outer join is based on the left table, and returns all the left table records (the right table has no associated records and returns empty), and the cross join returns the product of the records on both sides.

Three, database index concept and precautions

The purpose of a database index is to increase the efficiency of query sorting of records and avoid full table scans to improve efficiency. Especially in the case of a large amount of table data, the correct use of indexes can greatly improve the query efficiency of the database. Usually the database index uses the B+ tree structure, and other similar commonly used index structures include Hash index and LSM tree index.

From the perspective of whether the order of records is consistent with the order of physical disks, indexes are divided into clustered indexes and non-clustered indexes. Clustered index means that the order of index records is consistent with the order of physical records, the query efficiency is higher, and it is more suitable for interval record query. Only one clustered index can be built for each table. By default, when the primary key of the table is created, it will be set to clustered Index (if you want to set other columns as a clustered index, set it before specifying the primary key).

Common index categories include ordinary index (common index), unique index (index with unique column value, allowing null values), primary key index (special unique index), singleton index, and joint index. There are the following considerations for using indexes:

  1. Avoid excessive use of indexes. If there are not many records in the table, or there are many duplicate records (such as gender columns), or if the table is frequently added, modified, or deleted, it is not recommended to use an index, because the effect of the index is not obvious and the maintenance cost is increased.
  2. Reasonable use of indexes, especially joint indexes, should place columns with more distinguishing records and query conditions in front of the joint index, to avoid the left joint index rule of MySQL database (assuming that the joint index columns are a, b, c, if It doesn't work without a column in the condition, PgSql doesn't seem to have this problem).
  3. Any operation on a column will result in a table scan, including database functions, calculation expressions, etc. When querying, move the operation to the right of the equal sign as much as possible.
  4. The in and or clauses often use work tables to invalidate the index; if a large number of duplicate values ​​are not generated, consider splitting the clause; the split clause should contain an index.

What situations need to be indexed:
1 Primary key, unique index
2 Fields that are often used as query conditions need to be indexed
3 Fields that often need sorting, grouping and statistics need to be indexed
4 Fields associated with other tables in the query, foreign key relationships need to be indexed

Under what circumstances do not create an index:
1. There are too few records in the table, and the data below one million does not need to create an index
. 2. Tables that are frequently added, deleted, and modified do not
need to create an index. such as.
4 Fields that are frequently updated are not suitable for creating indexes
5 Fields that are not used in the where condition do not need to create indexes

Some summary of the index

Mysql index optimization analysis

Four, three types of values ​​that should not be stored in relational database tables

  1. For large files, sound and picture files, although the database field has a blog type, in the end, it is better not to store the file directly into the database, but to store it in the corresponding file path. Because the operation speed through the database is not as fast as the direct operation of the file, it will increase the trouble of database file backup and migration data. Some netizens said that the image file column is stored in the MySQL database, although not including this field in the query will slow down the query efficiency.
  2. Temporary data, such as session, hourly, and daily data that will expire and be cleaned up, should not be stored in the database. It is more appropriate to store it in a cache such as Redis.
  3. For a large number of log files, if you want to store the logs in the database to facilitate log query, it is best to build a separate log library to avoid affecting the access efficiency of the main business table due to frequent log writing.

Guess you like

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