mysql 如何打开和关闭表
MySQL 如何统计打开的表数量 执行命令 mysqladmin status 时,可以看到类如下结果: Uptime:426Runningthreads:1Questions:11082 Reloads:1Opentables:12 如果只有6个数据表时,Open tables 的确实12,这可能会有些疑惑。 MySQL是多线程的,因此可能会有多个
MySQL 如何统计打开的表数量
执行命令 mysqladmin status 时,可以看到类似如下结果:
|
如果只有6个数据表时,Open tables 的值确实12,这可能会有些疑惑。
MySQL是多线程的,因此可能会有多个客户端同时发起查询某个表的请求。为了最小化多个客户端线程在同一个表上的不同状态,针对每个并发的线程单独打开数据表。这会占用一些内存,但是通常会提高性能。如果是 MyISAM 表,针对每个打开数据表的客户端都要有一个额外的文件描述符打开数据文件的情况(不过索引文件的描述符则可以在所有的线程间共享)。ISAM 存储引擎则共享这些操作。
MySQL 如何打开和关闭数据表
系统变量 table_cache, max_connections 和max_tmp_tables 影响着服务器保持打开的文件最大数量。提高一个或多个这些变量,就可以提高操作系统在每次处理时能打开的文件描述符限制。很多操作系统都可以增加文件打开的数量限制,不过每个系统的方法都各不一样。查阅一下操作系统文档来判断是否可以提高限制以及怎么去做。
table_cache 和 max_connections 相关。例如,有200个并发的连接,那么则必须至少有 200 * N 大小的表缓存,N 是在一个表连接中最大的表数量。同时也需要为临时表和临时文件保留一些额外的文件描述符。
请确认操作系统可以通过设定 table_cache 来处理对应数量的打开文件。如果 table_cache 设得太高了,MySQL可能会用完全部的文件描述符而拒绝新连接,就无法执行查询,变得很不可靠。因此必须要考虑到 MyISAM 存储引擎要为每个独立打开的表使用两个文件描述符。可以在启动mysqld_safe 的时候增加 --open-files-limit 参数来提高MySQL打开文件描述符的数量。详情请看"A.2.17 File NotFound".
缓存中会保存 table_cache 个打开的表会。它的默认值是64;它可以在启动 mysqld 的时候通过修改 --table_cache 参数来改变。注意,MySQL可能会临时打开比这个数更多的表来执行查询。
在以下情况中,一个不用的表会被关闭并且从缓存中被删除:
- 如果缓存满了,并且一个线程试图打开一个不在缓存中的表。
- 如果缓存中已经包含了不止 table_cache 个表目,并且线程无需再使用该表。
- 当发生刷新表操作。当提交一个FLUSH TABLES 语句或者执行 mysqladmin flush-tables or mysqladmin refresh 命令时就会这样。
- 当表缓存满了,服务器遵循以下步骤来分配一个新的缓存表目:
- 当前没使用的表都释放,依照最近最少使用的顺序。
- 如果有一个新的表要被打开,但是缓存满了且没有表被释放,缓存就临时根据需要扩充一下。
- 当缓存处于临时扩充状态,且有表处于从使用变成不使用状态时,就关闭这个表并且从缓存中释放。
· 每个并行访问都打开一个表。这意味着当有两个线程同时访问一个表,或者一个线程在同一个查询中访问两次这个表(例如,在表连接中连接自己),那么这 个表就需要被打开两次。每次并发的打开都在表缓存中请求一个表目。每个表第一次打开时都需要两个文件描述符:一个给数据文件,一个给索引文件。其他新增的对该表的打开则只需要一个文件,给数据文件。索引文件的描述符在所有的线程间是共享的。
· 如果使用 HANDLERtbl_name OPEN 语句打开表,则有一个专用的表对象给该线程。这个表对象不和其他线程共享,并且除非调用HANDLER tbl_name CLOSE 语句或者线程结束,否则它不会关闭;这样的话,它就重新放回表索引中(如果索引还未满)。详情请看"14.1.3HANDLER Syntax"。
· 可以检查mysqld 的状态变量 Opened_tables 来判断表索引是否太小了:
|
· 如果这个值比较大,不过完全没必要执行一大堆的 FLUSH
· TABLES 语句。只需加大表索引缓存大小即可。
from 官方翻译文档

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Big data structure processing skills: Chunking: Break down the data set and process it in chunks to reduce memory consumption. Generator: Generate data items one by one without loading the entire data set, suitable for unlimited data sets. Streaming: Read files or query results line by line, suitable for large files or remote data. External storage: For very large data sets, store the data in a database or NoSQL.

MySQL query performance can be optimized by building indexes that reduce lookup time from linear complexity to logarithmic complexity. Use PreparedStatements to prevent SQL injection and improve query performance. Limit query results and reduce the amount of data processed by the server. Optimize join queries, including using appropriate join types, creating indexes, and considering using subqueries. Analyze queries to identify bottlenecks; use caching to reduce database load; optimize PHP code to minimize overhead.

Backing up and restoring a MySQL database in PHP can be achieved by following these steps: Back up the database: Use the mysqldump command to dump the database into a SQL file. Restore database: Use the mysql command to restore the database from SQL files.

How to insert data into MySQL table? Connect to the database: Use mysqli to establish a connection to the database. Prepare the SQL query: Write an INSERT statement to specify the columns and values to be inserted. Execute query: Use the query() method to execute the insertion query. If successful, a confirmation message will be output.

One of the major changes introduced in MySQL 8.4 (the latest LTS release as of 2024) is that the "MySQL Native Password" plugin is no longer enabled by default. Further, MySQL 9.0 removes this plugin completely. This change affects PHP and other app

To use MySQL stored procedures in PHP: Use PDO or the MySQLi extension to connect to a MySQL database. Prepare the statement to call the stored procedure. Execute the stored procedure. Process the result set (if the stored procedure returns results). Close the database connection.

Creating a MySQL table using PHP requires the following steps: Connect to the database. Create the database if it does not exist. Select a database. Create table. Execute the query. Close the connection.

Oracle database and MySQL are both databases based on the relational model, but Oracle is superior in terms of compatibility, scalability, data types and security; while MySQL focuses on speed and flexibility and is more suitable for small to medium-sized data sets. . ① Oracle provides a wide range of data types, ② provides advanced security features, ③ is suitable for enterprise-level applications; ① MySQL supports NoSQL data types, ② has fewer security measures, and ③ is suitable for small to medium-sized applications.
