Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to estimate temporary disk space required for MySQL query?

I have a set of quite complex SELECT queries which use a lot of disk space (I see this from df -h while running). Is there a way to estimate the temporary disk space required for a query before starting it?

like image 514
Maksym Polshcha Avatar asked Apr 22 '13 11:04

Maksym Polshcha


People also ask

How do I find temp tablespace in MySQL?

Alternatively, check the temporary tablespace data file size on your operating system. By default, the temporary tablespace data file is located in the directory defined by the innodb_temp_data_file_path configuration option.

Do temp tables make queries run faster?

I have often found that temp tables speed things up, sometimes dramatically. The simple explanation is that it makes it easier for the optimiser to avoid repeating work.


1 Answers

You can use the EXPLAIN keyword to describe how your joins will effect the number of rows that will be joined together. This will also assist you in properly using keys if they are not already present. Explain will tell you when it thinks it will need to use temp tables (disk space). Based on the size of the rows being joined, you can then roughly estimate your disk space need.

See the docs on explain here:

http://dev.mysql.com/doc/refman/5.0/en/explain.html

Basically though, just prepend "Explain" to your select query to get info output. I believe you can also do this programatically if needed and use the results in your actual code, say for instance you needed to calculate(estimate) a large query run time and display it to the user before proceeding.

like image 68
Zak Avatar answered Oct 14 '22 08:10

Zak