当前位置:网站首页>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
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
3. Right outer connection query
SELECT d.dept_name, e.* FROM t_employee e RIGHT JOIN t_dept d ON e.dept=d.id
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
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
What is the index ?
- Greatly reduce the amount of scanning required by the server .
- Help server avoid sorting and temporary tables
- 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 .
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
- When using indexed columns , Try not to use expressions .
- Try to use primary keys , Self increasing ( The leaves split ) It doesn't go back to the table .
- Put a string before x Bit create index .
- remarks 、 Or the Chinese name field should not be indexed . For example, the discrimination of remark information is not high , Do not add index .
- Use index scan to sort .( Cost performance )
- topic : use 256M Memory handle 1T File sorting .key_Length Calculation method .
- where In conditional query , Try not to use intermediate parameters > or <
- Cast , Full table scan .( Integer to string conversion , Implicit conversion )
- Total number of rows is greater than 80%, Need to index . If it is less than, there is no need to build an index .
- The columns that create the index , Not allowed to be empty .Mybatis Pit handle 0 When it comes to null.
- When the meter is connected , Not more than 3 A watch .( Minimize data redundancy )
- limiit
- Number of single table indexes 5 Within a .
- The number of multi column single index fields shall not exceed 5 individual .
- 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
边栏推荐
- Notes on the development of raspberry pie (16): Raspberry pie 4b+ install MariaDB database (MySQL open source branch) and test basic operations
- 模型搭建过程1==MIIDock
- fitfi运动赚钱链游系统开发模式详情
- 抖音如此重视直播销售外卖套餐,会不会是创业者巨大机会?
- [tcapulusdb knowledge base] tcapulusdb tmonitor module architecture introduction
- Ue5 small knowledge points geometry script modeling
- 22、ADS使用记录之E类功放设计(下)
- Will it be a great opportunity for entrepreneurs for Tiktok to attach so much importance to live broadcast sales of takeout packages?
- fastapi 如何响应文件下载
- TS进阶之条件类型
猜你喜欢
(一)爬取Best Sellers的所有分类信息:爬取流程
VSCode 如何将已编辑好的文件中的 tab 键转换成空格键
camunda如何使用script脚本节点
State compression DP example (traveling salesman problem and rectangle filling problem)
[tcapulusdb knowledge base] Introduction to tmonitor system upgrade
Mac MySQL installation tutorial
CPU的分支预测
[tcapulusdb knowledge base] tcapulusdb tmonitor module architecture introduction
socket编程(中)
[ROS] moveit rviz seven DOF Manipulator Simulation
随机推荐
TS进阶之条件类型
欧拉函数和线性筛求欧拉函数
Lvgl Library Tutorial 01- porting to STM32 (touch screen)
[tcapulusdb knowledge base] tcapulusdb doc acceptance - table creation approval introduction
The road of ospo construction of Weibo: how to promote enterprise open source through ospo construction?
【sql语句基础】——查(select)(单表查询顺序补充)
Digital DP example
【TcaplusDB知识库】Tmonitor单机安装指引介绍(二)
break algorithm---dynamic planning(dp-arr)
Discord机器人开发
kubernetes 部署 ActiveMQ
camunda如何使用script脚本节点
Audio and video technology development weekly 𞓜 249
论文翻译 | PointNet: Deep Learning on Point Sets for 3D Classification and Segmentation
Determine the maximum match between bipartite graph and bipartite graph
89C51 single chip microcomputer driving LCD based on dream
Multiplicative inverse action
[tcapulusdb knowledge base] Introduction to tmonitor stand-alone installation guidelines (I)
Tamidog knowledge | a comprehensive analysis of the meaning and role of mergers and acquisitions of state-owned enterprises
Gauss elimination for solving N-element equations