search
HomeDatabaseMysql TutorialDetailed introduction to mysql theory and basic knowledge

This article will give you a detailed introduction to mysql theory and basic knowledge. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

Detailed introduction to mysql theory and basic knowledge

mysql architecture

1. Network connection layer

Client Connectors: Provided Built-in support for MySQL server. Currently, almost all mainstream server-side programming technologies are supported, such as common Java, C, Python, .NET, etc., which establish connections with MySQL through their respective API technologies

2. Service layer (MySQL Server)

The service layer is the core of MySQL Server and mainly includes six parts: system management and control tools, connection pool, SQL interface, parser, query optimizer and cache.

Connection Pool: Responsible for storing and managing the connection between the client and the database. One thread is responsible for managing one connection.

System management and control tools (Management Services & Utilities): such as backup and recovery, security management, cluster management, etc.

SQL Interface (SQL Interface): used to accept various types of data sent by the client SQL command and returns the results that the user needs to query. Such as DML, DDL, stored procedures, views, triggers, etc.

Parser (Parser): Responsible for parsing the requested SQL to generate a "parse tree". Then further check whether the parse tree is legal according to some MySQL rules.

Query Optimizer (Optimizer): When the "parse tree" passes the parser grammar check, it will be handed over to the optimizer to convert it into an execution plan, and then interact with the storage engine.

select uid,name from user where gender=1;

Select-》Projection-》Join strategy

1)select first selects based on the where statement, not a query Extract all the data and then filter it

2) The select query performs attribute projection based on uid and name, not all fields

3) Connect the previous selection and projection to finally generate the query result

Cache (Cache&Buffer): The caching mechanism is composed of a series of small caches. For example, table cache, record cache, permission cache, engine cache, etc. If the query cache has a hit query result, the query statement can directly fetch data from the query cache.

3. Storage Engine Layer (Pluggable Storage Engines)

The storage engine is responsible for the storage and extraction of data in MySQL, and interacts with the underlying system files. The MySQL storage engine is plug-in. The query execution engine in the server communicates with the storage engine through an interface. The interface shields the differences between different storage engines. There are many storage engines now, each with its own characteristics. The most common ones are MyISAM and InnoDB.

4. System File Layer (File System)

This layer is responsible for storing database data and logs on the file system and completing the interaction with the storage engine. , is the physical storage layer of files. Mainly includes log files, data files, configuration files, pid files, socket files, etc.

Log file

Error log(Error log)

Enabled by default, show variables like '%log_error%'

General query log ( General query log)

Records general query statements, show variables like '%general%';

Binary log (binary log)

Records changes performed on the MySQL database operation, and records the occurrence time and execution time of the statement; however, it does not record select, show, etc. SQL that does not modify the database. Mainly used for database recovery and master-slave replication.

show variables like '%log_bin%'; //Whether to enable it

show variables like '%binlog%'; //Parameter view

show binary logs;// View the slow query log file

Query log (Slow query log)

Records all query SQL whose execution time has expired. The default is 10 seconds.

show variables like '%slow_query%'; //Whether to enable it

show variables like '%long_query_time%'; //Duration

Configuration file

Used to store all MySQL configuration information files, such as my.cnf, my.ini, etc.

Data file

db.opt file: records the default character set and verification rules used by this library.

frm file: stores metadata (meta) information related to the table, including definition information of the table structure, etc. Each table will have a frm file.

MYD file: It is dedicated to the MyISAM storage engine and stores the data of the MyISAM table. Each table will have a .MYD file.

MYI file: It is dedicated to the MyISAM storage engine and stores index-related information of the MyISAM table. Each MyISAM table corresponds to a .MYI file.

ibd file and IBDATA file: store InnoDB data files (including indexes). The InnoDB storage engine has two table space modes: exclusive table space and shared table space. Exclusive table spaces use .ibd files to store data, and each InnoDB table corresponds to one .ibd file. Shared table spaces use .ibdata files, and all tables use one (or multiple, self-configured) .ibdata files.

ibdata1 file: system table space data file, which stores table metadata, Undo logs, etc.

ib_logfile0, ib_logfile1 files: Redo log log files.

pid file

The pid file is a process file of the mysqld application in the Unix/Linux environment. Like many other Unix/Linux server programs, it stores its own process id.

socket file

The socket file is also available in the Unix/Linux environment. Users can directly use Unix Socket to connect to the client in the Unix/Linux environment without going through the TCP/IP network. Connect to MySQL.

InnoDB and MyISAM

Transactions and foreign keys

InnoDB supports transactions and foreign keys, with security and integrity, suitable for a large number of Insert or update operation

MyISAM does not support transactions and foreign keys. It provides high-speed storage and retrieval, suitable for a large number of select query operations

Lock mechanism

InnoDB supports row-level locking and locks specified records. Locking is implemented based on index.

MyISAM supports table-level locking, locking the entire table.

Index structure

InnoDB uses a clustered index (clustered index). The index and records are stored together, caching both the index and the records.

MyISAM uses non-clustered index (non-clustered index), and the index and record are separated.

Concurrency processing capability

MyISAM uses table locks, which will lead to a low concurrency rate of write operations, no blocking between reads, and blocking of reads and writes.

InnoDB read and write blocking can be related to the isolation level, and multi-version concurrency control (MVCC) can be used to support high concurrency

Storage files

InnoDB The table corresponds to two files, a .frm table structure file and an .ibd data file. InnoDB tables support a maximum of 64TB;

