0

Why I get "Copying to tmp table on disk" after several iteration of loop? In the first few iterations I have not this. Table has 10 million and more rows. When I set lover offset, loop execute more iteration without "tmp table".

My select is

SELECT u.domena_id, u.umiestnenie, u.datum 
FROM (SELECT domena_id, min(datum) as min_datum 
FROM umiestnenie
WHERE datum BETWEEN 'date1' AND 'date2'
GROUP BY domena_id 
LIMIT 200000
OFFSET offset (increasing in lopp)
) x 
INNER JOIN umiestnenie u ON u.domena_id = x.domena_id and u.datum = x.min_datum

Table umiestnenie

CREATE TABLE IF NOT EXISTS `umiestnenie` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `domena_id` int(11) NOT NULL,
  `datum` date NOT NULL,
  `umiestnenie` int(11) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `fk_umiestnenie_domeny_idx` (`domena_id`),
  KEY `datum_idx` (`datum`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=15243077 ;

Table domena

CREATE TABLE IF NOT EXISTS `domena` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `nazov` varchar(200) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `nazov` (`nazov`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=15343156 ;

Why when I run this select one time, it doesn't need temporary table and when I run this select more times it does?

This is Explain of my select

1   PRIMARY <derived2>  ALL NULL    NULL    NULL    NULL    1000000 
1   PRIMARY u   ref domena_id_idx   domena_id_idx   4   x.domena_id 7   Using where
2   DERIVED umiestnenie index   NULL    domena_id_idx   4   NULL    20109031    Using where

Here is not tmp table, but when I run this select with increased offset from php I see using of temp table.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Michal6677
  • 11
  • 2
  • Probably because your RAM is not enough to keep all the data processed... – Marco Aug 26 '13 at 11:51
  • but why first interations does not need tmp table? When I set offset for example 1000 (it need small RAM), it will use tmp table after several iterations. What is in RAM? – Michal6677 Aug 26 '13 at 11:57
  • RAM is the memory installed on your pc; it's usually expressed in GB (Gigabytes) – Marco Aug 26 '13 at 11:58
  • I know what is RAM, but I dont know what is saved IN my RAM. My query can't take all RAM. And Why my first queries in loop does not need temp table and queries in cca 20th iterations need temp table. – Michal6677 Aug 26 '13 at 12:02
  • Try to read [this SO answer](http://stackoverflow.com/questions/7532307/skip-copying-to-tmp-table-on-disk-mysql) and [this link](http://www.linuxquestions.org/questions/linux-server-73/mysql-optimization-copying-to-tmp-table-on-disk-844527/) where it's said *"Instead of only increasing tmp_table_size, I need to increase MAX_HEAP_TABLE_SIZE too. And currently everything is working as I expected."* – Marco Aug 26 '13 at 12:06
  • I've read this articles yet. I have not rights for MAX_HEAP_TABLE_SIZE or tmp_table_size, but this variable are setted good for me. My query doesn't need temp table when I execute it 1-10 times. When I execute this query more times, it need temp table. Why? Does it save any cache or what? – Michal6677 Aug 26 '13 at 12:14

0 Answers0