简体   繁体   中英

MYSQL Temporary Tables - How to view active ones

We have a simple "crm-like" software in our company. 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.

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

there is no direct solution, except logging all the queries and execute one-by-one to exam which query require tmp_table

the system variables like Created_tmp_tables might give some ideas

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.

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.

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

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?

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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