mysql数据库查询优化 mysql效率

  • A+
所属分类:数据库
MySQL由于它本身的小巧和操作的高效, 在数据库应用中越来越多的被采用.我在开发一个P2P应用的时候曾经使用MySQL来保存P2P节点,由于P2P的应用中,结点数动辄上万个,而且节点变化频繁,因此一定要保持查询和插入的高效.以下是我在使用过程中做的提高效率的三个有效的尝试. 1. 使用statement进行绑定查询 2. 随机的获取记录 3. 使用连接池管理连接.
提高MySQL 查询效率的三个技巧小结
MySQL由于它本身的小巧和操作的高效, 在数据库应用中越来越多的被采用.我在开发一个P2P应用的时候曾经使用MySQL来保存P2P节点,由于P2P的应用中,结点数动辄上万个,而且节点变化频繁,因此一定要保持查询和插入的高效.以下是我在使用过程中做的提高效率的三个有效的尝试.l        使用statement进行绑定查询
使用statement可以提前构建查询语法树,在查询时不再需要构建语法树就直接查询.因此可以很好的提高查询的效率. 这个方法适合于查询条件固定但查询非常频繁的场合.
使用方法是:
绑定, 创建一个MYSQL_STMT变量,与对应的查询字符串绑定,字符串中的问号代表要传入的变量,每个问号都必须指定一个变量.
查询, 输入每个指定的变量, 传入MYSQL_STMT变量用可用的连接句柄执行.
代码如下:
  1. //1.绑定  
  2. bool CDBManager::BindInsertStmt(MYSQL * connecthandle)
  3. {
  4.        //作插入操作的绑定  
  5.        MYSQL_BIND insertbind[FEILD_NUM];
  6.        if(m_stInsertParam == NULL)
  7.               m_stInsertParam = new CHostCacheTable;
  8.        m_stInsertStmt = mysql_stmt_init(connecthandle);
  9.        //构建绑定字符串  
  10.        char insertSQL[SQL_LENGTH];
  11.        strcpy(insertSQL, "insert into HostCache(SessionID, ChannelID, ISPType, "
  12.               "ExternalIP, ExternalPort, InternalIP, InternalPort) "
  13.               "values(?, ?, ?, ?, ?, ?, ?)");
  14.        mysql_stmt_prepare(m_stInsertStmt, insertSQL, strlen(insertSQL));
  15.        int param_count= mysql_stmt_param_count(m_stInsertStmt);
  16.        if(param_count != FEILD_NUM)
  17.               return false;
  18.        //填充bind结构数组, m_sInsertParam是这个statement关联的结构变量  
  19.        memset(insertbind, 0, sizeof(insertbind));
  20.        insertbind[0].buffer_type = MYSQL_TYPE_STRING;
  21.        insertbind[0].buffer_length = ID_LENGTH /* -1 */;
  22.        insertbind[0].buffer = (char *)m_stInsertParam->sessionid;
  23.        insertbind[0].is_null = 0;
  24.        insertbind[0].length = 0;
  25.        insertbind[1].buffer_type = MYSQL_TYPE_STRING;
  26.        insertbind[1].buffer_length = ID_LENGTH /* -1 */;
  27.        insertbind[1].buffer = (char *)m_stInsertParam->channelid;
  28.        insertbind[1].is_null = 0;
  29.        insertbind[1].length = 0;
  30.        insertbind[2].buffer_type = MYSQL_TYPE_TINY;
  31.        insertbind[2].buffer = (char *)&m_stInsertParam->ISPtype;
  32.        insertbind[2].is_null = 0;
  33.        insertbind[2].length = 0;
  34.        insertbind[3].buffer_type = MYSQL_TYPE_LONG;
  35.        insertbind[3].buffer = (char *)&m_stInsertParam->externalIP;
  36.        insertbind[3].is_null = 0;
  37.        insertbind[3].length = 0;
  38.        insertbind[4].buffer_type = MYSQL_TYPE_SHORT;
  39.        insertbind[4].buffer = (char *)&m_stInsertParam->externalPort;
  40.        insertbind[4].is_null = 0;
  41.        insertbind[4].length = 0;
  42.        insertbind[5].buffer_type = MYSQL_TYPE_LONG;
  43.        insertbind[5].buffer = (char *)&m_stInsertParam->internalIP;
  44.        insertbind[5].is_null = 0;
  45.        insertbind[5].length = 0;
  46.        insertbind[6].buffer_type = MYSQL_TYPE_SHORT;
  47.        insertbind[6].buffer = (char *)&m_stInsertParam->internalPort;
  48.        insertbind[6].is_null = 0;
  49.        insertbind[6].is_null = 0;
  50.        //绑定  
  51.        if (mysql_stmt_bind_param(m_stInsertStmt, insertbind))
  52.               return false;
  53.        return true;
  54. }
  55. //2.查询  
  56. bool CDBManager::InsertHostCache2(MYSQL * connecthandle, char * sessionid, char * channelid, int ISPtype, \
  57.               unsigned int eIP, unsigned short eport, unsigned int iIP, unsigned short iport)
  58. {
  59.        //填充结构变量m_sInsertParam  
  60.        strcpy(m_stInsertParam->sessionid, sessionid);
  61.        strcpy(m_stInsertParam->channelid, channelid);
  62.        m_stInsertParam->ISPtype = ISPtype;
  63.        m_stInsertParam->externalIP = eIP;
  64.        m_stInsertParam->externalPort = eport;
  65.        m_stInsertParam->internalIP = iIP;
  66.        m_stInsertParam->internalPort = iport;
  67.        //执行statement,性能瓶颈处  
  68.        if(mysql_stmt_execute(m_stInsertStmt))
  69.               return false;
  70.        return true;
  71. }
图片引用自网络