怎么提高MySQL查詢效率

本篇內(nèi)容介紹了“怎么提高MySQL查詢效率”的有關(guān)知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細閱讀,能夠?qū)W有所成!

創(chuàng)新互聯(lián)于2013年創(chuàng)立,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項目成都網(wǎng)站制作、成都網(wǎng)站設(shè)計網(wǎng)站策劃,項目實施與項目整合能力。我們以讓每一個夢想脫穎而出為使命,1280元永新做網(wǎng)站,已為上家服務(wù),為永新各地企業(yè)和個人服務(wù),聯(lián)系電話:028-86922220

MySQL由于它本身的小巧和操作的高效, 在數(shù)據(jù)庫應(yīng)用中越來越多的被采用.我在開發(fā)一個P2P應(yīng)用的時候曾經(jīng)使用MySQL來保存P2P節(jié)點,由于P2P的應(yīng)用中,結(jié)點數(shù)動輒上萬個,而且節(jié)點變 化頻繁,因此一定要保持查詢和插入的高效.以下是我在使用過程中做的提高效率的三個有效的嘗試.
l       使用statement進行綁定查詢
使用statement可以提前構(gòu)建查詢語法樹,在查詢時不再需要構(gòu)建語法樹就直接查詢.因此可以很好的提高查詢的效率. 這個方法適合于查詢條件固定但查詢非常頻繁的場合.
使用方法是:
1.綁定, 創(chuàng)建一個MYSQL_STMT變量,與對應(yīng)的查詢字符串綁定,字符串中的問號代表要傳入的變量,每個問號都必須指定一個變量.
2.查詢, 輸入每個指定的變量, 傳入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;
}

“怎么提高MySQL查詢效率”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實用文章!

網(wǎng)站欄目:怎么提高MySQL查詢效率
URL鏈接:http://bm7419.com/article10/gosggo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計、移動網(wǎng)站建設(shè)、微信公眾號、外貿(mào)建站、品牌網(wǎng)站設(shè)計、網(wǎng)站排名

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站優(yōu)化排名