MySQL databaseBecause of its small size and efficient operations, it 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 my effective attempts to improve efficiency during use. I hope this will help you.
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.
The Code is as follows:
Reference content 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;
}
Here is a summary of this technique. More skills will be explained in the next article.