MySQL创建临时表时包含哪些数据?
我一直在阅读有关每当 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当我试图找出表中是否有一个
TEXT
或BLOB
是否有必要在 MySQL 必须创建基于磁盘的临时表时,我遇到了类似的情况临时表,或者它还基于该列是否在查询中(SELECT
或WHERE
子句)。这是查询(
posts.body
的类型为TEXT
),我看到运行此查询会增加
Created_tmp_disk_tables
变量。所以我将查询更改为有趣的是计数器没有增加。然而,MySQL文档提到以下内容与我的推断不同
I had a similar situation when I tried to find out if just having a
TEXT
orBLOB
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
orWHERE
clause).This was the query (
posts.body
is of the typeTEXT
)I saw that running this increments the
Created_tmp_disk_tables
variable. So I changed the query toAnd interestingly the counter didn't increase. However, the MySQL documentation mentions the following which is different from my inference
如果临时表符合内存限制,它是使用 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.