search
HomeDatabaseMysql Tutorial提高MySQL查询效率的三个技巧(1)
提高MySQL查询效率的三个技巧(1)Jun 07, 2016 pm 04:06 PM
mysqlblogSkillimproveefficiencyInquire

【引自陈敏的博客】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>

随机的获取记录

在某些数据库的应用中,我们并不是要获取所有的满足条件的记录,而只是要随机挑选出满足条件的记录。这种情况常见于数据业务的统计分析,从大容量数据库中获取小量的数据的场合。

有两种方法可以做到:

1、常规方法,首先查询出所有满足条件的记录,然后随机的挑选出部分记录。这种方法在满足条件的记录数很多时效果不理想。

2、使用limit语法,先获取满足条件的记录条数,然后在sql查询语句中加入limit来限制只查询满足要求的一段记录。这种方法虽然要查询两次,但是在数据量大时反而比较高效。

Statement
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
mysql怎么替换换行符mysql怎么替换换行符Apr 18, 2022 pm 03:14 PM

在mysql中,可以利用char()和REPLACE()函数来替换换行符;REPLACE()函数可以用新字符串替换列中的换行符,而换行符可使用“char(13)”来表示,语法为“replace(字段名,char(13),'新字符串') ”。

mysql怎么将varchar转换为int类型mysql怎么将varchar转换为int类型May 12, 2022 pm 04:51 PM

转换方法:1、利用cast函数,语法“select * from 表名 order by cast(字段名 as SIGNED)”;2、利用“select * from 表名 order by CONVERT(字段名,SIGNED)”语句。

MySQL复制技术之异步复制和半同步复制MySQL复制技术之异步复制和半同步复制Apr 25, 2022 pm 07:21 PM

本篇文章给大家带来了关于mysql的相关知识,其中主要介绍了关于MySQL复制技术的相关问题,包括了异步复制、半同步复制等等内容,下面一起来看一下,希望对大家有帮助。

mysql怎么判断是否是数字类型mysql怎么判断是否是数字类型May 16, 2022 am 10:09 AM

在mysql中,可以利用REGEXP运算符判断数据是否是数字类型,语法为“String REGEXP '[^0-9.]'”;该运算符是正则表达式的缩写,若数据字符中含有数字时,返回的结果是true,反之返回的结果是false。

带你把MySQL索引吃透了带你把MySQL索引吃透了Apr 22, 2022 am 11:48 AM

本篇文章给大家带来了关于mysql的相关知识,其中主要介绍了mysql高级篇的一些问题,包括了索引是什么、索引底层实现等等问题,下面一起来看一下,希望对大家有帮助。

mysql怎么删除unique keymysql怎么删除unique keyMay 12, 2022 pm 03:01 PM

在mysql中,可利用“ALTER TABLE 表名 DROP INDEX unique key名”语句来删除unique key;ALTER TABLE语句用于对数据进行添加、删除或修改操作,DROP INDEX语句用于表示删除约束操作。

mysql需要commit吗mysql需要commit吗Apr 27, 2022 pm 07:04 PM

在mysql中,是否需要commit取决于存储引擎:1、若是不支持事务的存储引擎,如myisam,则不需要使用commit;2、若是支持事务的存储引擎,如innodb,则需要知道事务是否自动提交,因此需要使用commit。

mysql-connector是什么mysql-connector是什么May 12, 2022 pm 04:04 PM

“mysql-connector”是mysql官方提供的驱动器,可以用于连接使用mysql;可利用“pip install mysql-connector”命令进行安装,利用“import mysql.connector”测试是否安装成功。

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尊渡假赌尊渡假赌尊渡假赌

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MantisBT

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.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools