提高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
投稿

猜你喜欢

  • 对Python中的@classmethod用法详解

    2023-07-22 17:55:54
  • php之Aes加密案例讲解

    2023-06-11 12:59:12
  • Python 经典面试题 21 道【不可错过】

    2023-10-29 04:56:20
  • Python实现8种常用抽样方法

    2023-02-01 18:03:19
  • pandas创建DataFrame的方式小结

    2021-10-25 13:26:23
  • 如何自己制作验证码的head.fix和body.fix文件

    2009-06-01 12:50:00
  • Linux中Oracle数据库备份

    2024-01-18 16:03:57
  • pytho matplotlib工具栏源码探析一之禁用工具栏、默认工具栏和工具栏管理器三种模式的差异

    2021-08-31 05:15:21
  • python os.path模块使用方法介绍

    2023-08-03 18:02:32
  • JavaScript命令模式原理与用法实例详解

    2024-04-23 09:32:04
  • Django-celery-beat动态添加周期性任务实现过程解析

    2021-07-29 13:09:49
  • 利用python list完成最简单的DB连接池方法

    2022-04-19 18:38:00
  • CentOS6.4上使用yum安装mysql

    2024-01-18 10:07:09
  • 详解如何用Python登录豆瓣并爬取影评

    2021-09-08 00:10:10
  • Python图片处理之图片采样处理详解

    2021-01-14 12:58:36
  • python中list循环语句用法实例

    2022-09-12 15:08:05
  • 整理的比较全的一句话后门代码(方面大家查找后门)

    2023-06-17 18:30:29
  • python设置检查点简单实现代码

    2023-09-25 19:08:44
  • Python实现的金山快盘的签到程序

    2022-12-05 21:14:51
  • go redis实现滑动窗口限流的方式(redis版)

    2024-02-02 18:04:27
  • asp之家 网络编程 m.aspxhome.com