


Meituan interviewer: Explain clearly the MySQL structure system and make an offer immediately
Story
Continue to share with you, I will The technical questions encountered during the interviews at Meituan in Shanghai were so-so at that time. They could not say bad or good, but they did not give the interviewer a pleasant feeling anyway.
It’s bad, it’s a lot of things. I usually feel fine, but once it’s time for the interview, I can’t think of anything.
Although, I have been developing Java
for almost five years (2017), and I have used Oracle
Database (the system in the bank), but most of the time I use the MySQL
database, but I am still confused when faced with this question (I bit the bullet and made some mistakes), and I thought the interviewer You need to ask about indexing, slow queries, performance optimization, etc. (because these are all interview questions found online and memorized). <figure data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;display: flex;flex-direction: column;justify-content: center;align-items: center;"><img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/001/273/727/6b0135fbb00d408ab4d424ca864e24bb-0.png?x-oss-process=image/resize,p_40" class="lazy" alt="Meituan interviewer: Explain clearly the MySQL structure system and make an offer immediately" ><figcaption style="max-width:90%"><br></figcaption></figure><p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;line-height: 26px;margin-top: 1px;margin-bottom: 1px;">Today we will talk about the architecture system of <code style="font-size: 14px;padding: 2px 4px;border-radius: 4px;margin-right: 2px;margin-left: 2px;background-color: rgba(27, 31, 35, 0.05);font-family: " operator mono consolas monaco menlo monospace break-all rgb>MySQL
, even though we are Java
developers , but in the daily development process, I often deal with the MySQL database. It would be slightly better if the company has a DBA
that can do something. If there is no DBA
or the DBA
is of no use, we still need to understand## The entire system of #MySQL, and meeting him in the interview is also a plus.
SQL is queried, you only need to understand the entire system of
MySQL before you can say it. 123.
MySQL.
MySQL as a software system we develop. Since it is a software system, there is an architecture diagram and the architecture is How it is layered and what is the function of each layer.
What is MySQL?
MySQL
is a relational database management system developed by the SwedishMySQL AB
company. Currently Belongs toOracle
company.MySQL
is a relational database management system that stores data in different tables instead of putting all data in a large warehouse, so that Increased speed and improved flexibility.MySQL
is open source, so you don’t need to pay extra.MySQL
supports large databases and can handle large databases with tens of millions of records.MySQL
Use standard SQL data language form.MySQL
can be used on multiple systems and supports multiple languages. These programming languages include C, C, Python, Java, Ped, PHP, Eifel , Ruby and TCL, etc.MySQL
has good support for PHP, which is currently the most popular web development language.MySQL
Supports large databases and data warehouses with 50 million records. 32-bit system table files can support up to 4GB, and 64-bit systems support the largest table files. is 8TB.MySQL
is customizable and adopts the GPL protocol. You can modify the source code to develop your ownMySQL
system.
Please pay attention to the spelling of
MySQL
. In addition, many people may have questions, why is the logo ofMySQL
a dolphin?
Let’s take a look at the overall architecture diagram of MySQL
.
MySQL Architecture Diagram


MySQL be layered?
The answer is: Yes, let’s talk about the layering of MySQL
and the functions of each layer.
Architecture diagram layering
We can split the above architecture diagram and make a brief illustrate.
Connection layer

JDBC that we are developing in
Java.
gateway layer in our project? If you are not familiar with the gateway, then we can understand my
controller layer.

