Mysql:Optimizing for Many Tables:如果你的表太多,怎办?
Some techniques for keeping individual queries fast involve splitting data across many tables. When the number of tables runs into the thousands or even millions, the overhead of dealing with all these tables becomes a new performance consideration.
When you execute a mysqladmin status command, you should see something like this:
Uptime: 426 Running threads: 1 Questions: 11082 Reloads: 1 Open tables: 12
The Open tables
value of 12 can be somewhat puzzling if you have fewer than 12 tables.
MySQL is multithreaded, so there may be many clients issuing queries for a given table simultaneously. To minimize the problem with multiple client sessions having different states on the same table, the table is opened independently by each concurrent session. This uses additional memory but normally increases performance. With MyISAM
tables, one extra file descriptor is required for the data file for each client that has the table open. (By contrast, the index file descriptor is shared 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 *
, where N
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.
Make sure that your operating system can handle the number of open file descriptors implied by the table_open_cache
setting. If table_open_cache
is set too high, MySQL may run out of file descriptors and exhibit symptoms such as refusing connections or failing to perform queries.
Also take into account that the MyISAM
storage engine needs two file descriptors for each unique open table. For a partitioned MyISAM
table, two file descriptors are required for each partition of the opened table. (When MyISAM
opens a partitioned table, it opens every partition of this table, whether or not a given partition is actually used. See MyISAM and partition file descriptor usage.) To increase the number of file descriptors available to MySQL, set the open_files_limit
system variable. See Section B.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. -
When a table-flushing operation occurs. This happens when someone issues a
FLUSH TABLES
statement or executes a mysqladmin flush-tables or mysqladmin refresh command.
When the table cache fills up, the server uses the following procedure to locate a cache entry to use:
-
Tables not currently in use are released, beginning with the table least recently used.
-
If a new table must be opened, but the cache is full and no tables can be released, the cache is temporarily extended as necessary. When the cache is in a temporarily extended state and a table goes from a used to unused state, the table is closed and released from the cache.
A MyISAM
table is opened for each concurrent access. This means the table needs to be opened twice if two threads access the same table or if a thread accesses the table twice in the same query (for example, by joining the table to itself). Each concurrent open requires an entry in the table cache. The first open of any MyISAM
table takes two file descriptors: one for the data file and one for the index file. Each additional use of the table takes only one file descriptor for the data file. The index file descriptor is shared among all threads.
If you are opening a table with the HANDLER
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 tbl_name
OPENHANDLER
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”.tbl_name
CLOSE
To determine whether your table cache is too small, check the Opened_tables
status variable, which indicates the number of table-opening operations since the server started:
mysql> SHOW GLOBAL STATUS LIKE 'Opened_tables';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| Opened_tables | 2741 |
+---------------+-------+
If the value is very large or increases rapidly, even when you have not issued many FLUSH TABLES
statements, increase the table_open_cache
value at server startup.
If you have many MyISAM
tables in the same database directory, open, close, and create operations are slow. If you execute SELECT
statements on many different tables, there is a little overhead when the table cache is full, because for every table that has to be opened, another must be closed. You can reduce this overhead by increasing the number of entries permitted in the table cache.
原文地址:https://www.cnblogs.com/jinzhenshui/p/12615066.html
- Angularjs中UI Router超级详细的教程{{上}}
- 【LEETCODE】模拟面试-120- Triangle
- Spring Cloud Edgware新特性之四:Zuul routes端点功能增强
- Spring Cloud Edgware新特性之三:使用配置属性自定义Feign的行为
- 碎片化 | 第四阶段-43-struts2拦截器interceptor-视频
- Spring Cloud Edgware新特性之二:如何配置Zuul的Hystrix线程池
- 碎片化 | 第四阶段-44-struts2注解使用-视频
- Spring Cloud Edgware新特性之:解决Eureka中Jersey 1.x版本过旧的问题-不使用Jersey
- 如何自定义微服务的Instance ID
- 怎样做情感分析
- Python|编写自己的类
- 碎片化 | 第四阶段-45-session为空问题解决-视频
- 以后有机会写框架用得着的
- 合并两个不相关的Git仓库
- MySQL 教程
- MySQL 安装
- MySQL 管理与配置
- MySQL PHP 语法
- MySQL 连接
- MySQL 创建数据库
- MySQL 删除数据库
- MySQL 选择数据库
- MySQL 数据类型
- MySQL 创建数据表
- MySQL 删除数据表
- MySQL 插入数据
- MySQL 查询数据
- MySQL where 子句
- MySQL UPDATE 查询
- MySQL DELETE 语句
- MySQL LIKE 子句
- mysql order by
- Mysql Join的使用
- MySQL NULL 值处理
- MySQL 正则表达式
- MySQL 事务
- MySQL ALTER命令
- MySQL 索引
- MySQL 临时表
- MySQL 复制表
- 查看MySQL 元数据
- MySQL 序列 AUTO_INCREMENT
- MySQL 处理重复数据
- MySQL 及 SQL 注入
- MySQL 导出数据
- MySQL 导入数据
- MYSQL 函数大全
- MySQL Group By 实例讲解
- MySQL Max()函数实例讲解
- mysql count函数实例
- MYSQL UNION和UNION ALL实例
- MySQL IN 用法
- MySQL between and 实例讲解
- spring security oauth2 资源服务/客户端无法正确获取权限
- EXTJS grid.column.renderer绑定失效
- Head First设计模式——装饰者模式
- spring security oauth2 资源服务器WebAsyncTask/DeferredResult接口调用报错InsufficientAuthenticationException
- Head First设计模式——简单工厂、工厂、抽象工厂
- Golang | 简介channel常见用法,完成goroutin通信
- Head First设计模式——单例模式
- 【日拱一卒】链表——两个有序的链表合并
- Head First设计模式——命令模式
- Flask表单之WTForms和flask-wtf
- Head First设计模式——适配器和外观模式
- 「2020最新」Spring最易学习教程 4—整合Mybatis 事务控制
- Head First设计模式——模板方法模式
- Head First设计模式——迭代器模式
- PR工具自带的电源网络分析功能靠谱吗?