Home Backend Development PHP Tutorial Overview of PHP to MySQL data query process

Overview of PHP to MySQL data query process

Jul 30, 2016 pm 01:30 PM
buffer mysql mysqli

HP layer to MySQL layer

Php to sql component hierarchy is as shown below:

Overview of PHP to MySQL data query process

ext/mysqli and ext/mysql are client-side extension libraries (library functions), and extension libraries at the client-side script level . Mysqli library is an extended version of the mysql library. The extended version adds column binding (Bind Column). PDO (PHP Data Object) is another data object-oriented extension library. These extension libraries are directly oriented to programmers, and their underlying implementation is the mysql connection engine (such as mysqlnd and libmysql) (refer to http://bbs.chinaunix.net/thread-3679393-1-1.html, http://blog .csdn.net/treesky/article/details/7286098).

mysqlnd and libmysql are the database connection driver engines of the PHP side (client). libmysql is a general database connection engine, while mysqlnd is a connection engine developed exclusively for PHP and belongs to Zend. When PHP performs database query by calling the mysql_query() function in the extension library (ext/mysqli and ext/mysql), the Zend engine will issue a query request to the MySQL server through the mysql (mysqlnd and libmysql) query engine.

Data query at MySQL layer

Overview of PHP to MySQL data query process

After the MySQL server receives the query request from the client, the query execution process is as shown in the figure above:
1. Query cache, if hit, the result set will be directly returned to the client, otherwise Enter step 2
2. Perform parsing, preprocessing, query optimization and other operations on the SQL statement in sequence, and finally generate a query execution plan (the query execution plan of select can be viewed through explain select)
3. The query execution engine of the MySQL server will execute the query based on the query The execution plan calls the storage engine to query the data. When the last level of association of the SQL statement is executed, a query result set will be generated
4. The query result set is sent to the client, and there are two ways to return it: the MySQL server caches the result set or does not cache it. This is determined by the parameter SQL_BUFFER_RESULT set up. And, if the user sets SQL_CACHE, then a copy of the result set of this query is stored in the query cache (relevant to step 1).

Enlightenment of the SQL_CACHE parameter:
Decompose complex (multiple association) queries into multiple simple queries, because
1) cache hits of simple queries,
2) cache of complex query results are prone to failure (too many associations) Table)
3) The simple query lock holding rate is low

MySQL Server to the PHP layer

Communication mode The communication between MySQL Server and the client adopts "half-duplex communication", which means: there can only be one client and one server is reading, and the other must be writing.

Advantages: The protocol is simple, and the write permissions of the client and server are mutually exclusive.

Disadvantages: Flow control cannot be performed. One end starts sending a message, and the other end must fully accept the message before it can respond to it.

Enlightenment: The result set after the server query is sent to the client, and the client (the client's query engine, such as mysqlnd) must accept it completely. Therefore, if you only need a few rows, remember to add limit to the sql statement and use select * sparingly.

Result set return mode In the result set return, each row of records is packaged through the client-server communication protocol, and then handed over to the lower tcp protocol; of course, at the tcp layer, the protocol of each row of records can be cached first Packets are sent out into large packets (transparent to the application layer).

The MySQL server can only release the buffer occupied by the result set after sending all the result sets to the client.

Server-side cache mode

Overview of PHP to MySQL data query process


Client command: mysql_unbuffer_query(), the cache of the result set is not set in the client's sql driver extension (mysqlnd), so when feth_array_xxx reads a record from the result set, it is required Read from the server buffer.

Server-side no cache mode

Overview of PHP to MySQL data query process


Client command: mysql_query(), a buffer is set in the client's sql driver extension (mysqlnd) to cache the server's result set, so fetch_array_xxx reads from the result set When a record is obtained, the row is obtained directly from the mysqlnd extended buffer.

Summary

If the result set is large: The server-side cache-free mode can reduce the memory pressure on the server, but it takes up the memory of the client. It just depends on the situation.

PHP layer to user layer

On the client side, the mysql extension engine (libmysql or mysqlnd) is connected to the server, and the user layer interacts with the mysql engine through the extension library (ext/mysql or ext/mysqli) ( The inspiration is to call the engine's API to read the result set).

The mechanisms of the engines libmysql and mysqlnd are different. The main difference is that mysqlnd is written in PHP and compiled into zend. And libmysql is a general library, zend needs to call this library to realize the database connection. Under this distinction, mysqlnd and zend have better adhesion, and there is one less layer of data copy when the data is transmitted to the user layer. The specific architectural differences are shown in the figure below. In the figure, the five-pointed star represents the cache buffer.


Overview of PHP to MySQL data query process


ext/mysqli and ext/mysql are client-side extension libraries (library functions): At the client-side script level, mysqlInd and libmysql are MySQL Server-side drivers. Among them, libmysql is a universal MySQL query driver, while mysqlnd is a Zend engine-based SQL driver specially designed for PHP. That is, the data-driven actions of mysqlnd require interaction between Zend and mysqlserver, while libmysql directly interacts with mysqlserver.

Comparison:
The process of database query between ext/mysqli (or ext/mysql) and libmysql is:
1) mysqi sends a query request to the libmysql driver
2) Libmysql executes the request and gets the result set in the buffers of the storage domain libmysql
3) Mysqli applies for memory: a buffer specified by zval
4) Mysqii copies the result set from libmysql to the buffer specified by zval
The process in database query of ext/mysqli (or ext/mysql) and mysqlnd is:
1) mysqi sends a query request to the mysqlnd driver
2) The mysqlnd driver executes the sql query through the zend engine. Each row of the result set is stored in a buffer (each buffer is scattered)
3) Mysqlnd creates multiple zvals and points to these buffers

