当前位置: 移动技术网 > IT编程>数据库>Mysql > mysql数据库查询优化 mysql效率第1/3页

mysql数据库查询优化 mysql效率第1/3页

2017年12月12日  | 移动技术网IT编程  | 我要评论

  使用连接池管理连接.
在有大量节点访问的数据库设计中,经常要使用到连接池来管理所有的连接.
一般方法是:建立两个连接句柄队列,空闲的等待使用的队列和正在使用的队列.
当要查询时先从空闲队列中获取一个句柄,插入到正在使用的队列,再用这个句柄做数据库操作,完毕后一定要从使用队列中删除,再插入到空闲队列.
设计代码如下: 

复制代码 代码如下:

//定义句柄队列 
typedef std::list<mysql *> connection_handle_list; 
typedef std::list<mysql *>::iterator connection_handle_list_it; 

//连接数据库的参数结构 
class cdbparameter              

public: 
       char *host;                                 ///<主机名 
       char *user;                                 ///<用户名 
       char *password;                         ///<密码 
       char *database;                           ///<数据库名 
       unsigned int port;                 ///<端口,一般为0 
       const char *unix_socket;      ///<套接字,一般为null 
       unsigned int client_flag; ///<一般为0 
}; 

//创建两个队列 
connection_handle_list m_lsbusylist;                ///<正在使用的连接句柄 
connection_handle_list m_lsidlelist;                  ///<未使用的连接句柄 

//所有的连接句柄先连上数据库,加入到空闲队列中,等待使用. 
bool cdbmanager::connect(char * host /* = "localhost" */, char * user /* = "chenmin" */, \ 
                                           char * password /* = "chenmin" */, char * database /* = "hostcache" */) 

       cdbparameter * lpdbparam = new cdbparameter(); 
       lpdbparam->host = host; 
       lpdbparam->user = user; 
       lpdbparam->password = password; 
       lpdbparam->database = database; 
       lpdbparam->port = 0; 
       lpdbparam->unix_socket = null; 
       lpdbparam->client_flag = 0; 
       try 
       { 
              //连接 
              for(int index = 0; index < connection_num; index++) 
              { 
                     mysql * pconnecthandle = mysql_init((mysql*) 0);     //初始化连接句柄 
                     if(!mysql_real_connect(pconnecthandle, lpdbparam->host, lpdbparam->user, lpdbparam->password,\ 
       lpdbparam->database,lpdbparam->port,lpdbparam->unix_socket,lpdbparam->client_fla)) 
                            return false; 
//加入到空闲队列中 
                     m_lsidlelist.push_back(pconnecthandle); 
              } 
       } 
       catch(...) 
       { 
              return false; 
       } 
       return true; 


//提取一个空闲句柄供使用 
mysql * cdbmanager::getidleconnecthandle() 

       mysql * pconnecthandle = null; 
       m_listmutex.acquire(); 
       if(m_lsidlelist.size()) 
       { 
              pconnecthandle = m_lsidlelist.front();        
              m_lsidlelist.pop_front(); 
              m_lsbusylist.push_back(pconnecthandle); 
       } 
       else //特殊情况,闲队列中为空,返回为空 
       { 
              pconnecthandle = 0; 
       } 
       m_listmutex.release(); 

       return pconnecthandle; 


//从使用队列中释放一个使用完毕的句柄,插入到空闲队列 
void cdbmanager::setidleconnecthandle(mysql * connecthandle) 

       m_listmutex.acquire(); 
       m_lsbusylist.remove(connecthandle); 
       m_lsidlelist.push_back(connecthandle); 
       m_listmutex.release(); 

//使用示例,首先获取空闲句柄,利用这个句柄做真正的操作,然后再插回到空闲队列 
bool cdbmanager::deletehostcachebysessionid(char * sessionid) 

       mysql * pconnecthandle = getidleconnecthandle(); 
       if(!pconnecthandle) 
              return 0; 
       bool bret = deletehostcachebysessionid(pconnecthandle, sessionid); 
       setidleconnecthandle(pconnecthandle); 
       return bret; 

//传入空闲的句柄,做真正的删除操作 
bool cdbmanager::deletehostcachebysessionid(mysql * connecthandle, char * sessionid) 

       char deletesql[sql_length]; 
       memset(deletesql, 0, sizeof(deletesql)); 
       sprintf(deletesql,"delete from hostcache where sessionid = '%s'", sessionid); 
       if(mysql_query(connecthandle,deletesql) != 0) //删除 
              return false; 
       return true; 

3

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网