当前位置:网站首页>Interview questions MySQL database

Interview questions MySQL database

2022-06-13 11:42:00 Lyndon1107

Multi table associated query

Relational query sql The idea of writing :1. First determine the connected table ;2. Determine the field to query ;3. Determine the connection conditions and connection methods .

topic

Suppose there is t_employee( The employee table ) and t_dept( Departmental table ) For example :
t_employee In the table dept On behalf of the employee's department
 Insert picture description here
1. Query the employee name and its corresponding department name ( There is no department , If there is no employee in the Department, it will not be displayed )
2. Check the names of all employees and their departments ( No department is displayed as null)
3. Query all department names and their corresponding employee information ( No employee displayed null)
4. Query the information of all employees and all departments ( No departments or employees are displayed null)
5. Check the names of employees and their superiors

1. Internal connection query

SELECT e.emp_name, d.dept_name FROM t_employee e INNER JOIN t_dept d ON e.dept=d.id
SELECT e.emp_name, d.dept_name FROM t_employee e, t_dept d WHERE e.dept=d.id

2. Left outer connection query

SELECT e.emp_name, d.dept_name FROM t_employee e LEFT JOIN t_dept d ON e.dept=d.id
 Insert picture description here

3. Right outer connection query

SELECT d.dept_name, e.* FROM t_employee e RIGHT JOIN t_dept d ON e.dept=d.id
 Insert picture description here

4. All external connection query
SELECT e.*, d.* FROM t_employee e LEFT JOIN t_dept d ON e.dept=d.id
UNION
SELECT e.*, d.* FROM t_employee e RIGHT JOIN t_dept d ON e.dept=d.id
ps: stay oracle in , Use... Directly full outer join Just connect two tables with keywords 

 Insert picture description here

5. Self connect query

Self join query is the join query between the current table and itself , The key point is to virtualize a table to an alias .
SELECT e.emp_name, d.emp_name FROM t_employee e LEFT JOIN t_employee d ON e.boss_id = d.id
 Insert picture description here

What is the index ?

  1. Greatly reduce the amount of scanning required by the server .
  2. Help server avoid sorting and temporary tables
  3. Random IO Into order IO, Reduce track addressing time

Index type ? Primary key 、 only 、 Ordinary 、 The full text 、 Composite index

B+Tree

B+Tree yes B-Tree Extension of contract
Underlying data structure

There are several indexes in a table B+ Trees .

How many indexes can there be in a table ?

Look at the execution plan .id key,keylength Extral
key Represents whether the index will be used in the current query .
type: Access type .const,system,ref,range,all

Several copies of actual data are stored in a table ?

When inserting data in a table , Must be bound to an index to store .
The index is selected in the order of having a primary key and using a primary key , There is no primary key but a unique key . If there is no unique key, use 6 Bytes of rowid.
Other indexes B+ The leaf node in the tree structure is the data binding index key value .
 Insert picture description here

Cluster index 、 Nonclustered index

Cluster index : Index stored with data binding , Such as id.
Nonclustered index : An index stored separately from data , Such as name.
InnoDB Is it clustered or non clustered
InnoDB There is at least one clustered index , There can be multiple non clustered indexes .
If there is no index , The system generates a row_id.
.frm Table structure ,.ibd Data in the + Index file .

2.MyISAM Are non clustered indexes ..frm .myd .myi

Why use int type id Self increasing good

because InnoDB The default primary key of the engine is cluster index , The physical storage order of the cluster index data is consistent with the index order , As long as the index is adjacent , Then the data is also on the adjacent disks .(int type id Self increasing , This is related to the data structure of the database index .)

Index optimization

1 Back to the table

The index value is found according to the normal column , Go back to the primary key to find .

6 Bytes of role_id

2 Index overlay (select *)

select id, name FROM Student WHERE id=1( There is no need to return the form )
select * FROM Student WHERE id=1( We need to go back to the table )

3 The leftmost match

The optimizer automatically matches
Enforcement

4 Index push down

Processing in the storage engine

OR Can you walk the index ?

answer : The primary key will .

Optimize the details

  1. When using indexed columns , Try not to use expressions .
  2. Try to use primary keys , Self increasing ( The leaves split ) It doesn't go back to the table .
  3. Put a string before x Bit create index .
  4. remarks 、 Or the Chinese name field should not be indexed . For example, the discrimination of remark information is not high , Do not add index .
  5. Use index scan to sort .( Cost performance )
  6. topic : use 256M Memory handle 1T File sorting .key_Length Calculation method .
  7. where In conditional query , Try not to use intermediate parameters > or <
  8. Cast , Full table scan .( Integer to string conversion , Implicit conversion )
  9. Total number of rows is greater than 80%, Need to index . If it is less than, there is no need to build an index .
  10. The columns that create the index , Not allowed to be empty .Mybatis Pit handle 0 When it comes to null.
  11. When the meter is connected , Not more than 3 A watch .( Minimize data redundancy )
  12. limiit
  13. Number of single table indexes 5 Within a .
  14. The number of multi column single index fields shall not exceed 5 individual .
  15. myth :① The more indexes, the better ;② Premature optimization

Explain Implementation plan

topic : use 256M Memory handle 1T File sorting ?

Reference address

once MySQL Index interview
https://mp.weixin.qq.com/s/65V-htzeAU1ACBvga8MlaQ
What is the pessimistic lock and the optimistic lock ?
https://mp.weixin.qq.com/s/I5hHbSeJZlpGcRhV1vOBnw

Why index can improve query speed ?
https://mp.weixin.qq.com/s/BTCZikV60bZPWCtZ1to6Qg

原网站

版权声明
本文为[Lyndon1107]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/164/202206131131562832.html