使用 PHP 将 Heavy Excel 导入 MySQL


import heavy excel to mysql using php

我有 excel 工作表,其中包含类似这样的数据,例如,如果工作表有"城市"和"人名"列。因此,每次我们需要检查城市是否存在于城市表(mysql)中时,如果不存在,则将在城市表中为城市插入新行,同样会检查人名,如果人表(mysql)中不存在,那么我们需要将人名保存在人表中。因此,在上面解释的场景中,使用 php 将至少 2gb 的繁重 excel 文件导入 mysql 的最佳方法是什么,这样就可以在服务器上执行更少的查询,减少服务器上的负载,并在几分之一秒内完成执行。请向我推荐上述情况的优化解决方案。

对于需要随时"规范化"数据的高速引入,请分两步执行(将原始数据加载到临时表后):

首先,添加新值(在本例中为 host_name):

# This should not be in the main transaction, and it shoud be with autocommit = ON
# In fact, it could lead to strange errors if this were part of the main transaction and it ROLLBACKed.
INSERT IGNORE INTO HostNorm (host_name)
    SELECT DISTINCT s.host_name
        FROM Staging AS s
        LEFT JOIN HostNorm AS n  ON n.host_name = s.host_name
        WHERE n.host_id IS NULL;

然后获取放入事实数据表的host_id

# Also not in the main transaction, and it should be with autocommit = ON
# This multi-table UPDATE sets the ids in Staging:
UPDATE   HostNorm AS n
    JOIN Staging AS s  ON s.host_name = n host_name
    SET s.host_id = n.host_id

在我的博客中有更多讨论