![MySQL数据库查询优化MySQL效率页面13 MySQL数据库查询优化MySQL效率页面13](/rjstyle/noimg/12.webp)
MySQL由于其体积小和运行效率高,在数据库应用中得到越来越广泛的应用。当我在一个P2P应用程序开发中使用了MySQL来保存P2P节点时,由于p2p的应用,成千上万的节点和节点频繁变化,所以一定要保持高效的查询并插入以下三个有效的。我试着在做的过程中提高效率。
l使用语句进行绑定查询
使用语句可以提前建立查询语法树,不需查询就能直接查询。它能很好地提高查询效率。该方法适用于查询条件固定但查询频繁的场合。
使用方法是:
结合,创造一个mysql_stmt变量绑定到相应的查询字符串,字符串中的问号代表要输入的变量,每个问号必须指定一个变量。
查询,输入每一个指定的变量,和传入的mysql_stmt变量执行可用的连接处理。
代码如下:
复制代码代码如下所示:
1。结合
布尔cdbmanager::BindInsertStmt(mysql * connecthandle)
{
使插入操作绑定
mysql_bind insertbind { feild_num };
如果(m_stinsertparam = null)
m_stinsertparam =新chostcachetable;
m_stinsertstmt = mysql_stmt_init(connecthandle);
构建/绑定字符串
insertsql sql_length } { char;
Strcpy(insertsql,插入hostcache(SessionID,channelid,ISPType,)
externalip,ExternalPort,internalip,internalport)。
值(,);
mysql_stmt_prepare(m_stinsertstmt,insertsql,strlen(insertsql));
国际param_count = mysql_stmt_param_count(m_stinsertstmt);
如果(param_count!= feild_num)
返回false;
结合/ m_sinsertparam充满结构数组,声明结构变量的关系
memset(insertbind,0,sizeof(insertbind));
insertbind { 0 }。buffer_type = mysql_type_string;
insertbind { 0 }。buffer_length = id_length 1 / * * /;
insertbind { 0 }。缓冲=(char *)m_stinsertparam -> SessionID;
insertbind { 0 }。is_null = 0;
insertbind { 0 }。长度= 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 }。长度= 0;
insertbind { 2 }。buffer_type = mysql_type_tiny;
insertbind { 2 }。缓冲=(char *)m_stinsertparam -> isptype;
insertbind { 2 }。is_null = 0;
insertbind { 2 }。长度= 0;
insertbind { 3 }。buffer_type = mysql_type_long;
insertbind { 3 }。缓冲=(char *)m_stinsertparam -> externalip;
insertbind { 3 }。is_null = 0;
insertbind { 3 }。长度= 0;
insertbind { 4 }。buffer_type = mysql_type_short;
insertbind { 4 }。缓冲=(char *)m_stinsertparam -> externalport;
insertbind { 4 }。is_null = 0;
insertbind { 4 }。长度= 0;
insertbind { 5 }。buffer_type = mysql_type_long;
insertbind { 5 }。缓冲=(char *)m_stinsertparam -> internalip;
insertbind { 5 }。is_null = 0;
insertbind { 5 }。长度= 0;
insertbind { 6 }。buffer_type = mysql_type_short;
insertbind { 6 }。缓冲=(char *)m_stinsertparam -> internalport;
insertbind { 6 }。is_null = 0;
insertbind { 6 }。is_null = 0;
绑定
如果(mysql_stmt_bind_param(m_stinsertstmt,insertbind))
返回false;
返回true;
}
2。查询
布尔cdbmanager::inserthostcache2(mysql * connecthandle,char * channelid SessionID,char *,int ISPtype,
unsigned int型、无符号短整型,短整型,IIP,iPORT)
{
/ /填充结构变量m_sinsertparam
Strcpy(m_stinsertparam -> SessionID,SessionID);
Strcpy(m_stinsertparam -> channelid,channelid);
m_stinsertparam -> isptype = isptype;
m_stinsertparam -> externalip = EIP;
m_stinsertparam -> externalport =报;
m_stinsertparam -> internalip = IIP;
m_stinsertparam -> internalport = iPORT;
执行语句,性能瓶颈
如果(mysql_stmt_execute(m_stinsertstmt))
返回false;
返回true;
}