跳至正文
-
收藏很文博客的最新网址 www.hw97.com,不错过任何精彩内容。
很文博客 很文博客 很文博客

生活点滴 贵在分享

很文博客 很文博客 很文博客

生活点滴 贵在分享

  • 首页
  • 站务联系
  • 首页
  • 站务联系
关

搜索

数据库

mysql数据库查询优化 mysql效率

2018年5月30日
0
MySQL由于它本身的小巧和操作的高效, 在数据库应用中越来越多的被采用.我在开发一个P2P应用的时候曾经使用MySQL来保存P2P节点,由于P2P的应用中,结点数动辄上万个,而且节点变化频繁,因此一定要保持查询和插入的高效.以下是我在使用过程中做的提高效率的三个有效的尝试. 1. 使用statement进行绑定查询 2. 随机的获取记录 3. 使用连接池管理连接.
提高MySQL 查询效率的三个技巧小结
MySQL由于它本身的小巧和操作的高效, 在数据库应用中越来越多的被采用.我在开发一个P2P应用的时候曾经使用MySQL来保存P2P节点,由于P2P的应用中,结点数动辄上万个,而且节点变化频繁,因此一定要保持查询和插入的高效.以下是我在使用过程中做的提高效率的三个有效的尝试.l        使用statement进行绑定查询
使用statement可以提前构建查询语法树,在查询时不再需要构建语法树就直接查询.因此可以很好的提高查询的效率. 这个方法适合于查询条件固定但查询非常频繁的场合.
使用方法是:
绑定, 创建一个MYSQL_STMT变量,与对应的查询字符串绑定,字符串中的问号代表要传入的变量,每个问号都必须指定一个变量.
查询, 输入每个指定的变量, 传入MYSQL_STMT变量用可用的连接句柄执行.
代码如下:
  1. //1.绑定  
  2. bool CDBManager::BindInsertStmt(MYSQL * connecthandle)
  3. {
  4.        //作插入操作的绑定  
  5.        MYSQL_BIND insertbind[FEILD_NUM];
  6.        if(m_stInsertParam == NULL)
  7.               m_stInsertParam = new CHostCacheTable;
  8.        m_stInsertStmt = mysql_stmt_init(connecthandle);
  9.        //构建绑定字符串  
  10.        char insertSQL[SQL_LENGTH];
  11.        strcpy(insertSQL, "insert into HostCache(SessionID, ChannelID, ISPType, "
  12.               "ExternalIP, ExternalPort, InternalIP, InternalPort) "
  13.               "values(?, ?, ?, ?, ?, ?, ?)");
  14.        mysql_stmt_prepare(m_stInsertStmt, insertSQL, strlen(insertSQL));
  15.        int param_count= mysql_stmt_param_count(m_stInsertStmt);
  16.        if(param_count != FEILD_NUM)
  17.               return false;
  18.        //填充bind结构数组, m_sInsertParam是这个statement关联的结构变量  
  19.        memset(insertbind, 0, sizeof(insertbind));
  20.        insertbind[0].buffer_type = MYSQL_TYPE_STRING;
  21.        insertbind[0].buffer_length = ID_LENGTH /* -1 */;
  22.        insertbind[0].buffer = (char *)m_stInsertParam->sessionid;
  23.        insertbind[0].is_null = 0;
  24.        insertbind[0].length = 0;
  25.        insertbind[1].buffer_type = MYSQL_TYPE_STRING;
  26.        insertbind[1].buffer_length = ID_LENGTH /* -1 */;
  27.        insertbind[1].buffer = (char *)m_stInsertParam->channelid;
  28.        insertbind[1].is_null = 0;
  29.        insertbind[1].length = 0;
  30.        insertbind[2].buffer_type = MYSQL_TYPE_TINY;
  31.        insertbind[2].buffer = (char *)&m_stInsertParam->ISPtype;
  32.        insertbind[2].is_null = 0;
  33.        insertbind[2].length = 0;
  34.        insertbind[3].buffer_type = MYSQL_TYPE_LONG;
  35.        insertbind[3].buffer = (char *)&m_stInsertParam->externalIP;
  36.        insertbind[3].is_null = 0;
  37.        insertbind[3].length = 0;
  38.        insertbind[4].buffer_type = MYSQL_TYPE_SHORT;
  39.        insertbind[4].buffer = (char *)&m_stInsertParam->externalPort;
  40.        insertbind[4].is_null = 0;
  41.        insertbind[4].length = 0;
  42.        insertbind[5].buffer_type = MYSQL_TYPE_LONG;
  43.        insertbind[5].buffer = (char *)&m_stInsertParam->internalIP;
  44.        insertbind[5].is_null = 0;
  45.        insertbind[5].length = 0;
  46.        insertbind[6].buffer_type = MYSQL_TYPE_SHORT;
  47.        insertbind[6].buffer = (char *)&m_stInsertParam->internalPort;
  48.        insertbind[6].is_null = 0;
  49.        insertbind[6].is_null = 0;
  50.        //绑定  
  51.        if (mysql_stmt_bind_param(m_stInsertStmt, insertbind))
  52.               return false;
  53.        return true;
  54. }
  55. //2.查询  
  56. bool CDBManager::InsertHostCache2(MYSQL * connecthandle, char * sessionid, char * channelid, int ISPtype, \
  57.               unsigned int eIP, unsigned short eport, unsigned int iIP, unsigned short iport)
  58. {
  59.        //填充结构变量m_sInsertParam  
  60.        strcpy(m_stInsertParam->sessionid, sessionid);
  61.        strcpy(m_stInsertParam->channelid, channelid);
  62.        m_stInsertParam->ISPtype = ISPtype;
  63.        m_stInsertParam->externalIP = eIP;
  64.        m_stInsertParam->externalPort = eport;
  65.        m_stInsertParam->internalIP = iIP;
  66.        m_stInsertParam->internalPort = iport;
  67.        //执行statement,性能瓶颈处  
  68.        if(mysql_stmt_execute(m_stInsertStmt))
  69.               return false;
  70.        return true;
  71. }

