提高MySQL数据库查询效率的技巧(一)

数据库 MySQL
MySQL数据库在数据库种类中算是应用很广泛的数据库之一,MySQL数据库功能性不是很强大,但MySQL数据库凭借着自身的速度快,体积小,总造价成本低等优点,还是广受用户的喜爱,如今是个快速的社会,什么东西都讲求速度,讲求效率,拿么本文将为大家介绍提高MySQL数据库查询效率的几个技巧。

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

使用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;
}

这里就为大家总结这一个技巧,更多的技巧将会在下篇文章中为大家讲解。

【编辑推荐】

  1. 让MySQL数据库服务器支持远程连接
  2. 教你为MySQL数据库换挡加速
  3. 认识MySQL数据库对服务器端光标的限制
责任编辑:迎迎 来源: 赛迪网
相关推荐

2011-04-02 09:33:13

MySQL数据库查询效率

2011-04-02 09:33:08

MySQL数据库查询效率

2010-04-07 17:45:22

Oracle位图索引

2010-10-27 14:15:44

Oracle数据库效率

2010-08-27 13:27:50

DB2备份恢复

2011-08-03 18:01:54

MySQL数据库提高查询速度

2023-10-09 18:13:14

MySQL数据库查询

2009-05-15 10:11:55

数据库查询查询性能分页浏览

2011-04-11 16:50:13

Oracle数据库索引

2018-01-10 09:33:18

数据库SQL查询优化Java Web

2011-03-17 14:48:49

高级扫描数据库查询

2012-07-06 09:00:34

MySQL

2018-05-24 08:47:15

数据存储技巧

2009-06-30 15:54:00

数据库访问JSP

2010-05-27 17:16:20

MySQL数据库

2010-11-22 13:23:52

MySQL数据库优化

2010-05-31 14:50:49

MySQL数据库性能

2009-07-06 21:20:34

SQL Server数

2020-11-26 10:29:01

Redis

2013-01-04 10:00:12

MySQL数据库数据库查询优化
点赞
收藏

51CTO技术栈公众号