简体   繁体   English

使用php将大量excel导入mysql

[英]import heavy excel to mysql using php

I am having excel sheet which contains data in something like this ex.我有一个 excel 表,其中包含类似这个 ex 的数据。 if sheet has "city" and "person name" columns.So every time we need to check whether the city exists in city table(mysql) or not if not then new row will be inserted for city in city table similarly will check for person name also if don't exists in person table(mysql) then we need to save person name in person table .如果工作表有“城市”和“人名”列。所以每次我们需要检查城市是否存在于城市表(mysql)中,如果不存在,那么将在城市表中为城市插入新行,同样将检查人如果person表(mysql)中不存在name,那么我们需要将person名称保存在person表中。 So what can be the best way to import heavy excel file of 2gb atleast into mysql using php in the above explained scenario so that less queries will be executed with less load on server and execution will be done in fraction of seconds.那么,在上面解释的场景中,使用 php 将至少 2gb 的大量 excel 文件导入 mysql 的最佳方法是什么,以便在服务器上执行更少的查询,并且执行将在几分之一秒内完成。 Kindly suggest me the optimize solution for above scenario.请为我推荐上述场景的优化解决方案。

For high speed ingestion that needs to "normalize" the data as it goes, do it in two steps (after loading the raw data into a temp table):对于需要“标准化”数据的高速摄取,分两步完成(在将原始数据加载到临时表之后):

First, add the new values (of host_name in this example):首先,添加新值(在本例中为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;

Then grab the host_id for putting into the Fact table:然后获取host_id以放入 Fact 表:

# 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

More discussion in my blog更多讨论在我的博客

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM