因用户需要,给网站增加了一项批量导入excel功能,单个excel文件记录大概2万到3万行,20列左右数据,目前测试发现每次导入数据的时候,都会造成网站其他功能短暂卡住无法使用的情况,直至excel全部处理完才可访问。目前每一行记录均通过网站API统一入库(进行一系列的权限、查询、增加、修改、去重、记录日志等操作)。
不知道有什么方法可以改进,使其导入同时不影响其他用户使用,目前想到的办法是将excel解析结果临时保存到xcache,然后分批(30000条/50条=600次)导入。不知是否有更好的办法。
回复内容:
因用户需要,给网站增加了一项批量导入excel功能,单个excel文件记录大概2万到3万行,20列左右数据,目前测试发现每次导入数据的时候,都会造成网站其他功能短暂卡住无法使用的情况,直至excel全部处理完才可访问。目前每一行记录均通过网站API统一入库(进行一系列的权限、查询、增加、修改、去重、记录日志等操作)。
不知道有什么方法可以改进,使其导入同时不影响其他用户使用,目前想到的办法是将excel解析结果临时保存到xcache,然后分批(30000条/50条=600次)导入。不知是否有更好的办法。
这个问题我也遇到过,不知道楼主遇到的情况是不是和我一样
两个写法都是用的同一个版本的PHPEXCEL,但是不知道为啥第一种会慢很多~
以下是老代码
:
<code>//代码是基于Yii1 set_time_limit(0);//这个一定要 $Excel = CUploadedFile::getInstanceByName("Excel"); $filePath = $Excel->getTempName() . '.' . $Excel->getExtensionName(); $Excel->saveAs($filePath); Yii::import("application.extensions.*"); $excel = new ExcelReader($filePath); foreach ($excel->getWorksheetList() as $worksheet => $value) { $rs = $excel->getWorksheetData($worksheet); if (!empty($rs)) { foreach ($rs as $index => $row) { $name = $row[1]; $phone = $row[2]; //more code... } } </code>
以下是新代码
:
<code>//代码是基于Yii1 set_time_limit(0);//这个一定要 $Excel = CUploadedFile::getInstanceByName("Excel"); $filePath = $Excel->getTempName() . '.' . $Excel->getExtensionName(); $Excel->saveAs($filePath); Yii::import("application.extensions.*"); $PHPExcel = new PHPExcel(); $PHPReader = new PHPExcel_Reader_Excel2007();//如果new的是2007,那么excel必须是2007格式的 $excel = $PHPReader->load($filePath);//引入文件 $excelSheets = $excel->getAllSheets(); $activeSheetNames = $excel->getSheetNames(); foreach ($excelSheets as $SheetIndex => $activeSheet) { $sheetColumnTotal = $activeSheet->getHighestRow(); if($sheetColumnTotal == 0){ continue; } for($i = 2;$i getCell('A'.$i)->getValue(); $phone = $activeSheet->getCell('B'.$i)->getValue(); } } </code>
异步任务队列
上传后生成任务记录,放到队列处理
如果需要处理的数据太多,建议写入一个队列,然后使用后台php—cli队列任务处理进程来异步处理吧。这个有点像我之前做的一个视频上传后加入队列,然后使用后台PHP—cli进程来异步处理视频转码过程的例子。

PHPisusedforsendingemailsduetoitsintegrationwithservermailservicesandexternalSMTPproviders,automatingnotificationsandmarketingcampaigns.1)SetupyourPHPenvironmentwithawebserverandPHP,ensuringthemailfunctionisenabled.2)UseabasicscriptwithPHP'smailfunct

The best way to send emails is to use the PHPMailer library. 1) Using the mail() function is simple but unreliable, which may cause emails to enter spam or cannot be delivered. 2) PHPMailer provides better control and reliability, and supports HTML mail, attachments and SMTP authentication. 3) Make sure SMTP settings are configured correctly and encryption (such as STARTTLS or SSL/TLS) is used to enhance security. 4) For large amounts of emails, consider using a mail queue system to optimize performance.

CustomheadersandadvancedfeaturesinPHPemailenhancefunctionalityandreliability.1)Customheadersaddmetadatafortrackingandcategorization.2)HTMLemailsallowformattingandinteractivity.3)AttachmentscanbesentusinglibrarieslikePHPMailer.4)SMTPauthenticationimpr

Sending mail using PHP and SMTP can be achieved through the PHPMailer library. 1) Install and configure PHPMailer, 2) Set SMTP server details, 3) Define the email content, 4) Send emails and handle errors. Use this method to ensure the reliability and security of emails.

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

The reason for using Dependency Injection (DI) is that it promotes loose coupling, testability, and maintainability of the code. 1) Use constructor to inject dependencies, 2) Avoid using service locators, 3) Use dependency injection containers to manage dependencies, 4) Improve testability through injecting dependencies, 5) Avoid over-injection dependencies, 6) Consider the impact of DI on performance.

PHPperformancetuningiscrucialbecauseitenhancesspeedandefficiency,whicharevitalforwebapplications.1)CachingwithAPCureducesdatabaseloadandimprovesresponsetimes.2)Optimizingdatabasequeriesbyselectingnecessarycolumnsandusingindexingspeedsupdataretrieval.

ThebestpracticesforsendingemailssecurelyinPHPinclude:1)UsingsecureconfigurationswithSMTPandSTARTTLSencryption,2)Validatingandsanitizinginputstopreventinjectionattacks,3)EncryptingsensitivedatawithinemailsusingOpenSSL,4)Properlyhandlingemailheaderstoa


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Mac version
God-level code editing software (SublimeText3)

SublimeText3 English version
Recommended: Win version, supports code prompts!

SublimeText3 Linux new version
SublimeText3 Linux latest version
