Maison > base de données > tutoriel mysql > le corps du texte

提高MySQL查询效率的三个技巧(1)

WBOY
Libérer: 2016-06-07 16:06:59
original
1077 Les gens l'ont consulté

【引自陈敏的博客】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>
Copier après la connexion

随机的获取记录

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

有两种方法可以做到:

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

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

Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!