提高MYSQL查询效率的三个有效的尝试

来源:陈敏的Blog 时间:2009-02-27 16:08:00 

MySQL由于它本身的小巧和操作的高效, 在数据库应用中越来越多的被采用.我在开发一个P2P应用的时候曾经使用MySQL来保存P2P节点,由于P2P的应用中,结点数动辄上万个,而且节点变化频繁,因此一定要保持查询和插入的高效.以下是我在使用过程中做的提高效率的三个有效的尝试.

  1、使用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;
}

//2.查询
bool CDBManager::InsertHostCache2(MYSQL * connecthandle, char * sessionid, char * channelid, int ISPtype, \
              unsigned int eIP, unsigned short eport, unsigned int iIP, unsigned short iport)
{
       //填充结构变量m_sInsertParam
       strcpy(m_stInsertParam->sessionid, sessionid);
       strcpy(m_stInsertParam->channelid, channelid);
       m_stInsertParam->ISPtype = ISPtype;
       m_stInsertParam->externalIP = eIP;
       m_stInsertParam->externalPort = eport;
       m_stInsertParam->internalIP = iIP;
       m_stInsertParam->internalPort = iport;
       //执行statement,性能瓶颈处
       if(mysql_stmt_execute(m_stInsertStmt))
              return false;
       return true;
}


标签:mysql,查询,效率
0
投稿

猜你喜欢

  • asp如何统计字符串出现的次数?

    2009-11-25 20:36:00
  • fso对象CreateTextFile方法调用时“无效的过程调用或参数”错误

    2009-05-26 15:39:00
  • 微软:跑分速度不是IE8的开发重点

    2008-09-12 12:10:00
  • CSS网页布局扩展小技巧

    2010-06-03 12:13:00
  • 详细介绍查询优化技术在现实系统中的运用

    2009-01-04 13:34:00
  • IE下float产生双倍margin

    2007-12-26 12:29:00
  • ASP程序员面试题

    2011-09-15 20:51:20
  • 跨浏览器实现float:center,No CSS hacks

    2008-08-22 12:59:00
  • 将后台数据从Berkeley的文件DB转到MySQL

    2009-01-04 13:31:00
  • Google首页的CSS Sprite

    2007-09-29 21:36:00
  • HTML标签tbody的用法

    2009-11-02 10:11:00
  • SQL学习笔记四 聚合函数、排序方法

    2011-09-30 11:33:29
  • HTML5硝烟弥漫

    2009-07-06 14:44:00
  • CSS Sprites (CSS图像拼合技术)教程工具

    2009-05-26 15:30:00
  • MySql数据库基本命令集会

    2011-08-05 18:43:23
  • 用CSS设置表格Table的细边框的比较好用的方法

    2010-09-06 14:58:00
  • 并行查询让SQL Server加速运行

    2009-03-16 16:31:00
  • 将SQL Server中所有表的列信息显示出来

    2009-01-08 16:27:00
  • css利用锚点控制翻页

    2008-07-17 13:12:00
  • CSS如何做细线表格

    2009-01-09 13:12:00
  • asp之家 网络编程 m.aspxhome.com