页: 1 2

标签:

MySQL数据库查询优化
作者

很文博客

关注我
其他文章
上一个

MySQL 如何从表中取出随机数据

下一个

mysql创建Bitmap_Join_Indexes中的约束与索引

暂无评论!成为第一个。

发表回复 取消回复

您的邮箱地址不会被公开。 必填项已用 * 标注

近期浏览最多文章

  • 作品观看平台+科学上网工具(不定期更新……) - 5,816 浏览
  • 「博多彩叶」出道作品番号及封面,「博多彩叶」个人简介 - 2,261 浏览
  • 「河北彩伽」最新作品SNOS-056介绍及封面预览 - 2,086 浏览
  • 森日向子(Mori-Hinako)最新作品MIAB-021介绍及封面预览 - 1,928 浏览
  • 「河北彩伽」最新作品SNOS-003介绍及封面预览 - 1,779 浏览

热门标签

Julia miru MySQL 三上悠亚 三上悠亜 习惯 伊藤舞雪 传奇故事 动物故事 励志名言 励志故事 危机故事 哲学哲理 哲理小语 哲理故事 寓言故事 小花のん 小花暖 希腊寓言故事 帝王故事 心情说说 惊险故事 战争故事 明里つむぎ 明里䌷 松本いちか 枫富爱 桃乃木かな 桃乃木香奈 桜空もも 梦乃あいか 樱空桃 欢迎词 河北彩花 波多野结衣 生日祝福语 生活小语 祝福短信 祝福语 神宫寺ナオ 神宫寺奈绪 童话故事 美谷朱里 自然人文 藤森里穂
Copyright 2026 — 很文博客. All rights reserved. Sitemap |站务联系 |备用网址