Connection pool is mainly responsible for storing and managing the connection between the client and the database. One thread is responsible for managing one connection. Since the introduction of the connection pool, official reports: When the number of database connections reaches 128, the performance of using the connection pool and without the connection pool is improved by n times (anyway, the performance is greatly improved).
After the connection is established, you can execute the select statement. The execution logic will first come to the cache module.
Cache
After MySQL gets a query request, it will first go to the query cache to see if this statement has been executed before. Previously executed statements and their results are stored in memory in the form of key-value pairs. The key is the query statement, and the value is the query result. If your query can directly find the key (hit) in this cache, then the value will be returned directly to the client.
If there is a miss in the cache, the subsequent execution phase will continue. After the execution is completed, the execution results will be stored in the query cache. As you can see here, if the query hits the cache, MySQL can directly return the result without performing subsequent complex operations, which is very efficient.
But in most cases I would recommend you not to use query caching, why? Because query caching often does more harm than good.
The query cache fails very frequently. As long as a certain piece of data in a table is updated, all query caches on this table will be cleared.
So it may be very difficult to save the results, but before they are used, they will be completely cleared by an update. For databases with heavy update pressure, the hit rate of the query cache will be very low. Unless your business has a static table that will only be updated once a long time.
For example: a system configuration table, then the query on this table is suitable for query cache.
FortunatelyMySQL
also provides this "use on demand" method. You can set the parameter query_cache_type
to DEMAND
so that the query cache is not used for the default SQL statements.
「Note」:MySQL 8.0
version directly deletes the entire query cache function, marking the beginning of MySQL 8.0 without the cache function at all.
Parser
If the query cache is not hit, the actual execution of the statement will begin. First, MySQL needs to know what you want to do, so it needs to parse the SQL statement.
The analyzer will first do "lexical analysis". What you input is an SQL statement composed of multiple strings and spaces. MySQL needs to identify what the strings in it are and what they represent.
After completing the lexical analysis, we need to do "grammatical analysis". Based on the results of lexical analysis, the syntax analyzer will determine whether the SQL statement you entered satisfies MySQL syntax based on grammatical rules.
If we are missing or misspelling a letter when spelling SQL, we will receive the error message "You have an error in your SQL syntax
".
For example, in the following case:

The error is that there is an E missing in the WHERE keyword.
Similarly, we use SQL if a field does not exist.

Generally, grammatical errors will prompt the first position where the error occurs, so you should pay attention to the words immediately following "use near" The content is for reference only. Sometimes this tip is not very reliable.
The SQL was analyzed by the analyzer and no error was reported. Then enter the optimizer at this time to optimize the SQL.
Optimizer
The optimizer mainly determines which one to use if there are multiple indexes in our database table. Index; or when a statement has multiple table associations (join), determine the connection order of each table.
For example:
SELECT a.id, b.id FROM t_user a join t_user_detail b WHERE a.id=b.user_id and a.user_name='田维常' and b.id=10001
It will optimize the conditional query.
After the optimizer processing is completed, the SQL execution plan has been determined. Then continue into the actuator.
Executor
First of all, you must determine the permissions, that is, whether you have permission to execute this SQL. Permissions may be controlled on some clients during work.
For example: In the production environment, most developers only have query permissions and no permissions to add, delete or modify (except for some small companies).

If you have permission, open the table and continue execution. When a table is opened, the executor will use the interface provided by the engine based on the table's engine definition.
Storage engine layer

