MySQL创建临时表时包含哪些数据?

发布于 2024-12-18 11:21:32 字数 612 浏览 2 评论 0原文

我一直在阅读有关每当 MySQL 生成临时表时 VARCHAR 字段都会转换为 CHAR 字段的信息。

如果我有下表...

  • 超过 100K 行
  • 十几个 VARCHAR 列设置为实际长度(例如,电话号码为 10)
  • 5 个 VARCHAR 列设置为 15K(长度可能短至 20,但最多可达 15K)案例)

并且,假设我有这个查询...

  • VARCHAR
  • 返回多个列,包括与六个其他表连接的 ( VARCHAR 列)
  • 按日期和数字 ID 对行进行排序(即,在 VARCHAR 列上)
  • 具有不包含 VARCHAR 字段的 WHERE 子句
  • LIMIT 为 12 rows

当MySQL创建临时表时,为了排序和偶尔分组(在某些情况下),临时表中最终会出现什么?

例如,表是否只包含选择要返回的行所需的列(即在 WHERE 和 ORDER BY 子句中指定的列),然后由数据库引擎确定 LIMIT 中的 12 行,然后读取数据那12行?或者,临时表中是否包含较大的数据块(例如,包括冗长的 VARCHAR 列的所有潜在行)?

I have been reading about VARCHAR fields being converted to CHAR fields whenever MySQL generates a temporary table.

If I have the following table...

  • Excess of 100K rows
  • A dozen VARCHAR columns set to real lengths (e.g., 10 for phone numbers)
  • 5 VARCHAR columns set to 15K (with lengths that could be as short as 20 but up to 15K worse case)

And, let's say I have this query...

  • Returns multiple columns including the VARCHARs
  • JOIN'd with a half-dozen other tables (not on VARCHAR columns)
  • Sorts the rows by date and numeric id (i.e., not on the VARCHAR columns)
  • Has a WHERE clause that does not include the VARCHAR fields
  • Has a LIMIT of 12 rows

When MySQL creates a temporary table, in order to sort and occasionally group (in some cases), what ends up in the temporary table?

For instance, does the table only include the columns necessary to choose which rows to return (i.e., those specified in WHERE and ORDER BY clauses), followed by the database engine determining the 12 rows in the LIMIT, and then reading the data for just those 12 rows? Or, is a larger chunk of data included in the temporary table (e.g., all potential rows including the lengthy VARCHAR columns)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

二智少女猫性小仙女 2024-12-25 11:21:33

当我试图找出表中是否有一个 TEXTBLOB 是否有必要在 MySQL 必须创建基于磁盘的临时表时,我遇到了类似的情况临时表,或者它还基于该列是否在查询中(SELECTWHERE 子句)。

这是查询(posts.body 的类型为 TEXT),

SELECT * FROM posts p LEFT JOIN user u ON p.user_id = u.id LEFT JOIN setting s ON u.id = s.user_id WHERE (p.title LIKE '%search%' AND (p.status = 'PUBLISH' AND p.date <= 1368441957)) GROUP BY u.id LIMIT 5

我看到运行此查询会增加 Created_tmp_disk_tables 变量。所以我将查询更改为

SELECT p.id FROM posts p LEFT JOIN user u ON p.user_id = u.id LEFT JOIN setting s ON u.id = s.user_id WHERE (p.title LIKE '%search%' AND (p.status = 'PUBLISH' AND p.date <= 1368441957)) GROUP BY u.id LIMIT 5

有趣的是计数器没有增加。然而,MySQL文档提到以下内容与我的推断不同

某些情况会阻止使用内存中的临时表,在这种情况下,服务器会使用磁盘上的表:

表中存在 BLOB 或 TEXT 列

...

I had a similar situation when I tried to find out if just having a TEXT or BLOB in a table is necessary to force a disk-based temporary table when MySQL has to make a temporary table or that it is also based on whether that column is in the query (SELECT or WHERE clause).

This was the query (posts.body is of the type TEXT)

SELECT * FROM posts p LEFT JOIN user u ON p.user_id = u.id LEFT JOIN setting s ON u.id = s.user_id WHERE (p.title LIKE '%search%' AND (p.status = 'PUBLISH' AND p.date <= 1368441957)) GROUP BY u.id LIMIT 5

I saw that running this increments the Created_tmp_disk_tables variable. So I changed the query to

SELECT p.id FROM posts p LEFT JOIN user u ON p.user_id = u.id LEFT JOIN setting s ON u.id = s.user_id WHERE (p.title LIKE '%search%' AND (p.status = 'PUBLISH' AND p.date <= 1368441957)) GROUP BY u.id LIMIT 5

And interestingly the counter didn't increase. However, the MySQL documentation mentions the following which is different from my inference

Some 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

...

蘸点软妹酱 2024-12-25 11:21:32

如果临时表符合内存限制,它是使用 HEAP (内存)创建的引擎)。所有限制均适用。如果它变得太大,服务器会将其转换为 MyISAM 磁盘上的临时表。

If temporary table fits memory restrictions, it is created using HEAP (Memory engine). All limitations apply. If it grows too large, server converts it into MyISAM temporary table on disk.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文