MySQL:Optimizing for Many Tables: 如果你的表太多, 怎办?

8.4.3.1HowMySQLOpensandClosesTables

Whenyou execute a mysqladmin status command,you should see something likethis:

Uptime:426Runningthreads:1Questions:11082

Reloads:1Opentables:12

TheOpentablesvalueof12can be somewhat puzzlingifyou have fewer than12tables.

MySQLismultithreaded,so there may be many clients issuing queriesfora given table simultaneously.Tominimize the problemwithmultiple client sessions having different states on the same table,the tableisopened independentlybyeach concurrent session.Thisuses additional memory but normally increases performance.WithMyISAMtables,one extra file descriptorisrequiredforthe data fileforeach client that has the table open.(Bycontrast,the index file descriptorisshared between all sessions.)

The table_open_cache and max_connections system variables affect the maximum number of files the server keeps open. If you increase one or both of these values, you may run up against a limit imposed by your operating system on the per-process number of open file descriptors. Many operating systems permit you to increase the open-files limit, although the method varies widely from system to system. Consult your operating system documentation to determine whether it is possible to increase the limit and how to do so.

table_open_cache is related to max_connections. For example, for 200 concurrent running connections, specify a table cache size of at least 200 * N, where N is the maximum number of tables per join in any of the queries which you execute. You must also reserve some extra file descriptors for temporary tables and files.Makesure that your operating system can handle the numberofopen file descriptors impliedbythe table_open_cache setting.Iftable_open_cacheissettoo high,MySQLmay runoutoffile descriptorsandexhibit symptoms suchasrefusing connectionsorfailing to perform queries.

Alsotakeintoaccount that theMyISAMstorage engine needs two file descriptorsforeach unique open table.Fora partitionedMyISAMtable,two file descriptors are requiredforeach partitionofthe opened table.(WhenMyISAMopens a partitioned table,it opens every partitionofthistable,whetherornota given partitionisactually used.SeeMyISAMandpartition file descriptor usage.)Toincrease the numberoffile descriptors available toMySQL,setthe open_files_limit system variable.SeeSectionB.4.2.17,"File Not Found and Similar Errors".

The cache of open tables is kept at a level of table_open_cache entries. The server autosizes the cache size at startup. To set the size explicitly, set the table_open_cache system variable at startup. MySQL may temporarily open more tables than this to execute queries, as described later in this section.

MySQL closes an unused table and removes it from the table cache under the following circumstances:

When the cache is full and a thread tries to open a table that is not in the cache.

When the cache contains more than table_open_cache entries and a table in the cache is no longer being used by any threads.Whena table-flushing operation occurs.Thishappenswhensomeone issues a FLUSH TABLES statementorexecutes a mysqladmin flush-tablesormysqladmin refresh command.

Whenthe table cache fills up,the server uses the following procedure to locate a cache entry touse:

Tables not currently in use are released, beginning with the table least recently used.Ifanewtable must be opened,but the cacheisfullandnotables can be released,the cacheistemporarily extendedasnecessary.Whenthe cacheisina temporarily extended stateanda table goesfroma used to unused state,the tableisclosedandreleasedfromthe cache.

AMyISAMtableisopenedforeach concurrent access.Thismeans the table needs to be opened twiceiftwo threads access the same tableorifa thread accesses the table twiceinthe same query(forexample,byjoining the table to itself).Eachconcurrent open requires an entryinthe table cache.Thefirst openofanyMyISAMtable takes two file descriptors:oneforthe data fileandoneforthe index file.Eachadditionaluseofthe table takes only one file descriptorforthe data file.Theindex file descriptorisshared among all threads.

If you are opening a table with the HANDLER tbl_name OPEN statement, a dedicated table object is allocated for the thread. This table object is not shared by other threads and is not closed until the thread calls HANDLER tbl_name CLOSE or the thread terminates. When this happens, the table is put back in the table cache (if the cache is not full). See Section 13.2.4, "HANDLER Statement".Todetermine whether your table cacheistoo small,check theOpened_tablesstatus variable,which indicates the numberoftable-opening operations since the server started:

MySQL>

SHOW GLOBAL STATUS LIKE'Opened_tables';

+---------------+-------+

|Variable_name|Value|

+---------------+-------+

|Opened_tables|2741|

+---------------+-------+

Ifthevalueisvery largeorincreases rapidly,evenwhenyou havenotissued many FLUSH TABLES statements,increase the table_open_cachevalueat server startup.

来源: http://www.bubuko.com/infodetail-3487445.html

