复杂的mysql查询内存不足

时间:2016-05-17 21:14:47

标签: mysql

我正在尝试从多个表创建一个视图,而且我没有使用mysql。我认为查询有效,但表很大,临时表耗尽内存,所以我希望有人有另一种方法来实现这个目标

表inventory_dated(315782行)

+---------------+--------------+------+-----+---------+----------------+
| Field         | Type         | Null | Key | Default | Extra          |
+---------------+--------------+------+-----+---------+----------------+
| ID            | int(11)      | NO   | PRI | NULL    | auto_increment |
| Device_Name   | varchar(255) | NO   | MUL | NULL    |                |
| Chassis_Name  | varchar(255) | YES  |     | NULL    |                |
| FPC           | varchar(255) | NO   |     | NULL    |                |
| PIC           | varchar(255) | YES  |     | NULL    |                |
| XCVR          | varchar(255) | YES  |     | NULL    |                |
| Part_Name     | varchar(255) | YES  |     | NULL    |                |
| Version       | varchar(255) | YES  |     | NULL    |                |
| Part_Number   | varchar(255) | NO   |     | NULL    |                |
| Serial_Number | varchar(255) | NO   |     | NULL    |                |
| Model_Number  | varchar(255) | YES  |     | NULL    |                |
| Description   | varchar(255) | YES  |     | NULL    |                |
| Install_Date  | timestamp    | YES  | MUL | NULL    |                |
| maxdate       | timestamp    | YES  |     | NULL    |                |
+---------------+--------------+------+-----+---------+----------------+
+----+----------------------------+--------------+-----+-----+------+-----------+---------+-------------+---------------+--------------+-------------+---------------------+---------------------+
| ID | Device_Name                | Chassis_Name | FPC | PIC | XCVR | Part_Name | Version | Part_Number | Serial_Number | Model_Number | Description | Install_Date        | maxdate             |
+----+----------------------------+--------------+-----+-----+------+-----------+---------+-------------+---------------+--------------+-------------+---------------------+---------------------+
|  1 | abc.ga.atlanta             |              |     |     |      | Chassis   |         |             | 1234          |              | model       | 2012-03-29 21:00:00 | 2016-04-25 09:35:00 |
+----+----------------------------+--------------+-----+-----+------+-----------+---------+-------------+---------------+--------------+-------------+---------------------+---------------------+

table calendar_month(469行)

+------------+------+------+-----+---------+-------+
| Field      | Type | Null | Key | Default | Extra |
+------------+------+------+-----+---------+-------+
| date_month | date | NO   | PRI | NULL    |       |
+------------+------+------+-----+---------+-------+

+------------+
| date_month |
+------------+
| 2011-01-01 |
+------------+

table device_list(6715行)

 +-------------+--------------+------+-----+---------+----------------+
| Field       | Type         | Null | Key | Default | Extra          |
+-------------+--------------+------+-----+---------+----------------+
| ID          | int(25)      | NO   | PRI | NULL    | auto_increment |
| Device_Name | varchar(255) | NO   | PRI | NULL    |                |
| Platform    | varchar(50)  | YES  | MUL | NULL    |                |
| Network     | varchar(255) | YES  |     | NULL    |                |
| Division    | varchar(255) | YES  |     | NULL    |                |
| Market      | varchar(255) | YES  |     | NULL    |                |
+-------------+--------------+------+-----+---------+----------------+
+------+----------------------------+----------+---------+----------+---------+
| ID   | Device_Name                | Platform | Network | Division | Market  |
+------+----------------------------+----------+---------+----------+---------+
| 7086 | abc.ga.atlanta             | platform | net1    | div1     | market1 |
+------+----------------------------+----------+---------+----------+---------+

我的查询耗尽或内存,即使tmp中有超过18GB的

SELECT
    inventory_dated.Device_Name,
    inventory_dated.Chassis_Name,
    inventory_dated.FPC,
    inventory_dated.PIC,
    inventory_dated.XCVR,
    inventory_dated.Part_Name,
    inventory_dated.Version,
    inventory_dated.Part_Number,
    inventory_dated.Serial_Number,
    inventory_dated.Model_Number,
    inventory_dated.Description,
    calendar_month.date_month,
    device_list.Platform,
    device_list.Network,
    device_list.Division,
    device_list.Market
FROM inventory_dated 
JOIN calendar_month
INNER JOIN device_list ON inventory_dated.Device_Name = device_list.Device_Name
where (`calendar_month`.`date_month` between date_format(`inventory_dated`.`Install_Date`,'%Y-%m-01') and coalesce(`inventory_dated`.`maxdate`,cast(now() as datetime))) 
order by `calendar_month`.`date_month`

我想要完成的是域填充或密集数据,我可以将inventory_dated表与calendar_month表结合使用,以返回每个月之间的记录,包括install_date和max_date,以及inventory_dated中的每条记录以及抓取相应的来自同一设备的device_list的数据。该查询使用所有tmp内存并使用

消亡
[Err] 126 - Incorrect key file for table '/tmp/#sql_4125_0.MYI'; try to repair it

我基本上使用相同的查询在较小的表上正常工作。希望这是有道理的,有人可以帮助优化查询 感谢

0 个答案:

没有答案