> So, basically I think we are counting them in the 320qps.
Yes, I believe that SETs count.
> We could optimize the application to avoid calling this instructions when not necessary
I agree that that is probably not worth bothering with. SETs are very fast.
> How can we make sure that the system is generating a lot of tmp tables?
mysql> SHOW GLOBAL STATUS LIKE 'Created_tmp%';
+-------------------------+-------+
| Variable_name | Value |
+-------------------------+-------+
| Created_tmp_disk_tables | 931 |
| Created_tmp_files | 13 |
| Created_tmp_tables | 5322 |
+-------------------------+-------+
and then divide by Questions to get frequency of such.
> Is the memory allocated for tmp tables released after they get used?
It should be. However if there are multiple temp tables in a single SELECT, the memory/disk may not be released until the SELECT is finished.