2024-07-12
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
The SQL language is divided into four categories: data query language DQL, data manipulation language DML, data definition language DDL, and data control language DCL.
1. Data Query Language DQL
The basic structure of the data query language DQL is a query block consisting of the SELECT clause, the FROM clause, and the WHERE clause: SELECT <field name table> FROM <table or view name> WHERE <query condition>
2. Data Manipulation Language DML
There are three main forms of data manipulation language DML:
1) INSERT
2) Update: UPDATE
3) Delete: DELETE
3. Data Definition Language DDL
Data Definition Language (DDL) is used to create various objects in the database - tables, views, indexes, synonyms, clusters, etc. For example: CREATE TABLE / VIEW / INDEX / SYN / CLUSTER | table view index synonym cluster. DDL operations are implicitly submitted! Cannot be rolled back
4. Data Control Language DCL
Data Control Language (DCL) is used to grant or revoke certain privileges to access the database, control the time and effect of database manipulation transactions, monitor the database, etc. For example:
1) GRANT: authorization.
2) ROLLBACK [WORK] TO [SAVEPOINT]: Roll back to a certain point. Rollback --- ROLLBACK rollback command returns the database status to the last submitted status. Its format is: SQL>ROLLBACK;
3) COMMIT [WORK]: Commit. When inserting, deleting, and modifying data in a database, the transaction is considered complete only when it is committed to the database. Before a transaction is committed, only the person operating the database has the right to see what has been done. Others can only see it after the transaction is finally committed.
Database authorization command:
GRANT <privilege> on table name (or column name) to user
Correct answer option B: insert,select: Permission table name: user User: nkw
Supplementary knowledge point - reclaiming permissions
REVOKE <privilege> on table name (or column name) FROM user
I believe everyone is familiar with the explain statement, which is used to view the execution plan. Explain is often used in SQL optimization analysis.
It is worth noting here that explain does not actually execute the statement, it just displays the execution plan.
What information can I see?
- EXPLAIN SELECT select具体语句
- 如:
- EXPLAIN SELECT * FROM userpro
Column Name | describe | Replenish |
id | Each SELECT keyword corresponds to an id | |
select_type | The query type corresponding to the SELECT keyword | |
table | Table Name | |
partitions | Matching partition information | |
type | Access methods for a single table | |
possible_keys | Possible indexes | |
key | The actual index used | |
key_len | The actual index length used | |
ref | When using an index column for equality query, the object information that matches the index column for equality | |
rows | Estimated number of records to be read | |
filtered | The percentage of remaining records after filtering by search criteria |
|
Extra | extra information |
1,id
Each SELECT keyword corresponds to an id
The larger the id value, the higher the priority, and the earlier it is executed.
If the id is the same, it can be considered as a group and executed sequentially from top to bottom.
Each ID number represents an independent query. The fewer the number of queries for a SQL statement, the better.
2,select_type
category | illustrate |
SIMPLE | Single table query, no subquery or UNION query. |
PRIMARY | The outermost SELECT statement in a query. |
SUBQUERY | A subquery is used in the WHERE clause. |
DERIVED | MySQL marks a subquery included in the FROM clause as DERIVED and generates a temporary table for its result set for use by the outer query. |
UNION | The second and subsequent query statements in a UNION query. |
UNION RESULT | The result set of a UNION query. |
DEPENDENT SUBQUERY | The result of the subquery depends on the value of the outer query, and the subquery is executed once for each row in the outer query. |
DEPENDENT UNION | The second and subsequent query statements of a UNION query whose results depend on the value of the outer query |
UNCACHEABLE SUBQUERY | Subqueries cannot be cached and are executed each time they are referenced. |
3. The table column represents the table name of the table (sometimes it is not the real table name, it may be an abbreviation).
4. partitions (optional)
5. type ☆
Common types are as follows
type | illustrate |
system | There is only one row in the table, usually SELECT ... FROM DUAL Query optimization. |
const | The query is found once through the index, with only one row of results (constant table). |
eq_ref | Use a unique index or primary key to identify a row from another table. |
ref | Use a nonunique index to locate one or more rows from another table. |
range | Returns a range of rows using an index. |
index | Fully scan the index to find the row, rather than scanning the entire table. |
all | A full table scan checks every row in the table. |
Generally try to avoid all
6, key and possible_keys
key:
key
The field shows the index actually used by the query. If the value of this field isNULL
, it means that no index is used. If this field has a value, it means that MySQL uses the specified index to execute the query.possible_keys:
possible_keys
The field shows a list of indexes that MySQL can use. These indexes are indexes that can be considered in the query, but may not be actually used. Usually,possible_keys
The indexes listed in are determined based on the query conditions and table structure.if key
The field has an index name, andpossible_keys
Multiple index names are listed in the table, indicating that MySQL has selectedkey
The fields are listed in the index to perform the query, while other indexes are listed inpossible_keys
It was considered, but ultimately not used.
if key
Fields areNULL
,and possible_keys
Multiple index names are listed in the query, indicating that MySQL did not use any indexes when executing the query, which may result in a full table scan or other non-index optimized access method.
7,key_len
key_len
It is a field that describes the length of the index key. It indicates the usage of the index and the length of the index key when MySQL executes a query using a specific index.
Single column index:
INT
),but key_len
The value is the length of the column.VARCHAR
),but key_len
The value is the maximum length of the field.Composite Index:
key_len
Indicates the total length of all columns in the index.Composite Index:
key_len
Is the total length of all columns in the composite index.Index prefix:
key_len
The length of the index portion actually used will be displayed.8,ref
ref
It is a field that describes the join conditions between tables or uses non-unique indexes for search. It indicates which join conditions or indexes MySQL uses to access tables when executing queries.
9,rows
rows
Is an estimate of the number of rows that are accessed or examined when executing a query.
SELECT
Inquire,rows
Indicates the estimated number of rows to be returned.JOIN
) or a subquery,rows
Probably indicates the number of rows accessed during a join operation.rows
Probably indicates the number of rows scanned.10,filtered
Indicates the percentage of rows filtered according to WHERE conditions and index conditions. Filtered reflects the optimizer's estimated query optimization effect.
when filtered
When it is close to 100%, it means that the query condition effectively filters out most of the rows that do not meet the condition, which is usually a good optimization indicator.
On the contrary, if filtered
A low value may indicate that the query condition is not precise enough or that the optimizer is not effectively using the index to filter data.
11,Extra
Additional Information Field
Here are some common Extra
Fields and their meanings:
Using index:
Using where:
Using temporary:
Using filesort:
Range checked for each record (index map: ...):
range
Query type.Full scan on NULL key:
Distinct:
Using join buffer (Block Nested Loop):
Impossible WHERE:
No tables used:
SELECT NOW()
。- CREATE TABLE s1 (
- id INT AUTO_INCREMENT,
- key1 VARCHAR(100),
- key2 INT,
- key3 VARCHAR(100),
- key_part1 VARCHAR(100),
- key_part2 VARCHAR(100),
- key_part3 VARCHAR(100),
- common_field VARCHAR(100),
- PRIMARY KEY (id),
- INDEX idx_key1 (key1),
- UNIQUE INDEX idx_key2 (key2),
- INDEX idx_key3 (key3),
- INDEX idx_key_part(key_part1, key_part2, key_part3)
- ) ENGINE=INNODB CHARSET=utf8;
- CREATE TABLE s2 (
- id INT AUTO_INCREMENT,
- key1 VARCHAR(100),
- key2 INT,
- key3 VARCHAR(100),
- key_part1 VARCHAR(100),
- key_part2 VARCHAR(100),
- key_part3 VARCHAR(100),
- common_field VARCHAR(100),
- PRIMARY KEY (id),
- INDEX idx_key1 (key1),
- UNIQUE INDEX idx_key2 (key2),
- INDEX idx_key3 (key3),
- INDEX idx_key_part(key_part1, key_part2, key_part3)
- ) ENGINE=INNODB CHARSET=utf8;
Prepare the data yourself.
Single table query
EXPLAIN SELECT * FROM `s1`;
Connection query
EXPLAIN SELECT * FROM `s1` INNER JOIN `s2`;
Subqueries
- EXPLAIN SELECT * FROM s1 WHERE key1 IN (SELECT key2 FROM s2 WHERE common_field
- = 'a');
- #Union 去重
- EXPLAIN SELECT * FROM `s1` UNION SELECT * FROM `s2`;
- #Union 全查
- EXPLAIN SELECT * FROM `s1` UNION ALL SELECT * FROM `s2`;
The last step is to remove duplicates, so a temporary table will be used for this. However, UNION ALL queries all data, so no temporary table query message will appear.
const (index is found once, only one row of results)
EXPLAIN SELECT * FROM s1 WHERE id = 10002;
eq_ref (use a unique index or primary key to find a row from another table)
EXPLAIN SELECT * FROM s1 INNER JOIN s2 ON s1.id = s2.id;
ref (use a non-unique index to retrieve one or more rows from another table)
EXPLAIN SELECT * FROM s1 WHERE key1 = 'a';
range (returns a range of rows using an index)
EXPLAIN SELECT * FROM s1 WHERE key1 IN ('a', 'b', 'c');
index (full scan of the index to find the row)
EXPLAIN SELECT key_part2 FROM s1 WHERE key_part3 = 'a';
Filtered decimal time
EXPLAIN SELECT * FROM s1 WHERE key1 > 'za' AND common_field = 'la'