search
HomeDatabaseMysql TutorialMySQL内部临时表(Internal Temporary Table)

当某些SQL命令在MySQL数据库中被执行的时候,它可能需要先创建一些内部的临时表来完成比较复杂的排序或分组查询。MySQL的临时表分

当某些SQL命令在MySQL数据库中被执行的时候,它可能需要先创建一些内部的临时表来完成比较复杂的排序或分组查询。MySQL的临时表分为 in-memory 和 on-disk 两种。 如有可能,MySQL 总是首先使用 in-memory 的临时表, 而当临时表变得太大的时候,也可能被转存为on-disk 的临时表。

如下几个条件下可能导致SQL命令需要创建临时表:

  * 使用了不同的 ORDER BY 和 GROUP BY 条件,或它们包含了JOIN查询中非首先表的字段;
  * 同时使用了DISTINCT和ORDER BY;
  * 如果SQL命令使用了SQL_SMALL_RESULT选项,,那么需要时将创建in-memory临时表,除非
    查询中还包含有需用 on-disk 存储的元素;

一些条件下可能阻碍使用in-memory临时表, 导致MySQL改用on-disk临时表:

  * 数据表中含有BLOB类型或TEXT类型字段列;
  * 在GROUP BY或DISTINCT任何条件中含有超过512字节的列;
  * 如果使用了UNION或UNION ALL,而且SELECT列中含有任何超过512字节的列;
  * 如果in-memory临时表变得太大,超过tmp_table_size或max_heap_table_size数值时;

MySQL数据库用Created_tmp_tables和Created_tmp_disk_tables变量记录所用临时表的数目;

关于MySQL内部临时表(internal temporary table)的优化:

(1) 首先,应该尽可能考虑如何避免您的SQL命令创建临时表;

对于一个查询连接非常繁忙的数据库,频繁地使用需要创建临时表的查询本身就已经是一个性能瓶颈。作为一个 DBA,您需要重新检视您的数据表的结构以及各表之间的关联, 重新考虑主键和索引,重组数据结构以减少应用中需不同的ORDER BY和GROUP BY的情况。
作为一个程序员,您则可能需要重新分析您的应用需求, 设法简化SQL命令的复杂程度,例如,尽可能将单次的多层关联查询,拆分为较少关联层次的多次查询,或使用View表。

(2) 其次,应该尽量设法确保临时表被创建于内存而非磁盘之中;

如果实在是无法避免创建临时表,那么退而求其次,则需要尽量确保这些临时表能够被创建在内存之中。避免在结构设计和查询命令中使用BLOB和TEXT类型字段,或可考虑用 SUBSTRRING(colum,length)函数将其转换为字符串类型;用SQL_SMALL_RESULT选项通知数据库使用in-memory临时表;使用View来简化查询;使用RAM disk内存盘来存储MySQL 数据库的临时表(需确保无使用BLOB和TEXT字段)。

如何避免 On-Disk Temporary Tables

因为 Memory 存储引擎不支持 BLOB和TEXT 类型,所以包含有 BLOB和TEXT 类型字段的查询,当它需要用到隐式临时表(implicit temporary table)的时候,就不得不使用 on-disk的MyISAM临时表,即使它的查询结果可能只有很简单的几行数据。

这会导致一个严重的性能瓶颈,即使您能配置将MySQL的临时表存储到RAM disk上,依然还是会需要用到许多昂贵的操作系统的调用函数。 在实用中还发现,某些SQL语句的临时表甚至根本连RAM disk都不能使用(此时SQL查询命令会因为不能创建临时表而失败)。

The best solution is to avoid using the BLOB and TEXT types unless you really need them.
If you can't avoid them, you may be able to use the ORDER BY SUBSTRRING(colum,length)
trick to convert the values to character strings. wihich will permit in-memory temporary
tables.

Just be sure that you are using a short engough substring that the temporary table
doesn't grow larger than max_heap_table_size or tmp_table_size, or MySQL will convert
the table to an on-disk MyISAM table.

If the Extra column of EXPLAIN contains "Using temporary",
the query uses an implicit temporary table.

linux

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How do you alter a table in MySQL using the ALTER TABLE statement?How do you alter a table in MySQL using the ALTER TABLE statement?Mar 19, 2025 pm 03:51 PM

The article discusses using MySQL's ALTER TABLE statement to modify tables, including adding/dropping columns, renaming tables/columns, and changing column data types.

How do I configure SSL/TLS encryption for MySQL connections?How do I configure SSL/TLS encryption for MySQL connections?Mar 18, 2025 pm 12:01 PM

Article discusses configuring SSL/TLS encryption for MySQL, including certificate generation and verification. Main issue is using self-signed certificates' security implications.[Character count: 159]

How do you handle large datasets in MySQL?How do you handle large datasets in MySQL?Mar 21, 2025 pm 12:15 PM

Article discusses strategies for handling large datasets in MySQL, including partitioning, sharding, indexing, and query optimization.

What are some popular MySQL GUI tools (e.g., MySQL Workbench, phpMyAdmin)?What are some popular MySQL GUI tools (e.g., MySQL Workbench, phpMyAdmin)?Mar 21, 2025 pm 06:28 PM

Article discusses popular MySQL GUI tools like MySQL Workbench and phpMyAdmin, comparing their features and suitability for beginners and advanced users.[159 characters]

How do you drop a table in MySQL using the DROP TABLE statement?How do you drop a table in MySQL using the DROP TABLE statement?Mar 19, 2025 pm 03:52 PM

The article discusses dropping tables in MySQL using the DROP TABLE statement, emphasizing precautions and risks. It highlights that the action is irreversible without backups, detailing recovery methods and potential production environment hazards.

How do you represent relationships using foreign keys?How do you represent relationships using foreign keys?Mar 19, 2025 pm 03:48 PM

Article discusses using foreign keys to represent relationships in databases, focusing on best practices, data integrity, and common pitfalls to avoid.

How do you create indexes on JSON columns?How do you create indexes on JSON columns?Mar 21, 2025 pm 12:13 PM

The article discusses creating indexes on JSON columns in various databases like PostgreSQL, MySQL, and MongoDB to enhance query performance. It explains the syntax and benefits of indexing specific JSON paths, and lists supported database systems.

How do I secure MySQL against common vulnerabilities (SQL injection, brute-force attacks)?How do I secure MySQL against common vulnerabilities (SQL injection, brute-force attacks)?Mar 18, 2025 pm 12:00 PM

Article discusses securing MySQL against SQL injection and brute-force attacks using prepared statements, input validation, and strong password policies.(159 characters)

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.