提高MySQL查询效率的三个技巧(1)
【引自陈敏的博客】MySQL由于它本身的小巧和操作的高效,在数据库应用中越来越多的被采用。我在开发一个P2P应用的时候曾经使用MySQL来保存P2P节点,由于P2P的应用中,结点数动辄上万个,而且节点变化频繁,因此一定要保持查询和插入的高效。以下是我在使用过
【引自陈敏的博客】MySQL由于它本身的小巧和操作的高效,在数据库应用中越来越多的被采用。我在开发一个P2P应用的时候曾经使用MySQL来保存P2P节点,由于P2P的应用中,结点数动辄上万个,而且节点变化频繁,因此一定要保持查询和插入的高效。以下是我在使用过程中做的提高效率的三个有效的尝试。
使用statement进行绑定查询
使用statement可以提前构建查询语法树,在查询时不再需要构建语法树就直接查询。因此可以很好的提高查询的效率。这个方法适合于查询条件固定但查询非常频繁的场合。
使用方法是:
绑定,创建一个MYSQL_STMT变量,与对应的查询字符串绑定,字符串中的问号代表要传入的变量,每个问号都必须指定一个变量。
查询,输入每个指定的变量,传入MYSQL_STMT变量用可用的连接句柄执行。
代码如下:
<p>//1.绑定<br>bool CDBManager::BindInsertStmt(MYSQL * connecthandle)<br>{<br>//作插入操作的绑定<br>MYSQL_BIND insertbind[FEILD_NUM];<br>if(m_stInsertParam == NULL)<br>m_stInsertParam = new CHostCacheTable;<br>m_stInsertStmt = mysql_stmt_init(connecthandle);<br>//构建绑定字符串<br>char insertSQL[SQL_LENGTH];<br>strcpy(insertSQL, "insert into HostCache(SessionID, ChannelID, ISPType, "<br>"ExternalIP, ExternalPort, InternalIP, InternalPort) "<br>"values(?, ?, ?, ?, ?, ?, ?)");<br>mysql_stmt_prepare(m_stInsertStmt, insertSQL, strlen(insertSQL));<br>int param_count= mysql_stmt_param_count(m_stInsertStmt);<br>if(param_count != FEILD_NUM)<br>return false;<br>//填充bind结构数组, m_sInsertParam是这个statement关联的结构变量<br>memset(insertbind, 0, sizeof(insertbind));<br>insertbind[0].buffer_type = MYSQL_TYPE_STRING;<br>insertbind[0].buffer_length = ID_LENGTH /* -1 */;<br>insertbind[0].buffer = (char *)m_stInsertParam->sessionid;<br>insertbind[0].is_null = 0;<br>insertbind[0].length = 0;<br><br>insertbind[1].buffer_type = MYSQL_TYPE_STRING;<br>insertbind[1].buffer_length = ID_LENGTH /* -1 */;<br>insertbind[1].buffer = (char *)m_stInsertParam->channelid;<br>insertbind[1].is_null = 0;<br>insertbind[1].length = 0;<br><br>insertbind[2].buffer_type = MYSQL_TYPE_TINY;<br>insertbind[2].buffer = (char *)&m_stInsertParam->ISPtype;<br>insertbind[2].is_null = 0;<br>insertbind[2].length = 0;<br><br>insertbind[3].buffer_type = MYSQL_TYPE_LONG;<br>insertbind[3].buffer = (char *)&m_stInsertParam->externalIP;<br>insertbind[3].is_null = 0;<br>insertbind[3].length = 0;<br><br>insertbind[4].buffer_type = MYSQL_TYPE_SHORT;<br>insertbind[4].buffer = (char *)&m_stInsertParam->externalPort;<br>insertbind[4].is_null = 0;<br>insertbind[4].length = 0;<br><br>insertbind[5].buffer_type = MYSQL_TYPE_LONG;<br>insertbind[5].buffer = (char *)&m_stInsertParam->internalIP;<br>insertbind[5].is_null = 0;<br>insertbind[5].length = 0;<br><br>insertbind[6].buffer_type = MYSQL_TYPE_SHORT;<br>insertbind[6].buffer = (char *)&m_stInsertParam->internalPort;<br>insertbind[6].is_null = 0;<br>insertbind[6].is_null = 0;<br>//绑定<br>if (mysql_stmt_bind_param(m_stInsertStmt, insertbind))<br>return false;<br>return true;<br>}</p> Copy after login |
随机的获取记录
在某些数据库的应用中,我们并不是要获取所有的满足条件的记录,而只是要随机挑选出满足条件的记录。这种情况常见于数据业务的统计分析,从大容量数据库中获取小量的数据的场合。
有两种方法可以做到:
1、常规方法,首先查询出所有满足条件的记录,然后随机的挑选出部分记录。这种方法在满足条件的记录数很多时效果不理想。
2、使用limit语法,先获取满足条件的记录条数,然后在sql查询语句中加入limit来限制只查询满足要求的一段记录。这种方法虽然要查询两次,但是在数据量大时反而比较高效。

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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

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

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Big data structure processing skills: Chunking: Break down the data set and process it in chunks to reduce memory consumption. Generator: Generate data items one by one without loading the entire data set, suitable for unlimited data sets. Streaming: Read files or query results line by line, suitable for large files or remote data. External storage: For very large data sets, store the data in a database or NoSQL.

MySQL query performance can be optimized by building indexes that reduce lookup time from linear complexity to logarithmic complexity. Use PreparedStatements to prevent SQL injection and improve query performance. Limit query results and reduce the amount of data processed by the server. Optimize join queries, including using appropriate join types, creating indexes, and considering using subqueries. Analyze queries to identify bottlenecks; use caching to reduce database load; optimize PHP code to minimize overhead.

Backing up and restoring a MySQL database in PHP can be achieved by following these steps: Back up the database: Use the mysqldump command to dump the database into a SQL file. Restore database: Use the mysql command to restore the database from SQL files.

How to insert data into MySQL table? Connect to the database: Use mysqli to establish a connection to the database. Prepare the SQL query: Write an INSERT statement to specify the columns and values to be inserted. Execute query: Use the query() method to execute the insertion query. If successful, a confirmation message will be output.

One of the major changes introduced in MySQL 8.4 (the latest LTS release as of 2024) is that the "MySQL Native Password" plugin is no longer enabled by default. Further, MySQL 9.0 removes this plugin completely. This change affects PHP and other app

To use MySQL stored procedures in PHP: Use PDO or the MySQLi extension to connect to a MySQL database. Prepare the statement to call the stored procedure. Execute the stored procedure. Process the result set (if the stored procedure returns results). Close the database connection.

Creating a MySQL table using PHP requires the following steps: Connect to the database. Create the database if it does not exist. Select a database. Create table. Execute the query. Close the connection.

Oracle database and MySQL are both databases based on the relational model, but Oracle is superior in terms of compatibility, scalability, data types and security; while MySQL focuses on speed and flexibility and is more suitable for small to medium-sized data sets. . ① Oracle provides a wide range of data types, ② provides advanced security features, ③ is suitable for enterprise-level applications; ① MySQL supports NoSQL data types, ② has fewer security measures, and ③ is suitable for small to medium-sized applications.
