Three Tips for improving MySQL query efficiency
Because of its small size and efficient operations, MySQL is increasingly used in database applications. when I was developing a P2P application, I used MySQL to store P2P nodes. Because P2P applications have tens of thousands knots, and nodes change frequently, therefore, you must ensure efficient query and insertion. the following are three effective attempts to improve efficiency during use.
L use statement for binding Query
You can use statement to build a query syntax tree in advance. You do not need to build a syntax tree to directly query a query. therefore, the query efficiency can be improved. this method is suitable for scenarios with fixed query conditions but frequent queries.
The usage is as follows:
Create a mysql_stmt variable and bind it to the corresponding query string. The question mark in the string indicates the variable to be passed in. Each question mark must specify a variable.
Query, enter each specified variable, and input the mysql_stmt variable to be executed using the available connection handle.
Code As follows: Copy code The Code is as follows: // 1. Bind
Bool cdbmanager: bindinsertstmt (MySQL * connecthandle)
{
// Insert binding
Mysql_bind insertbind [feild_num];
If (m_stinsertparam = NULL)
M_stinsertparam = new chostcachetable;
M_stinsertstmt = mysql_stmt_init (connecthandle );
// Construct the BIND string
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;
// Fill in the BIND structure array. m_sinsertparam is the structure variable associated with this 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;
// bind
If (mysql_stmt_bind_param (m_stinsertstmt, insertbind)
return false;
return true;
}
// 2. Query
Bool cdbmanager: inserthostcache2 (MySQL * connecthandle, char * sessionid, char * channelid, int isptype ,\
Unsigned int EIP, unsigned short eport, unsigned int IIP, unsigned short iport)
{
// Fill in the Structure Variable 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;
// Execute statement, performance bottleneck
If (mysql_stmt_execute (m_stinsertstmt ))
Return false;
Return true;
}