For example:
In ext/mysql & libmysql, the libmysql driver obtains the result set Row1~Row3 after executing the SQL statement, and then ext/mysql copies the result set to the zend buffer, and then the mysqli_fetch_xxx function reads the result set from the memory in this area. content.
In ext/mysqli & mysqlInd, the mysqlnd driver executes the SQL statement to obtain the result set Row1~Row3, where each row is directly stored by a buffer of zend and pointed to by a zval. The client reads the results directly from this memory area through mapping to implement mysqli_fetch_xxx.

Summary

mysqlnd and zend are more cohesive. In the sql query driver, mysqlnd accesses the database through the zend engine and directly stores the results in the zend buffer. Compared with the libmysql driver (independent of zend), there are fewer A cache copy of the result set is made.

Reference

"High Performance MySQL"

http://www.cnxct.com/libmysql-mysqlnd-which-is-best-and-what-about-mysqli-pdomysql-mysql/

http:// www.cnxct.com/wp-content/uploads/2012/12/andrey-mysqlnd.pdf

Copyright Statement: This article is an original article by the blogger (http://blog.csdn.net/ordeder) and has not been authorized by the blogger No reproduction allowed.

The above introduces an overview of the data query process from PHP to MySQL, including all aspects. I hope it will be helpful to friends who are interested in MySql.

Computer/mobile phone tips: Cancel shared documents
By default, when you open My Computer in Windows XP, you will see some folders above the hard disk icon. These are "shared folders", and here are each folder used by users to share files. We can make these folders disappear from my computer. The principle is very simple. Just open the registry and find the following location: HKEY_LOCAL_MACHINESOFTWAREMicrosoftWindows CurrentVersion ExplorerMyComputerNameSpaceDelegateFolders. Delete the {59031a47-3f72-44a7-89c5-5595fe6b30ee} key value. Open me next time. computer, these annoying folders no longer exist.

The above has introduced an overview of the PHP to MySQL data query process, including various aspects. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

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

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MySQL's Role: Databases in Web Applications MySQL's Role: Databases in Web Applications Apr 17, 2025 am 12:23 AM

The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

Laravel Introduction Example Laravel Introduction Example Apr 18, 2025 pm 12:45 PM

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

How to start mysql by docker How to start mysql by docker Apr 15, 2025 pm 12:09 PM

The process of starting MySQL in Docker consists of the following steps: Pull the MySQL image to create and start the container, set the root user password, and map the port verification connection Create the database and the user grants all permissions to the database

Solve database connection problem: a practical case of using minii/db library Solve database connection problem: a practical case of using minii/db library Apr 18, 2025 am 07:09 AM

I encountered a tricky problem when developing a small application: the need to quickly integrate a lightweight database operation library. After trying multiple libraries, I found that they either have too much functionality or are not very compatible. Eventually, I found minii/db, a simplified version based on Yii2 that solved my problem perfectly.

How to install mysql in centos7 How to install mysql in centos7 Apr 14, 2025 pm 08:30 PM

The key to installing MySQL elegantly is to add the official MySQL repository. The specific steps are as follows: Download the MySQL official GPG key to prevent phishing attacks. Add MySQL repository file: rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm Update yum repository cache: yum update installation MySQL: yum install mysql-server startup MySQL service: systemctl start mysqld set up booting

Centos install mysql Centos install mysql Apr 14, 2025 pm 08:09 PM

Installing MySQL on CentOS involves the following steps: Adding the appropriate MySQL yum source. Execute the yum install mysql-server command to install the MySQL server. Use the mysql_secure_installation command to make security settings, such as setting the root user password. Customize the MySQL configuration file as needed. Tune MySQL parameters and optimize databases for performance.

Laravel framework installation method Laravel framework installation method Apr 18, 2025 pm 12:54 PM

Article summary: This article provides detailed step-by-step instructions to guide readers on how to easily install the Laravel framework. Laravel is a powerful PHP framework that speeds up the development process of web applications. This tutorial covers the installation process from system requirements to configuring databases and setting up routing. By following these steps, readers can quickly and efficiently lay a solid foundation for their Laravel project.

MySQL and phpMyAdmin: Core Features and Functions MySQL and phpMyAdmin: Core Features and Functions Apr 22, 2025 am 12:12 AM

MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.

See all articles