In this layer, we It can be understood as the persistence layer in our business system.
The concept of storage engine is unique to MySQL. Not all relational databases have the concept of storage engine.
The database storage engine is the underlying software organization of the database. The database management system (DBMS) uses the data engine to create, query, update and delete data. Different storage engines provide different storage mechanisms, indexing techniques, locking levels and other functions. Using different storage engines, you can also obtain specific functions. Many different database management systems today support a variety of different data engines.
Because data is stored in the form of a table in a relational database, the storage engine can also be called a table type (Table Type, that is, the type of storage and operation of this table).
MySQL5.5 version (mysql version MySQL5.5 version (mysql version >= 5.5 version) From now on, the default storage engine is InnoDB.
The following is a comparison of some relatively commonly used engines:

In In actual projects, most use InnoDB, then MyISAM, and at least other storage engines are used.
We can use the command to see what storage engines MySQL has provided:
show engies;
We can also use the command To view MySQL’s current default storage engine:
show variables like '%storage_engine%';
##MyISAM and
InnoDBThe difference between engines
MySQLThe default storage engine before version 5.5 is the MyISAM storage engine. Many system tables in MySQL use the MyISAM storage engine and system temporary tables. The MyISAM storage engine will also be used, but after Mysql5.5, the default storage engine is the InnoDB storage engine.

- #Are there any transaction operations? Yes, InnoDB.
- Do you want to store concurrent modifications? Yes, InnoDB.
- Are you pursuing fast query with less data modification? Yes, MyISAM.
- Do you want to use full-text indexing? If you do not reference a third-party framework, you can choose MyISAM, but it will be more efficient to use a third-party framework and InnDB.
The InnoDB storage engine mainly has the following features:
-
Support transactions
Support 4 levels of transaction isolation
Supports multi-version reading
##Supports row-level locks
Read and write blocking is related to transaction isolation level
Support caching, which can cache both index and data
# #The entire table and primary key are stored in Cluster mode to form a balanced tree
Of course, this does not mean that InnoDB is necessarily good. In actual development, you still have to choose whether to use InnoDB or MyISAM based on specific scenarios.
MyIASM (this engine is the default storage engine in MySQL databases before 5.5) Features:
MyISAM does not provide support for database transactions
Does not support row-level locks and foreign keys
Due to 2, when executing the INSERT insertion or UPDATE update statement, that is, the entire table needs to be locked to perform the write operation, so it will cause Reduced efficiency
MyISAM saves the number of rows in the table. When executingSELECT COUNT(*) FROM TABLE
, you can read the relevant values directly without scanning the whole table, which is fast.
The difference between the two:
MyISAM is non-transactionally safe, while InnoDB is transactionally safe
MyISAM lock granularity is table-level, while InnoDB supports Row-level lock
MyISAM supports full-text indexes, while InnoDB does not support full-text indexes before MySQL5.6. It supports FULLTEXT indexes after MySQL5.6.
Usage scenario comparison:
If you want to perform a large number of select operations, you should choose MyISAM
##If you want to perform a large number of insert and update operations, you should choose InnoDB
Large size data sets tend to choose the InnoDB engine because it supports transaction processing and failure recovery. The size of the database determines the length of failure recovery time. InnoDB can use transaction logs for data recovery, which is faster. Primary key queries will also be quite fast under the InnoDB engine, but it should be noted that if the primary key is too long, it will also cause performance problems.
Relatively speaking, InnoDB is used more by Internet companies.
System file storage layer
The system file storage layer is mainly responsible for storing database data and logs in system files, and at the same time completing the transaction with the storage engine. It is the physical storage layer of files.
For example: data files, log files, pid files, configuration files, etc.
Data file
「db.opt file」: Record the default character set and calibration of this database test rules.
「frm file」: Metadata information stored in the edge, including definition information of the table structure, etc. Each table will have a frm file corresponding to it.
「MYD file」: A file dedicated to the MyISAM storage engine, which stores the data information of the MyISAM table. Each MyISAM table has a .MYD file. 「MYI文件」:也是MyISAM存储引擎专用的文件,存放MyISAM表的索引相关信息,每一张MyISAM表都有对应的.MYI文件。 「ibd文件和ibdata文件」:存放InnoDB的数据文件(包括索引)。InnoDB存储引擎有两种表空间方式:独立表空间和共享表空间。 「ibdata1文件」:系统表空间数据文件,存储表元数据、Undo日志等。 「ib_logfile0、ib_logfile0文件」:Redo log日志文件。 错误日志:默认是开启状态,可以通过命令查看: 二进制日志binary log:记录了对MySQL数据库执行的更改操作,并且记录了语句的发生时间、执行耗时;但是不记录查询select、show等不修改数据的SQL。主要用于数据库恢复和数据库主从复制。也是大家常说的binlog日志。 慢查询日志:记录查询数据库超时的所有SQL,默认是10秒。 通用查询日志:记录一般查询语句; 用于存放MySQL所有的配置信息的文件,比如:my.cnf、my.ini等。 「pid文件」 pid文件是mysqld应用程序在Linux或者Unix操作系统下的一个进程文件,和许多其他Linux或者Unix服务端程序一样,该文件放着自己的进程id。 「socket文件」 socket文件也是Linux和Unix操作系统下才有的,用户在Linux和Unix操作系统下客户端连接可以不通过TCP/IP网络而直接使用Unix socket来连接MySQL数据库。 SQL查询流程图 MySQL MySQL Some pictures in the article come from the Internet and have been deleted! Recommended reading Summary of interviews with 4 companies in 10 days after resignation
日志文件
show variables like '%log_error%';
show variables like '%log_log%';//查看是否开启binlog日志记录。
show variables like '%binllog%';//查看参数
show binary logs;//查看日志文件
show variables like '%slow_query%';//查看是否开启慢查询日志记录。
show variables '%long_query_time%';//查看时长
show variables like '%general%';
配置文件
##Summary
We can think of the entire system as a software system we develop daily. It also has an access layer, which is specifically designed to connect to external clients. It is very similar to the gateway of our system. The cache is similar to the cache used in our business code. Analysis The processor can be understood as parameter parsing and parameter verification in the business system. The optimization layer can be used as a means of optimizing our code development. Then the storage engine is equivalent to our persistence layer, and the file system is equivalent to the database in the entire business system.
.
Don’t envy any great people or great gods every day, they have also come step by step. Be confident, as long as you do it bit by bit and work down-to-earth, you will become a great master.
The above is the detailed content of Meituan interviewer: Explain clearly the MySQL structure system and make an offer immediately. For more information, please follow other related articles on the PHP Chinese website!

MySQLstringtypesimpactstorageandperformanceasfollows:1)CHARisfixed-length,alwaysusingthesamestoragespace,whichcanbefasterbutlessspace-efficient.2)VARCHARisvariable-length,morespace-efficientbutpotentiallyslower.3)TEXTisforlargetext,storedoutsiderows,

