当某些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.

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

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]

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

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

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.

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

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.

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


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

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

WebStorm Mac version
Useful JavaScript development tools

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

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
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.