简体   繁体   English

查询TEXT列可以导致创建临时表吗?

[英]Can querying a TEXT column lead to creation of a temp table?

I am not very knowledgable on mysql. 我不是很了解mysql。 So forgive if its a stupid question. 因此,如果这是一个愚蠢的问题,请原谅。

I got a call from my DBA today that there is a spike in the number of temp tables my application creates. 我今天从数据库管理员那里接到一个电话,说我的应用程序创建的临时表数量激增。

Except that I don't create any temp table explicitly. 除了我没有明确创建任何临时表。

On troubleshooting we found that everytime a TEXT colomn is used in a sub query, a temp table was created. 在进行故障排除时,我们发现每次在子查询中使用TEXT列时,都会创建一个临时表。

As a developer I find this confusing that using a subquery and TEXT column created a temp table. 作为开发人员,我发现使用子查询和TEXT列创建了一个临时表感到困惑。

My question is, as a developer, how can I understand when a QUERY will create a temp table (in the background). 我的问题是,作为开发人员,我如何才能理解QUERY何时会创建临时表(在后台)。

I did some googling and found that I can do something like 我做了一些谷歌搜索,发现我可以做类似的事情

show GLOBAL status like 'created_tmp_disk_tables'

but what does it tell me? 但是它告诉我什么? how can I know whether MY query caused the temp table. 我怎么知道我的查询是否导致了临时表。

To determine whether a statement requires a temporary table, use EXPLAIN and check the Extra column to see whether it says Using temporary. 要确定语句是否需要临时表,请使用EXPLAIN并检查Extra列以查看其是否显示“使用临时表”。

From the documentation : 文档中

The server creates temporary tables under conditions such as these: 服务器在以下条件下创建临时表:

  • Evaluation of UNION statements, with some exceptions described later. 评估UNION语句,但稍后会有一些例外。

  • Evaluation of some views, such those that use the TEMPTABLE algorithm, UNION, or aggregation. 评估某些视图,例如使用TEMPTABLE算法,UNION或聚合的视图。

  • Evaluation of derived tables (subqueries in the FROM clause). 派生表的评估(FROM子句中的子查询)。

  • Tables created for subquery or semi-join materialization (see Section 9.2.1.18, “Subquery Optimization”). 为子查询或半联接实现创建的表(请参见第9.2.1.18节“子查询优化”)。

  • Evaluation of statements that contain an ORDER BY clause and a different GROUP BY clause, or for which the ORDER BY or GROUP BY contains columns from tables other than the first table in the join queue. 评估包含ORDER BY子句和另一个GROUP BY子句的语句,或者对于ORDER BY或GROUP BY包含联接队列中第一个表以外的表中的列的语句,进行评估。

  • Evaluation of DISTINCT combined with ORDER BY may require a temporary table. 结合ORDER BY对DISTINCT进行评估可能需要一个临时表。

  • For queries that use the SQL_SMALL_RESULT option, MySQL uses an in-memory temporary table, unless the query also contains elements (described later) that require on-disk storage. 对于使用SQL_SMALL_RESULT选项的查询,MySQL使用内存中临时表,除非查询还包含需要磁盘存储的元素(稍后描述)。

  • Evaluation of multiple-table UPDATE statements. 评估多表UPDATE语句。

  • Evaluation of GROUP_CONCAT() or COUNT(DISTINCT) expressions. 计算GROUP_CONCAT()或COUNT(DISTINCT)表达式。

Also,Some query conditions prevent the use of an in-memory temporary table, in which case the server uses an on-disk table instead: 同样,某些查询条件会阻止使用内存中的临时表,在这种情况下,服务器将使用磁盘上的表来代替:

  • Presence of a BLOB or TEXT column in the table 表中是否存在BLOB或TEXT列

  • Presence of any string column in a GROUP BY or DISTINCT clause larger than 512 bytes for binary strings or 512 characters for nonbinary strings. GROUP BY或DISTINCT子句中存在任何字符串列,对于二进制字符串,大于512个字节,对于非二进制字符串,大于512个字符。 (This applies only before MySQL 5.7.5. In addition, before MySQL 5.7.3, the limit is 512 bytes regardless of string type.) (这仅在MySQL 5.7.5之前适用。此外,在MySQL 5.7.3之前,无论字符串类型如何,该限制均为512字节。)

  • Presence of any string column with a maximum length larger than 512 (bytes for binary strings, characters for nonbinary strings) in the SELECT list, if UNION or UNION ALL is used 如果使用UNION或UNION ALL,则SELECT列表中存在任何最大长度大于512(字符串为二进制字符串,非二进制为字符)的字符串列

  • The SHOW COLUMNS and DESCRIBE statements use BLOB as the type for some columns, thus the temporary table used for the results is an on-disk table. SHOW COLUMNS和DESCRIBE语句使用BLOB作为某些列的类型,因此用于结果的临时表是磁盘上的表。

MySQL uses temporary tables in quite a few different types of queries, without you asking for them. MySQL在很多不同类型的查询中使用临时表,而无需您查询它们。 A few cases that often (but not always) create temporary tables: 经常(但并非总是)创建临时表的几种情况:

  • Subqueries 子查询
  • GROUP BY
  • UNION
  • Querying through a VIEW 通过VIEW查询
  • Common table expressions ( WITH ... syntax) 常用表表达式( WITH ...语法)

A more complete guide to when MySQL uses "internal" temporary tables is found in the documentation: https://dev.mysql.com/doc/refman/5.7/en/internal-temporary-tables.html 在文档中可以找到关于MySQL使用“内部”临时表的更完整指南: https : //dev.mysql.com/doc/refman/5.7/en/internal-temporary-tables.html

Basically, a temp table is used when the execution of query needs to save rows from a partial result, and then move on to refine that partial result into a final result. 基本上,当执行查询需要保存部分结果中的行,然后继续将部分结果细化为最终结果时,将使用临时表。

Temp tables are fairly expensive for the MYSQL Server to use, so we like to avoid them when possible. 临时表对于使用MYSQL Server而言相当昂贵,因此我们希望尽可能避免使用它们。 Especially costly is when the temp table is written to the filesystem instead of remaining in memory. 当临时表写入文件系统而不是保留在内存中时,尤其昂贵。 This happens when the temp table involves TEXT or BLOB columns, or if it needs to store more rows than can fit in an amount of memory limited by the tmp_table_size configuration option. 当临时表涉及TEXTBLOB列,或者它需要存储的行超出tmp_table_size配置选项所限制的内存量时,就会发生这种情况。

You can (mostly) tell when your query will use a temp table by analyzing the query with EXPLAIN . 您可以(大部分)通过使用EXPLAIN分析查询来确定查询何时将使用临时表。 See https://dev.mysql.com/doc/refman/5.7/en/explain.html 参见https://dev.mysql.com/doc/refman/5.7/en/explain.html

Sometimes internal temp tables are unavoidable, because there's no other way to get the query result you need. 有时内部临时表是不可避免的,因为没有其他方法可以获取所需的查询结果。 But sometimes you can rewrite your SQL query or use indexes to help the query get the same result without using a temp table. 但是有时您可以重写SQL查询或使用索引来帮助查询获得相同的结果,而无需使用临时表。 It depends on the specific query, and your table definitions. 它取决于特定的查询和您的表定义。 Query optimization is a process that must be done on a case-by-case basis. 查询优化是必须根据具体情况进行的过程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM