Primary key, index, sql statement optimization, data type selection

Primary key, index, sql statement optimization, data type selection

1. Primary key, foreign key, unique key

Primary key: A rule for the integrity of database entities, which uniquely identifies an entity, and its value is non-empty and unique. For example, the student number of the student table.
Foreign key: A rule for the referential integrity of a database, which links two or more tables. The value must come from the value of the reference field of the reference table, which may or may not be empty. For example, the student number in the course selection table.
Unique key: Unique constraint can uniquely identify a single tuple in a relationship or table. Unlike the primary key, a table can have multiple unique keys. The unique key constraint can only accept a null value for the column; the unique constraint is also referenced by the foreign key of another table. It can be used when someone wants to enforce unique constraints on non-primary key columns and column groups.

#查询表结构,可以看到主键、外键、唯一键(如果有的话)
desc table;

The key in the table structure is MUL, and the value of the column can be repeated. The column is the leading column (the first column) of a non-unique index or is part of a unique index but can contain NULL

2. Index

Use indexes to quickly access specific information in a database table. For example, a query: select * from table1 where id=10000. If there is no index, you must traverse the entire table until the row with ID equal to 10000 is found; after having an index (it must be an index established on the ID column), you can search in the index, but the index needs to occupy physical space , It also reduces the speed of data maintenance, so not as many as possible.
Index classification: ordinary index, unique index, primary key index, multi-column index

#显示已有的索引
show INDEX FROM table1;

Primary key index: It is a special unique index that does not allow null values.
Unique index: The value of the index column must be unique, but null values ​​are allowed

#创建唯一索引
CREATE UNIQUE INDEX frame_num_index on payment_invoices_management(frame_num);
#创建多列索引
CREATE INDEX payment_framenum_invoicevalidity on payment_invoices_management(frame_num,invoice_validity);
#删除索引
ALTER TABLE payment_invoices_management DROP INDEX payment_framenum_invoicevalidity;

Covering query and non-covering query (frame_num is the primary key)
coverage query:

SELECT frame_num FROM payment_invoices_management WHERE frame_num='LRH03120000000001';

Non-covered query:

SELECT frame_num FROM payment_invoices_management WHERE invoice_number=666;

3. Sql statement optimization

a. Write as few SELECT * statements as possible, only return the required data, write WHERE clauses reasonably, and do not write SQL statements without WHERE. Try to do as little repetitive work as possible to merge some sql statements
b, try to avoid performing full table scans as follows: the
left fuzzy query'%...'
uses unequal operators! =
Or is used improperly, or both sides must have indexes.
In, not in
Where clauses perform expression operations on the fields.
For the created composite index (combined from the leftmost), the columns used in the query conditions must start from the left. interval. Otherwise, it is invalid. The structure of the composite index is similar to the
full-text index of the phone book : when an index is established for the file with thesaurus as the directory (the full-text index of the file is better than the fuzzy matching effect), it can be on the char, varchar, and text type columns Create a full-text index, MySQL 5.6 Innodb engine can also perform full-text index, search syntax: MATCH (column name 1, column name 2,...) AGAINST (search string [search modifier]), if the column type is a string, but in In the query, a numeric constant is assigned to a character column name name. Although there is an index on the name column, it is not used.
c. Use join instead of subquery
d, use union instead of manually creating temporary tables

4. Data type selection

Number type
Float and double selection (choose float as much as possible) to
distinguish between TINYINT / INT / BIGINT, it can be determined that fields that will not use negative numbers, it is recommended to add unsigned definitions. It is
possible to use number type fields to choose number types instead of string type
character types
The choice of char, varchar, TEXT: Do not use the TEXT data type as a last resort, fixed-length fields, it is recommended to use the CHAR type (fill in the blanks), and use VARCHAR for variable-length fields (automatically adapt to length, exceed the stage), and only set the appropriate Maximum length
Time type
Sort by priority of selection DATE (accurate to the day), TIMESTAMP, DATETIME (accurate to the time)
ENUM
For the status field, you can try to use ENUM to store it.
Avoid using NULL fields, it is difficult to query optimization and occupy additional index space

Guess you like

Origin blog.csdn.net/Krystal_RonghuiLi/article/details/107999397