mysql數(shù)據(jù)庫(kù)查詢(xún)優(yōu)化 mysql效率第2/3頁(yè)
更新時(shí)間:2008年01月02日 18:55:37 作者:
MySQL由于它本身的小巧和操作的高效, 在數(shù)據(jù)庫(kù)應(yīng)用中越來(lái)越多的被采用.我在開(kāi)發(fā)一個(gè)P2P應(yīng)用的時(shí)候曾經(jīng)使用MySQL來(lái)保存P2P節(jié)點(diǎn),由于P2P的應(yīng)用中,結(jié)點(diǎn)數(shù)動(dòng)輒上萬(wàn)個(gè),而且節(jié)點(diǎn)變化頻繁,因此一定要保持查詢(xún)和插入的高效.以下是我在使用過(guò)程中做的提高效率的三個(gè)有效的嘗試. 1. 使用statement進(jìn)行綁定查詢(xún) 2. 隨機(jī)的獲取記錄 3. 使用連接池管理連接.
隨機(jī)的獲取記錄
在某些數(shù)據(jù)庫(kù)的應(yīng)用中, 我們并不是要獲取所有的滿足條件的記錄,而只是要隨機(jī)挑選出滿足條件的記錄. 這種情況常見(jiàn)于數(shù)據(jù)業(yè)務(wù)的統(tǒng)計(jì)分析,從大容量數(shù)據(jù)庫(kù)中獲取小量的數(shù)據(jù)的場(chǎng)合.
有兩種方法可以做到
1. 常規(guī)方法,首先查詢(xún)出所有滿足條件的記錄,然后隨機(jī)的挑選出部分記錄.這種方法在滿足條件的記錄數(shù)很多時(shí)效果不理想.
2. 使用limit語(yǔ)法,先獲取滿足條件的記錄條數(shù), 然后在sql查詢(xún)語(yǔ)句中加入limit來(lái)限制只查詢(xún)滿足要求的一段記錄. 這種方法雖然要查詢(xún)兩次,但是在數(shù)據(jù)量大時(shí)反而比較高效.
示例代碼如下:
復(fù)制代碼 代碼如下:
//1.常規(guī)的方法
//性能瓶頸,10萬(wàn)條記錄時(shí),執(zhí)行查詢(xún)140ms, 獲取結(jié)果集500ms,其余可忽略
int CDBManager::QueryHostCache(MYSQL* connecthandle, char * channelid, int ISPtype, CDBManager::CHostCacheTable * &hostcache)
{
char selectSQL[SQL_LENGTH];
memset(selectSQL, 0, sizeof(selectSQL));
sprintf(selectSQL,"select * from HostCache where ChannelID = '%s' and ISPtype = %d", channelid, ISPtype);
if(mysql_real_query(connecthandle, selectSQL, strlen(selectSQL)) != 0) //檢索
return 0;
//獲取結(jié)果集
m_pResultSet = mysql_store_result(connecthandle);
if(!m_pResultSet) //獲取結(jié)果集出錯(cuò)
return 0;
int iAllNumRows = (int)(mysql_num_rows(m_pResultSet)); ///<所有的搜索結(jié)果數(shù)
//計(jì)算待返回的結(jié)果數(shù)
int iReturnNumRows = (iAllNumRows <= RETURN_QUERY_HOST_NUM)? iAllNumRows:RETURN_QUERY_HOST_NUM;
if(iReturnNumRows <= RETURN_QUERY_HOST_NUM)
{
//獲取逐條記錄
for(int i = 0; i<iReturnNumRows; i++)
{
//獲取逐個(gè)字段
m_Row = mysql_fetch_row(m_pResultSet);
if(m_Row[0] != NULL)
strcpy(hostcache[i].sessionid, m_Row[0]);
if(m_Row[1] != NULL)
strcpy(hostcache[i].channelid, m_Row[1]);
if(m_Row[2] != NULL)
hostcache[i].ISPtype = atoi(m_Row[2]);
if(m_Row[3] != NULL)
hostcache[i].externalIP = atoi(m_Row[3]);
if(m_Row[4] != NULL)
hostcache[i].externalPort = atoi(m_Row[4]);
if(m_Row[5] != NULL)
hostcache[i].internalIP = atoi(m_Row[5]);
if(m_Row[6] != NULL)
hostcache[i].internalPort = atoi(m_Row[6]);
}
}
else
{
//隨機(jī)的挑選指定條記錄返回
int iRemainder = iAllNumRows%iReturnNumRows; ///<余數(shù)
int iQuotient = iAllNumRows/iReturnNumRows; ///<商
int iStartIndex = rand()%(iRemainder + 1); ///<開(kāi)始下標(biāo)
//獲取逐條記錄
for(int iSelectedIndex = 0; iSelectedIndex < iReturnNumRows; iSelectedIndex++)
{
mysql_data_seek(m_pResultSet, iStartIndex + iQuotient * iSelectedIndex);
m_Row = mysql_fetch_row(m_pResultSet);
if(m_Row[0] != NULL)
strcpy(hostcache[iSelectedIndex].sessionid, m_Row[0]);
if(m_Row[1] != NULL)
strcpy(hostcache[iSelectedIndex].channelid, m_Row[1]);
if(m_Row[2] != NULL)
hostcache[iSelectedIndex].ISPtype = atoi(m_Row[2]);
if(m_Row[3] != NULL)
hostcache[iSelectedIndex].externalIP = atoi(m_Row[3]);
if(m_Row[4] != NULL)
hostcache[iSelectedIndex].externalPort = atoi(m_Row[4]);
if(m_Row[5] != NULL)
hostcache[iSelectedIndex].internalIP = atoi(m_Row[5]);
if(m_Row[6] != NULL)
hostcache[iSelectedIndex].internalPort = atoi(m_Row[6]);
}
}
//釋放結(jié)果集內(nèi)容
mysql_free_result(m_pResultSet);
return iReturnNumRows;
}
//2.使用limit版
int CDBManager::QueryHostCache(MYSQL * connecthandle, char * channelid, unsigned int myexternalip, int ISPtype, CHostCacheTable * hostcache)
{
//首先獲取滿足結(jié)果的記錄條數(shù),再使用limit隨機(jī)選擇指定條記錄返回
MYSQL_ROW row;
MYSQL_RES * pResultSet;
char selectSQL[SQL_LENGTH];
memset(selectSQL, 0, sizeof(selectSQL));
sprintf(selectSQL,"select count(*) from HostCache where ChannelID = '%s' and ISPtype = %d", channelid, ISPtype);
if(mysql_real_query(connecthandle, selectSQL, strlen(selectSQL)) != 0) //檢索
return 0;
pResultSet = mysql_store_result(connecthandle);
if(!pResultSet)
return 0;
row = mysql_fetch_row(pResultSet);
int iAllNumRows = atoi(row[0]);
mysql_free_result(pResultSet);
//計(jì)算待取記錄的上下范圍
int iLimitLower = (iAllNumRows <= RETURN_QUERY_HOST_NUM)?
0:(rand()%(iAllNumRows - RETURN_QUERY_HOST_NUM));
int iLimitUpper = (iAllNumRows <= RETURN_QUERY_HOST_NUM)?
iAllNumRows:(iLimitLower + RETURN_QUERY_HOST_NUM);
//計(jì)算待返回的結(jié)果數(shù)
int iReturnNumRows = (iAllNumRows <= RETURN_QUERY_HOST_NUM)?
iAllNumRows:RETURN_QUERY_HOST_NUM;
//使用limit作查詢(xún)
sprintf(selectSQL,"select SessionID, ExternalIP, ExternalPort, InternalIP, InternalPort "
"from HostCache where ChannelID = '%s' and ISPtype = %d limit %d, %d"
, channelid, ISPtype, iLimitLower, iLimitUpper);
if(mysql_real_query(connecthandle, selectSQL, strlen(selectSQL)) != 0) //檢索
return 0;
pResultSet = mysql_store_result(connecthandle);
if(!pResultSet)
return 0;
//獲取逐條記錄
for(int i = 0; i<iReturnNumRows; i++)
{
//獲取逐個(gè)字段
row = mysql_fetch_row(pResultSet);
if(row[0] != NULL)
strcpy(hostcache[i].sessionid, row[0]);
if(row[1] != NULL)
hostcache[i].externalIP = atoi(row[1]);
if(row[2] != NULL)
hostcache[i].externalPort = atoi(row[2]);
if(row[3] != NULL)
hostcache[i].internalIP = atoi(row[3]);
if(row[4] != NULL)
hostcache[i].internalPort = atoi(row[4]);
}
//釋放結(jié)果集內(nèi)容
mysql_free_result(pResultSet);
return iReturnNumRows;
}
相關(guān)文章
SQL實(shí)現(xiàn)LeetCode(196.刪除重復(fù)郵箱)
這篇文章主要介紹了SQL實(shí)現(xiàn)LeetCode(196.刪除重復(fù)郵箱),本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08
Mysql BinLog存儲(chǔ)機(jī)制與數(shù)據(jù)恢復(fù)方式
這篇文章主要介紹了Mysql BinLog存儲(chǔ)機(jī)制與數(shù)據(jù)恢復(fù)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-06-06
MYSQL Left Join優(yōu)化(10秒優(yōu)化到20毫秒內(nèi))
在實(shí)際開(kāi)發(fā)中,相信大多數(shù)人都會(huì)用到j(luò)oin進(jìn)行連表查詢(xún),但是有些人發(fā)現(xiàn),用join好像效率很低,而且驅(qū)動(dòng)表不同,執(zhí)行時(shí)間也不同。那么join到底是如何執(zhí)行的呢,本文就詳細(xì)的介紹一下2021-12-12
同時(shí)運(yùn)行多個(gè)MySQL服務(wù)器的方法
在同一臺(tái)機(jī)器上運(yùn)行多個(gè)有些情況下你可能想要在同一臺(tái)機(jī)器上運(yùn)行多個(gè)服務(wù)器。例如,你可能想要測(cè)試一個(gè)新的MySQL版本而讓你現(xiàn)有生產(chǎn)系統(tǒng)的設(shè)置不受到干擾, 或你可能是想要為不同的客戶提供獨(dú)立的MySQL安裝一個(gè)因特網(wǎng)服務(wù)供應(yīng)商。2008-05-05
Mysql下自動(dòng)刪除指定時(shí)間以前的記錄的操作方法
這篇文章主要介紹了MySQL下自動(dòng)刪除指定時(shí)間以前的記錄的操作方法,需要的朋友可以參考下2018-08-08
利用JuiceFS使MySQL?備份驗(yàn)證性能提升?10?倍
這篇文章主要介紹了如何讓?MySQL?備份驗(yàn)證性能提升?10?倍,JuiceFS?非常適合用來(lái)做?MySQL?物理備份,通過(guò)不斷調(diào)整?XtraBackup?的參數(shù)和?JuiceFS?的掛載參數(shù),在一個(gè)小時(shí)內(nèi)將時(shí)間縮短到原先的?1/10,下文一起來(lái)看相關(guān)內(nèi)容的詳細(xì)介紹吧2022-03-03