The MyISAM table corresponds to three files, a .frm table structure file, a MYD table data file, and a .MYI index file. Starting with MySQL 5.0, the default limit is 256TB.

The difference between Redo Log and Binlog

Redo Log is a function of the InnoDB engine, while Binlog is a built-in function of MySQL Server and is recorded in binary files.

Redo Log is a physical log, which records the update status content of the data page. Binlog is a logical log, which records the update process.

Redo Log is written in a circular manner, the log space size is fixed, Binlog is written additionally, after one is written, the next one is written, and it will not be overwritten.

Redo Log can be used for automatic recovery of transaction data after the server is down abnormally, and Binlog can be used for master-slave replication and data recovery. Binlog does not have automatic crash-safe capabilities.

In the application, multiple indexes can be added to the slave database to optimize queries. These indexes in the main database can be omitted to improve writing efficiency.

Reading and writing separation scheme

1Read immediately after writing

After writing to the database, the read operation will be completed within a certain period of time Go to the main library, and then read operations access the slave library.

2 Secondary Query

First go to the slave database to read the data. If it cannot be found, go to the main database to read the data. This operation will easily return the read pressure to the main library. In order to avoid malicious attacks, it is recommended to encapsulate the database access API operations, which is beneficial to security and low coupling.

3According to special business processing

Adjust according to business characteristics and importance. For example, important business data reading and writing with high real-time requirements can be placed in the main database. For secondary businesses that do not require high real-time performance, reading and writing can be separated, and queries can be made from the database.

Related recommendations: "mysql tutorial"

The above is the detailed content of Detailed introduction to mysql theory and basic knowledge. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:csdn. If there is any infringement, please contact admin@php.cn delete
How does MySQL index cardinality affect query performance?How does MySQL index cardinality affect query performance?Apr 14, 2025 am 12:18 AM

MySQL index cardinality has a significant impact on query performance: 1. High cardinality index can more effectively narrow the data range and improve query efficiency; 2. Low cardinality index may lead to full table scanning and reduce query performance; 3. In joint index, high cardinality sequences should be placed in front to optimize query.

MySQL: Resources and Tutorials for New UsersMySQL: Resources and Tutorials for New UsersApr 14, 2025 am 12:16 AM

The MySQL learning path includes basic knowledge, core concepts, usage examples, and optimization techniques. 1) Understand basic concepts such as tables, rows, columns, and SQL queries. 2) Learn the definition, working principles and advantages of MySQL. 3) Master basic CRUD operations and advanced usage, such as indexes and stored procedures. 4) Familiar with common error debugging and performance optimization suggestions, such as rational use of indexes and optimization queries. Through these steps, you will have a full grasp of the use and optimization of MySQL.

Real-World MySQL: Examples and Use CasesReal-World MySQL: Examples and Use CasesApr 14, 2025 am 12:15 AM

MySQL's real-world applications include basic database design and complex query optimization. 1) Basic usage: used to store and manage user data, such as inserting, querying, updating and deleting user information. 2) Advanced usage: Handle complex business logic, such as order and inventory management of e-commerce platforms. 3) Performance optimization: Improve performance by rationally using indexes, partition tables and query caches.

SQL Commands in MySQL: Practical ExamplesSQL Commands in MySQL: Practical ExamplesApr 14, 2025 am 12:09 AM

SQL commands in MySQL can be divided into categories such as DDL, DML, DQL, DCL, etc., and are used to create, modify, delete databases and tables, insert, update, delete data, and perform complex query operations. 1. Basic usage includes CREATETABLE creation table, INSERTINTO insert data, and SELECT query data. 2. Advanced usage involves JOIN for table joins, subqueries and GROUPBY for data aggregation. 3. Common errors such as syntax errors, data type mismatch and permission problems can be debugged through syntax checking, data type conversion and permission management. 4. Performance optimization suggestions include using indexes, avoiding full table scanning, optimizing JOIN operations and using transactions to ensure data consistency.

How does InnoDB handle ACID compliance?How does InnoDB handle ACID compliance?Apr 14, 2025 am 12:03 AM

InnoDB achieves atomicity through undolog, consistency and isolation through locking mechanism and MVCC, and persistence through redolog. 1) Atomicity: Use undolog to record the original data to ensure that the transaction can be rolled back. 2) Consistency: Ensure the data consistency through row-level locking and MVCC. 3) Isolation: Supports multiple isolation levels, and REPEATABLEREAD is used by default. 4) Persistence: Use redolog to record modifications to ensure that data is saved for a long time.

MySQL's Place: Databases and ProgrammingMySQL's Place: Databases and ProgrammingApr 13, 2025 am 12:18 AM

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

MySQL: From Small Businesses to Large EnterprisesMySQL: From Small Businesses to Large EnterprisesApr 13, 2025 am 12:17 AM

MySQL is suitable for small and large enterprises. 1) Small businesses can use MySQL for basic data management, such as storing customer information. 2) Large enterprises can use MySQL to process massive data and complex business logic to optimize query performance and transaction processing.

What are phantom reads and how does InnoDB prevent them (Next-Key Locking)?What are phantom reads and how does InnoDB prevent them (Next-Key Locking)?Apr 13, 2025 am 12:16 AM

InnoDB effectively prevents phantom reading through Next-KeyLocking mechanism. 1) Next-KeyLocking combines row lock and gap lock to lock records and their gaps to prevent new records from being inserted. 2) In practical applications, by optimizing query and adjusting isolation levels, lock competition can be reduced and concurrency performance can be improved.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools