mysql數(shù)據(jù)庫查詢優(yōu)化 mysql效率第1/3頁
更新時間:2008年01月02日 18:55:37 作者:
MySQL由于它本身的小巧和操作的高效, 在數(shù)據(jù)庫應(yīng)用中越來越多的被采用.我在開發(fā)一個P2P應(yīng)用的時候曾經(jīng)使用MySQL來保存P2P節(jié)點(diǎn),由于P2P的應(yīng)用中,結(jié)點(diǎn)數(shù)動輒上萬個,而且節(jié)點(diǎn)變化頻繁,因此一定要保持查詢和插入的高效.以下是我在使用過程中做的提高效率的三個有效的嘗試. 1. 使用statement進(jìn)行綁定查詢 2. 隨機(jī)的獲取記錄 3. 使用連接池管理連接.
提高M(jìn)ySQL 查詢效率的三個技巧小結(jié)
MySQL由于它本身的小巧和操作的高效, 在數(shù)據(jù)庫應(yīng)用中越來越多的被采用.我在開發(fā)一個P2P應(yīng)用的時候曾經(jīng)使用MySQL來保存P2P節(jié)點(diǎn),由于P2P的應(yīng)用中,結(jié)點(diǎn)數(shù)動輒上萬個,而且節(jié)點(diǎn)變化頻繁,因此一定要保持查詢和插入的高效.以下是我在使用過程中做的提高效率的三個有效的嘗試.
l 使用statement進(jìn)行綁定查詢
使用statement可以提前構(gòu)建查詢語法樹,在查詢時不再需要構(gòu)建語法樹就直接查詢.因此可以很好的提高查詢的效率. 這個方法適合于查詢條件固定但查詢非常頻繁的場合.
使用方法是:
綁定, 創(chuàng)建一個MYSQL_STMT變量,與對應(yīng)的查詢字符串綁定,字符串中的問號代表要傳入的變量,每個問號都必須指定一個變量.
查詢, 輸入每個指定的變量, 傳入MYSQL_STMT變量用可用的連接句柄執(zhí)行.
代碼如下:
//1.綁定
bool CDBManager::BindInsertStmt(MYSQL * connecthandle)
{
//作插入操作的綁定
MYSQL_BIND insertbind[FEILD_NUM];
if(m_stInsertParam == NULL)
m_stInsertParam = new CHostCacheTable;
m_stInsertStmt = mysql_stmt_init(connecthandle);
//構(gòu)建綁定字符串
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;
//填充bind結(jié)構(gòu)數(shù)組, m_sInsertParam是這個statement關(guān)聯(lián)的結(jié)構(gòu)變量
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;
//綁定
if (mysql_stmt_bind_param(m_stInsertStmt, insertbind))
return false;
return true;
}
//2.查詢
bool CDBManager::InsertHostCache2(MYSQL * connecthandle, char * sessionid, char * channelid, int ISPtype, \
unsigned int eIP, unsigned short eport, unsigned int iIP, unsigned short iport)
{
//填充結(jié)構(gòu)變量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;
//執(zhí)行statement,性能瓶頸處
if(mysql_stmt_execute(m_stInsertStmt))
return false;
return true;
}
MySQL由于它本身的小巧和操作的高效, 在數(shù)據(jù)庫應(yīng)用中越來越多的被采用.我在開發(fā)一個P2P應(yīng)用的時候曾經(jīng)使用MySQL來保存P2P節(jié)點(diǎn),由于P2P的應(yīng)用中,結(jié)點(diǎn)數(shù)動輒上萬個,而且節(jié)點(diǎn)變化頻繁,因此一定要保持查詢和插入的高效.以下是我在使用過程中做的提高效率的三個有效的嘗試.
l 使用statement進(jìn)行綁定查詢
使用statement可以提前構(gòu)建查詢語法樹,在查詢時不再需要構(gòu)建語法樹就直接查詢.因此可以很好的提高查詢的效率. 這個方法適合于查詢條件固定但查詢非常頻繁的場合.
使用方法是:
綁定, 創(chuàng)建一個MYSQL_STMT變量,與對應(yīng)的查詢字符串綁定,字符串中的問號代表要傳入的變量,每個問號都必須指定一個變量.
查詢, 輸入每個指定的變量, 傳入MYSQL_STMT變量用可用的連接句柄執(zhí)行.
代碼如下:
復(fù)制代碼 代碼如下:
//1.綁定
bool CDBManager::BindInsertStmt(MYSQL * connecthandle)
{
//作插入操作的綁定
MYSQL_BIND insertbind[FEILD_NUM];
if(m_stInsertParam == NULL)
m_stInsertParam = new CHostCacheTable;
m_stInsertStmt = mysql_stmt_init(connecthandle);
//構(gòu)建綁定字符串
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;
//填充bind結(jié)構(gòu)數(shù)組, m_sInsertParam是這個statement關(guān)聯(lián)的結(jié)構(gòu)變量
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;
//綁定
if (mysql_stmt_bind_param(m_stInsertStmt, insertbind))
return false;
return true;
}
//2.查詢
bool CDBManager::InsertHostCache2(MYSQL * connecthandle, char * sessionid, char * channelid, int ISPtype, \
unsigned int eIP, unsigned short eport, unsigned int iIP, unsigned short iport)
{
//填充結(jié)構(gòu)變量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;
//執(zhí)行statement,性能瓶頸處
if(mysql_stmt_execute(m_stInsertStmt))
return false;
return true;
}
相關(guān)文章
史上最簡單的MySQL數(shù)據(jù)備份與還原教程(上)(三十五)
這篇文章主要為大家詳細(xì)介紹了史上最簡單的MySQL數(shù)據(jù)備份與還原教程第一篇,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-10-10MySQL同步數(shù)據(jù)Replication的實(shí)現(xiàn)步驟
本文主要介紹了MySQL同步數(shù)據(jù)Replication的實(shí)現(xiàn)步驟,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03Linux(Ubuntu)下mysql5.7.17安裝配置方法圖文教程
這篇文章主要為大家詳細(xì)介紹了Linux下mysql5.7.17安裝配置方法圖文教程,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-01-01MySQL的多版本并發(fā)控制MVCC的實(shí)現(xiàn)
MVCC就是多版本并發(fā)控制,本文主要介紹了MySQL的多版本并發(fā)控制MVCC的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-12-12mysql 數(shù)據(jù)庫取前后幾秒 幾分鐘 幾小時 幾天的語句
這篇文章主要介紹了mysql 數(shù)據(jù)庫中取前后幾秒 幾分鐘 幾小時 幾天的語句,需要的朋友可以參考下2018-01-01