Home  >  Q&A  >  body text

php - laravel中导入excel 数据量很大的情况下 处理重复数据时如果做最好

用laravel excel导入用户的excel
能够预计到的一个excel大概会有几千到几万条不等。
需要判断不导入重复的用户,比如导入10000人 数据库里面已经有100人了 那么这100人需要被判断出来 做另外的逻辑处理。另外9900人导入。并且记录着9900的插入状态 比如成功8000条 失败1900条。想在想到几个方案 不知道哪个比较合理
1、传统的批量的插入方法,无法精确到每条数据是否重复或者记录返回状态。但是效率应该是比较高的。
2、用for循环 插入前 先查询然后是否存在 然后插入完再记录状态 从业务逻辑上是最能符合需求的 但是效率很低,估计很容易就要500错误或者timeout。
3、在2的基础上改进 把整个过程写进队列 chunk分组后 进队列 然后后台处理。缺点就是用户相对来说体验比较差,不能立等可取 还需要配合通知系统来做这个。
4、查了下 还有种是用事务 分块提交,不知道这种如何 还没有测试。也请各位大大指教。

不知道各位大大还有什么更好的办法。能够在效率和逻辑中都比较平衡的。感谢了

世界只因有你世界只因有你2683 days ago1106

reply all(2)I'll reply

  • 过去多啦不再A梦

    过去多啦不再A梦2017-05-16 13:00:18

    Looking at the meaning of the question, I understand it is
    1. Excel upload
    2. Excel analysis, storage
    3. Return data to the user

    The whole process should be quite time-consuming. You can give the user the status of [Processing], process it in the background, and output the result after it is completely processed.

    As for judging whether there are duplicate users and recording the status of success and failure, you can consider saving it to redis first, which will be faster, and finally all the results will be stored in the database for processing

    reply
    0
  • 大家讲道理

    大家讲道理2017-05-16 13:00:18

    Tell me what I think, don’t comment if you don’t like it.

    Assume that the unique identifier in the database and excel is the user ID number:

    • Check out the existing ID number in the database,

    • After Excel is uploaded, it traverses each row and determines whether the ID number in the current piece of data is found in the array in the database

    • If it is not there, write it into the database and add the ID number to the found array,

    • If yes, skip

    In my online environment, I can upload an Excel file with 50,000+ rows at a time, and the waiting time is only about 6 seconds.
    Of course, the number of single rows and columns in each online environment and Excel file is different,
    This is for the poster’s reference only.

    reply
    0
  • Cancelreply