MySQLstringtypesincludeVARCHAR,TEXT,CHAR,ENUM,andSET.1)VARCHARisversatileforvariable-lengthstringsuptoaspecifiedlimit.2)TEXTisidealforlargetextstoragewithoutadefinedlength.3)CHARisfixed-length,suitableforconsistentdatalikecodes.4)ENUMenforcesdatainte

MySQLoffersvariousstringdatatypes:1)CHARforfixed-lengthstrings,2)VARCHARforvariable-lengthtext,3)BINARYandVARBINARYforbinarydata,4)BLOBandTEXTforlargedata,and5)ENUMandSETforcontrolledinput.Eachtypehasspecificusesandperformancecharacteristics,sochoose

TograntpermissionstonewMySQLusers,followthesesteps:1)AccessMySQLasauserwithsufficientprivileges,2)CreateanewuserwiththeCREATEUSERcommand,3)UsetheGRANTcommandtospecifypermissionslikeSELECT,INSERT,UPDATE,orALLPRIVILEGESonspecificdatabasesortables,and4)

ToaddusersinMySQLeffectivelyandsecurely,followthesesteps:1)UsetheCREATEUSERstatementtoaddanewuser,specifyingthehostandastrongpassword.2)GrantnecessaryprivilegesusingtheGRANTstatement,adheringtotheprincipleofleastprivilege.3)Implementsecuritymeasuresl

ToaddanewuserwithcomplexpermissionsinMySQL,followthesesteps:1)CreatetheuserwithCREATEUSER'newuser'@'localhost'IDENTIFIEDBY'password';.2)Grantreadaccesstoalltablesin'mydatabase'withGRANTSELECTONmydatabase.TO'newuser'@'localhost';.3)Grantwriteaccessto'

The string data types in MySQL include CHAR, VARCHAR, BINARY, VARBINARY, BLOB, and TEXT. The collations determine the comparison and sorting of strings. 1.CHAR is suitable for fixed-length strings, VARCHAR is suitable for variable-length strings. 2.BINARY and VARBINARY are used for binary data, and BLOB and TEXT are used for large object data. 3. Sorting rules such as utf8mb4_unicode_ci ignores upper and lower case and is suitable for user names; utf8mb4_bin is case sensitive and is suitable for fields that require precise comparison.

The best MySQLVARCHAR column length selection should be based on data analysis, consider future growth, evaluate performance impacts, and character set requirements. 1) Analyze the data to determine typical lengths; 2) Reserve future expansion space; 3) Pay attention to the impact of large lengths on performance; 4) Consider the impact of character sets on storage. Through these steps, the efficiency and scalability of the database can be optimized.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Notepad++7.3.1
Easy-to-use and free code editor

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool
