提高MySQL数据库查询效率的三个实用技巧
作者:17819 发布时间:2008-12-03 16:40:00
MySQL数据库由于它本身的小巧和操作的高效, 在数据库应用中越来越多的被采用.本文中列举了一个P2P应用开发实例,实例中使用了MySQL来保存P2P节点,由于P2P的应用中,结点数动辄上万个,而且节点变化频繁,因此一定要保持查询和插入的高效,下文中列举了使用过程中提高效率的三个有效的尝试。
使用statement进行绑定查询
使用statement可以提前构建查询语法树,在查询时不再需要构建语法树就直接查询.因此可以很好的提高查询的效率. 这个方法适合于查询条件固定但查询非常频繁的场合.
使用方法是:
绑定, 创建一个MYSQL_STMT变量,与对应的查询字符串绑定,字符串中的问号代表要传入的变量,每个问号都必须指定一个变量.
查询, 输入每个指定的变量, 传入MYSQL_STMT变量用可用的连接句柄执行.
代码如下:
//1.绑定
bool CDBManager::BindInsertStmt(MYSQL * connecthandle)
{
//作插入操作的绑定
MYSQL_BIND insertbind[FEILD_NUM];
if(m_stInsertParam == NULL)
m_stInsertParam = new CHostCacheTable;
m_stInsertStmt = mysql_stmt_init(connecthandle);
//构建绑定字符串
char insertSQL[SQL_LENGTH];
strcpy(insertSQL, "insert into HostCache(SessionID, ChannelID, ISPType, "
"ExternalIP, ExternalPort, InternalIP, InternalPort) "
"values(?, ?, ?, ?, ?, ?, ?)");
mysql_stmt_prepare(m_stInsertStmt, insertSQL, strlen(insertSQL));
int param_count= mysql_stmt_param_count(m_stInsertStmt);
if(param_count != FEILD_NUM)
return false;
//填充bind结构数组, m_sInsertParam是这个statement关联的结构变量
memset(insertbind, 0, sizeof(insertbind));
insertbind[0].buffer_type = MYSQL_TYPE_STRING;
insertbind[0].buffer_length = ID_LENGTH /* -1 */;
insertbind[0].buffer = (char *)m_stInsertParam->sessionid;
insertbind[0].is_null = 0;
insertbind[0].length = 0;
insertbind[1].buffer_type = MYSQL_TYPE_STRING;
insertbind[1].buffer_length = ID_LENGTH /* -1 */;
insertbind[1].buffer = (char *)m_stInsertParam->channelid;
insertbind[1].is_null = 0;
insertbind[1].length = 0;
insertbind[2].buffer_type = MYSQL_TYPE_TINY;
insertbind[2].buffer = (char *)&m_stInsertParam->ISPtype;
insertbind[2].is_null = 0;
insertbind[2].length = 0;
insertbind[3].buffer_type = MYSQL_TYPE_LONG;
insertbind[3].buffer = (char *)&m_stInsertParam->externalIP;
insertbind[3].is_null = 0;
insertbind[3].length = 0;
insertbind[4].buffer_type = MYSQL_TYPE_SHORT;
insertbind[4].buffer = (char *)&m_stInsertParam->externalPort;
insertbind[4].is_null = 0;
insertbind[4].length = 0;
insertbind[5].buffer_type = MYSQL_TYPE_LONG;
insertbind[5].buffer = (char *)&m_stInsertParam->internalIP;
insertbind[5].is_null = 0;
insertbind[5].length = 0;
insertbind[6].buffer_type = MYSQL_TYPE_SHORT;
insertbind[6].buffer = (char *)&m_stInsertParam->internalPort;
insertbind[6].is_null = 0;
insertbind[6].is_null = 0;
//绑定
if (mysql_stmt_bind_param(m_stInsertStmt, insertbind))
return false;
return true;
}


猜你喜欢
- 我就废话不多说了,直接上代码吧!obj = Obj.objects.get(id=1)print obj.name #此时name的值假定为
- 最近稍稍有点空闲时间,于是重新温习了一下之前学习过的python基础。废话不多说,记录一下自己的所得。首先,安装什么的不在本人的温习范围,另
- 本文介绍基于Python中ArcPy模块,对大量不同时相的栅格遥感影像按照其成像时间依次执行批量拼接的方法。在前期的文章Python arc
- 在《CSS二级菜单》中,如果一级菜单中的超链接是#,那么只要单击该一级菜单,那么与这个一级菜单对应的二级菜单就会一直显示在网页中,不能隐藏,
- 本文实例代码主要实现的是python遍历文件目录的操作,有三种方法,具体代码如下。#coding:utf-8 # 方法1:递归遍历目录 im
- 要说基本操作,大家基本都会,但是有时候,有些操作使用小技巧会节省很多时间。本篇描述的就是使用dict小技巧,保存到数据库,用来节省大家编码的
- queue和pipe的区别: pipe用来在两个进程间通信。queue用来在多个进程间实现通信。 此两种方法为所有系统多进程通信的基本方法,
- 前言: json 模块提供了一种很简单的方式来编码和解码JSON数据。 其中两个主要的函数是 json.dumps() 和 jso
- 我们已经在数学上描述了多层感知机,现在让我们尝试自己实现一个多层感知机。为了与我们之前使用softmax回归获得的结果进行比较,我们将继续使
- 散点图,顾名思义是一些散乱的点构成的图。那么这些散乱的点有什么作用呢?散点图通过用两组数据构成多个坐标点,考察坐标点的分布,判断两变量之间是
- 代码共享url: http://code.google.com/p/region-select-js/ 数据已经更新到中国统计局网站中的20
- 数据库在高并发的场景下使用外键约束会有锁问题并且使用外键会增加运维成本,所以很多公司都规定生产环境的数据库禁止使用外键。那么不使用外键约束的
- 目录实现加权轮询负载均衡思路加权轮询负载均衡代码测试代码实现加权轮询负载均衡思路代码实现一个加权负载均衡Weight
- 这节介绍接口测试工具postman的基本使用方法, 测试系统就是2.8节自己开发的具有用户增删改查操作的web应用程序——[FirstJav
- 本文实例讲述了JS实现网页滚动条感应鼠标变色的方法。分享给大家供大家参考。具体实现方法如下:<html><head>
- 调用数据库存储过程见下:<%Set Dataconn = Server.CreateObject(&qu
- 一、需求描述手上有大量外文文档(本案例以5份为例,分别命名为 test1.docx test2.docx 以此
- 今天在部署一个实验系统的时候,报出下面这个错: Your 'max_allowed_packet' variable is
- 闭包在我们使用返回函数的时候,由于我们在一个函数中需要返回另一个函数,因此,我们在这个函数中就需要重新定义一个函数。而这样,就造成了我们的函
- 每次调用内部的方法时,方法前面加 self.举例:例子参考百度知道里面的回答class MyClass: def __init_