mysql optimizing太久_MySQL:Optimizing for Many Tables: 如果你的表太多, 怎办?相关推荐

  1. mysql 锁24小时_MySQL中Alter table 不长时间锁表的情况汇总。

    查看原文:http://www.tanbo.name/html/99669.html 前言: MySQL 的大表运维总是令人头疼的一件事,特别是大表表结构的修改尤为困难. 首先,alter table ...

  2. mysql stop很久_mysql的timeout

    mysql的timeout 很多时候我们连接mysql会在timeout这里跌倒,这里明确下mysql的timeout: 下面是获取timeout的变量: mysql> show global ...

  3. mysql 计算非重复_MySQL Query:在一个非常庞大的表中计算重复值

    我有这个MySQL表: CREATE TABLE `triple` ( `id_one` int(11) NOT NULL,`id_two` int(11) NOT NULL,`id_three` i ...

  4. mysql 插入学生信息_MySQL经典50题-1-创建数据表和插入数据

    MySQL经典50题-1-创建数据表和插入数据 本文的整理和学习来自CSDN的一位博主,接下来的一个系列将是自己的学习和整理内容,提升MySQL. 同时解法会对网上的版本进行整理和综合,尽可能有多种答 ...

  5. mysql cte的好处_Mysql 8 重要新特性 - CTE 通用表表达式

    前言 Mysql 8 正式发布了,新增了很多优秀特性,之后我会挑些重点来分享. 下面和大家一起熟悉下CTE(Common Table Expressions)通用表表达式. CTE 是什么 派生表大家 ...

  6. mysql 头行关联_mysql实现一样变多行(表关联,批量实现)

    网上很多方法是采用split的方式,写函数,判断分隔符,来截取字段放到临时表,但这样,无法批量来处理一行变多行的问题,如一个log文件,里面的日志格式为: 1       a,b,c,d,2,3,4 ...

  7. mysql索引空间太大_MySQL优化索引

    1.  MySQL如何使用索引 索引用于快速查找具有特定列值的行.如果没有索引,MySQL必须从第一行开始,然后遍历整个表以找到相关的行.表越大,花费越多.如果表中有相关列的索引,MySQL可以快速确 ...

  8. mysql sql 一部分记录_MySQL性能优化实践(很全面,值得收藏)

    一 题记 最近公司项目添加新功能,上线后发现有些功能的列表查询时间很久.原因是新功能用到旧功能的接口,而这些旧接口的 SQL 查询语句关联5,6张表且编写不够规范,导致 MySQL 在执行 SQL 语 ...

  9. mysql sleep详解_mysql sleep链接过多的原因及解决办法

    今天收到运维同事短信,说有个线上业务"可能是数据库DB堵塞了,导致mysql链接过多,让我看一下". 回家后赶紧用家里vpn登录数据库服务器,show processlist 看了 ...

最新文章

  1. 根据相机外参实现单应矩阵计算的理论与实践
  2. 一起talk C栗子吧(第一百二十三回:C语言实例--显示变量和函数的地址)
  3. linux操作系统版本 3100,Linux操作系统默认打开文件数
  4. Android之面试题总结加强版(一)
  5. 怎样从red hat linux图形登录改成从文本登录
  6. 【openMV与机器视觉】四旋翼飞行控制背景下的PID控制与摄像头算法简介
  7. 学用软件:laTex软件初体验
  8. python根据频率画出词云_利用pandas+python制作100G亚马逊用户评论数据词云
  9. SRM 207 Div II Level Two: RegularSeason,字符串操作(sstream),多关键字排序(操作符重载)...
  10. zookeeper进行leader选举
  11. CreateProcess并隐藏窗口
  12. kivy python_另一款带有Kivy,Python的Android蛇
  13. webrtc音频QOS方法三(回声的产生及抑制)
  14. 第一章 JSON语法用法
  15. 企业如何选择一个好的网站设计公司?
  16. RFID固定资产管理降低人工成本,实现智能化的管理-新导智能
  17. 交换机access trunk hybrid模式详解
  18. 关于代码编辑器IDEA突然不能编辑文件的原因
  19. macOS中调整终端默认窗口大小
  20. 你知道RxJava也可以实现AsyncTask吗?

热门文章

  1. EFCore中的主键
  2. 打工人不再为难打工人,这款神器,竟是摸鱼党上班必备
  3. 微信小程序之云开发入门
  4. jquery ui放大缩小_jQuery UI 实例
  5. PIC 8位单片机的分类和特点
  6. 同时使用IP代理池和用户代理池(做笔记)
  7. wifinbsp;驱动nbsp;进阶11
  8. 5610连接MMMB并显示隐藏文件夹的方法
  9. welearn考试切屏会有显示吗_welearn视听说教程4答案截图
  10. 音乐变成伴奏软件 怎么把歌曲变成伴奏、消除人音