---
title: "临时表过大"
author: "jedy"
date: "2007-02-26T07:51:00Z"
tags: []
slug: "untitled"
draft: false
lastmod: "2007-02-26T07:51:00Z"
id: 304703
url: "https://jedy.iteye.com/blog/304703"
---

今天遇到一个这样的错误：

```

ERROR 126 (HY000): Incorrect key file for table '/tmp/#sql_dbd_0.MYI'; try to repair it
```

发现其原因是临时表太大了，而/tmp分区只有2G，放不下就报错了。  
sql是这样的：

```

select promotion.promotion_id,
       client.* ,
       thanks.thanks_id,
       thanks.thanks_type,
       draft.draft_id,
       rand() as s
     from promotion, client, thanks, draft
     where promotion.client_id = client.client_id
       and promotion.promotion_id = thanks.promotion_id
       and promotion.promotion_id = draft.promotion_id
     order by s
     limit 10
```

如果把中间结果自己建一个临时表

```

create temporary table tmp select ...
```

建出的表文件只有100M左右，那么为什么/tmp中会放不下呢？  
猜测MySQL自己建的临时表都是静态行（没有找到资料证实）。有这样的猜测是因为如果用

```

create temporary table tmp row_format=fixed select ...
```

建表就会出现同样的错误了。

```

ERROR 126 (HY000): Incorrect key file for table '/tmp/#sqldbd_6bd_0.MYI'; try to repair it
```

所以这个问题有两个解决方法：  
1\. 自己建临时表  
2\. 加大临时目录。可以加大/tmp分区，也可以在启动MySQL时设置TMPDIR环境变量指定另外的临时目录