简体   繁体   English

MYSQL 临时表 - 如何查看活动表

[英]MYSQL Temporary Tables - How to view active ones

We have a simple "crm-like" software in our company.我们公司有一个简单的“类CRM”软件。 There are some complex queries that were taking some time, daily-use queries... so i am testing some modifications to use Temporary Tables to replace all the complex joins and subqueries we need.有一些复杂的查询需要一些时间,日常使用的查询......所以我正在测试一些修改以使用临时表来替换我们需要的所有复杂连接和子查询。

So far going really well, got a 90%+ speed.到目前为止进展非常顺利,速度达到了 90% 以上。

Since its a web app (codeigniter + mysql), i plan to put it in a "production-test" enviroment so 50% of the users can help me test it.由于它是一个 web 应用程序(codeigniter + mysql),我计划将它放在“生产测试”环境中,以便 50% 的用户可以帮助我测试它。 I would like to monitor the tables that are active, and if possible for each connection.我想监视活动的表,如果可能的话,监视每个连接。

My question is - Is there any way i can view all the TEMPORARY TABLES that are active in the mysql instance?我的问题是 - 有什么方法可以查看在 mysql 实例中处于活动状态的所有临时表? Maybe view its data?也许查看它的数据?

I read something about a PLUGIN or something like, but the article was far to messy and i coudnt understand.我读过一些关于插件或类似的东西,但这篇文章太混乱了,我无法理解。

Thanks alot and sorry for my english - not my native lang.非常感谢,对不起我的英语——不是我的母语。

temp table = temporary, it deleted right after the query temp table = 临时表,在查询后立即删除

there is no direct solution, except logging all the queries and execute one-by-one to exam which query require tmp_table没有直接的解决方案,除了记录所有查询并逐一执行以检查哪个查询需要 tmp_table

the system variables like Created_tmp_tables might give some ideasCreated_tmp_tables这样的系统变量可能会给出一些想法

mysql> show status like '%tmp%';
+-------------------------+-------+
| Variable_name           | Value |
+-------------------------+-------+
| Created_tmp_disk_tables | 0     |
| Created_tmp_files       | 0     |
| Created_tmp_tables      | 0     |
+-------------------------+-------+

One of the problems of temporary tables is actually tracking the usage, then there's the overhead of creating, indexing and deleting them - particularly with a web application where the lifetime of the temporary table is usually as long as the lifetime of the HTTP request.临时表的问题之一实际上是跟踪使用情况,然后是创建、索引和删除它们的开销——特别是对于 web 应用程序,其中临时表的生命周期通常与 HTTP 请求的生命周期一样长。

Where pre-compiled results (ie materialized views) will be of benefit, I set up a conventional table, adding field(s) which reference the MySQL connection id / the web session id / the source query + time generated depending on the TTL for the data and whether it will be shared or not.如果预编译的结果(即物化视图)会有所帮助,我设置了一个常规表,添加了引用 MySQL 连接 ID / web Z21D6F40CFB511982E4424E0E250A9 生成的源查询 + ID 时间的字段。数据以及是否会共享。

In addition to getting detailled tracking of the usage of the table, it makes it much easier to address query tuning and of course, the schema is better documented.除了详细跟踪表的使用情况外,它还可以更轻松地解决查询调优问题,当然,架构也有更好的文档记录。

MySQL INNODB from v5.7 provides some information about temporary tables from INFORMATION_SCHEMA.INNODB_TEMP_TABLE_INF but it shows only service information without details about table structure or name 从 v5.7 开始的 MySQL INNODB 提供了一些关于来自INFORMATION_SCHEMA.INNODB_TEMP_TABLE_INF的临时表的信息,但它只显示了服务信息,没有关于表结构或名称的详细信息

mysql> SELECT * FROM INFORMATION_SCHEMA.INNODB_TEMP_TABLE_INFO;
+----------+--------------+--------+------------+
| TABLE_ID | NAME         | N_COLS | SPACE      |
+----------+--------------+--------+------------+
|     1066 | #sql569_1c_4 |      4 | 4243767290 |
+----------+--------------+--------+------------+
1 row in set (0.00 sec)

mysql> 

When your data-set grows larger, temporary tables won't help you.当您的数据集变得更大时,临时表将无济于事。 In the end, how do they help?最后,他们如何提供帮助? The data has to be copied into those tables after you calculate the result, why not cache the results with memcached?计算结果后必须将数据复制到这些表中,为什么不使用 memcached 缓存结果呢?

Also, I've seen databases that are somewhat large (tens of gigabytes) and are running off of a single machine and queries were running quickly.此外,我还看到了有些大的数据库(数十 GB)并且在单台机器上运行,并且查询运行得很快。 There's a question whether you configured your database server properly (software and hardware).有一个问题是您是否正确配置了数据库服务器(软件和硬件)。 You might be experiencing temporary speedup, but there's no guarantee it's going to work permanently.您可能会遇到暂时的加速,但不能保证它会永久有效。 I'd optimize the application, queries, software and hardware required to run the app and then I'd cache the results with memcache unless you need the latest hotcopy of the query result.我会优化运行应用程序所需的应用程序、查询、软件和硬件,然后我会使用 memcache 缓存结果,除非您需要查询结果的最新热拷贝。

I believe that it doesn't make sense in view temp tables because of they live during connection and die after closing connections.我认为在视图临时表中没有意义,因为它们在连接期间存在并在关闭连接后死亡。 But there is one important fact about temporary table.但是关于临时表有一个重要的事实。 So if you use persistent connection in your scripts temporary table will live during life of this connection and clients which use this one will get access to the same temporary tables.因此,如果您在脚本中使用持久连接,则临时表将在此连接的生命周期内存在,并且使用此连接的客户端将可以访问相同的临时表。 Also in this case temp table will consume system resources.同样在这种情况下,临时表将消耗系统资源。 For avoiding it you should remove temp tables manually after run query necessary query which is used this temp table.为了避免这种情况,您应该在运行使用此临时表的必要查询后手动删除临时表。

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

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