検索
ホームページデータベースmysql チュートリアルmysql数据库查询优化 mysql效率第1/3页

MySQL由于它本身的小巧和操作的高效, 在数据库应用中越来越多的被采用.我在开发一个P2P应用的时候曾经使用MySQL来保存P2P节点,由于P2P的应用中,结点数动辄上万个,而且节点变化频繁,因此一定要保持查询和插入的高效.以下是我在使用过程中做的提高效率的三个有

提高MySQL 查询效率的三个技巧小结
MySQL由于它本身的小巧和操作的高效, 在数据库应用中越来越多的被采用.我在开发一个P2P应用的时候曾经使用MySQL来保存P2P节点,由于P2P的应用中,结点数动辄上万个,而且节点变化频繁,因此一定要保持查询和插入的高效.以下是我在使用过程中做的提高效率的三个有效的尝试.

l 使用statement进行绑定查询
使用statement可以提前构建查询语法树,在查询时不再需要构建语法树就直接查询.因此可以很好的提高查询的效率. 这个方法适合于查询条件固定但查询非常频繁的场合.
使用方法是:
绑定, 创建一个MYSQL_STMT变量,与对应的查询字符串绑定,字符串中的问号代表要传入的变量,每个问号都必须指定一个变量.
查询, 输入每个指定的变量, 传入MYSQL_STMT变量用可用的连接句柄执行.
代码如下:
代码如下:
//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);
//构建绑定字符串
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结构数组, m_sInsertParam是这个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;
//绑定
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)
{
//填充结构变量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;
//执行statement,性能瓶颈处
if(mysql_stmt_execute(m_stInsertStmt))
return false;
return true;
}

声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
mysql blob:制限はありますか?mysql blob:制限はありますか?May 08, 2025 am 12:22 AM

mysqlblobshavelimits:tinyblob(255bytes)、blob(65,535bytes)、mediumblob(16,777,215bytes)、andlongblob(4,294,967,295bytes).tousebl難易度:1)PROFFORMANCESANDSTORERGEBLOBSEXTERNALLY;

MySQL:ユーザーの作成を自動化するための最良のツールは何ですか?MySQL:ユーザーの作成を自動化するための最良のツールは何ですか?May 08, 2025 am 12:22 AM

MySQLでユーザーの作成を自動化するための最良のツールとテクノロジーには、次のものがあります。1。MySQLWorkBench、中小サイズの環境に適した、使いやすいがリソース消費量が高い。 2。アンシブル、マルチサーバー環境に適した、シンプルだが急な学習曲線。 3.カスタムPythonスクリプト、柔軟性がありますが、スクリプトセキュリティを確保する必要があります。 4。大規模な環境に適した人形とシェフ、複雑ですがスケーラブル。選択する際には、スケール、学習曲線、統合のニーズを考慮する必要があります。

mysql:blob内で検索できますか?mysql:blob内で検索できますか?May 08, 2025 am 12:20 AM

はい、youcansearchinsideablobinmysqlusingspecifictechniques.1)converttheblobtoautf-8stringwithconvert function andsearchusinglike.2)

MySQL文字列データ型:包括的なガイドMySQL文字列データ型:包括的なガイドMay 08, 2025 am 12:14 AM

mysqloffersvariousstringdatypes:1)charfofixed-lengthstrings、italforconsentlengtalikecountrycodes; 2)varcharforvariable-lengthstrings、適切なForfieldslikenames;

MySQLブロブのマスター:ステップバイステップのチュートリアルMySQLブロブのマスター:ステップバイステップのチュートリアルMay 08, 2025 am 12:01 AM

tomastermysqlblobs、soflowthesesteps:1)shoseetheapsosupturateblobtype(tinyblob、blob、mediumblob、longblob)basedOndatasize.2)insertDatausingload_fileforefficiency.3)storefilereferenceinsinsteadoffilestoimpeperformance.4)

MySQLのBLOBデータ型:開発者の詳細な概要MySQLのBLOBデータ型:開発者の詳細な概要May 07, 2025 pm 05:41 PM

blobdatatypesinmysqlareusedlarginglaredatalikeimagesorudio.1)useblobtypes(tinyblobtolongblob)Basedatasizeneeds。 2)storeblobsin perplate petooptimize performance.3)scondididididididididersxternalストレージBlob Romanaデータベースindimprovebackupe

コマンドラインからMySQLにユーザーを追加する方法コマンドラインからMySQLにユーザーを追加する方法May 07, 2025 pm 05:01 PM

toadduserstomysqlfromthecommandline、loginasroot、thenusecreateuser'username '@' host'ident'ident'identifidedby'password '; tocreateanewuser.grantpermissions with grantpermissions with grantalgegesondatabase

mysqlの文字列データ型は何ですか?詳細な概要mysqlの文字列データ型は何ですか?詳細な概要May 07, 2025 pm 03:33 PM

mysqlofferseightStringDatatypes:char、varchar、binary、varbinary、blob、text、enum、andset.1)charisfixed-length、yealforconsistent datalikecountrycodes.2)varcharisvariable length、efficational forvaryingdatalikenames.3)binaryandvanterbinarydata a similati

See all articles

ホットAIツール

Undresser.AI Undress

Undresser.AI Undress

リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover

AI Clothes Remover

写真から衣服を削除するオンライン AI ツール。

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

Video Face Swap

Video Face Swap

完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

ホットツール

MantisBT

MantisBT

Mantis は、製品の欠陥追跡を支援するために設計された、導入が簡単な Web ベースの欠陥追跡ツールです。 PHP、MySQL、Web サーバーが必要です。デモおよびホスティング サービスをチェックしてください。

mPDF

mPDF

mPDF は、UTF-8 でエンコードされた HTML から PDF ファイルを生成できる PHP ライブラリです。オリジナルの作者である Ian Back は、Web サイトから「オンザフライ」で PDF ファイルを出力し、さまざまな言語を処理するために mPDF を作成しました。 HTML2FPDF などのオリジナルのスクリプトよりも遅く、Unicode フォントを使用すると生成されるファイルが大きくなりますが、CSS スタイルなどをサポートし、多くの機能強化が施されています。 RTL (アラビア語とヘブライ語) や CJK (中国語、日本語、韓国語) を含むほぼすべての言語をサポートします。ネストされたブロックレベル要素 (P、DIV など) をサポートします。

SublimeText3 Linux 新バージョン

SublimeText3 Linux 新バージョン

SublimeText3 Linux 最新バージョン

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser は、オンライン試験を安全に受験するための安全なブラウザ環境です。このソフトウェアは、あらゆるコンピュータを安全なワークステーションに変えます。あらゆるユーティリティへのアクセスを制御し、学生が無許可のリソースを使用するのを防ぎます。

SecLists

SecLists

SecLists は、セキュリティ テスターの究極の相棒です。これは、セキュリティ評価中に頻繁に使用されるさまざまな種類のリストを 1 か所にまとめたものです。 SecLists は、セキュリティ テスターが必要とする可能性のあるすべてのリストを便利に提供することで、セキュリティ テストをより効率的かつ生産的にするのに役立ちます。リストの種類には、ユーザー名、パスワード、URL、ファジング ペイロード、機密データ パターン、Web シェルなどが含まれます。テスターはこのリポジトリを新しいテスト マシンにプルするだけで、必要なあらゆる種類のリストにアクセスできるようになります。