search
HomeBackend DevelopmentPHP Tutorialajax大数据导入的一系列相关问题处理_PHP教程

介绍:就是想实现简单的ajax上传数据,但是当数据量较大的时候,问题就一个一个接着来了,其实数据也不是很大,就是csv格式数据 不到5w条数据。大小5M,一开始认为这个很简单,就是先上传一下文件,然后读取一下,存到数据库就好了,结果,可能我比较菜,弄了半天做出这个功能。环境是linux.

遇到的问题就从先到后的一一说吧。

问题1 按照我最初的想法,先上传文件再读取文件。这里问题就来了,当文件较大的时候上传较慢,导致客户看到的操作一直处于等待状态,不人性化。

处理办法:我是这样做的,大神有更好的办法,求介绍。我先把文件上传上去,然后把文件存到一个特定的文件夹就叫 import吧   ,然后返回一个这个文件名字。这样就确保了文件是上传成功的。并且我可以在他返回名字的这一步用js  给客户一个提示。然后就是ajax去请求php读取文件,插入数据库。可是问题来了。

问题2 当我用ajax去请求php读取文件并插入数据库的时候,遇到一个问题,就是ajax请求总是在1min的时候,断掉。我一想 ,这应该是php的最大执行时间max_execution_time的原因吧,结果我修改为300秒。还是这样,那我就认为会不会是apache的 最大get时间max_input_time呢,我就在代码加一个 ini_set  结果,用ini_get   查看max_input_time,用ini_set设置无效,还是60秒,在网上查了很多资料,还是不知道为啥。有大神知道的,请给我回复下。菜鸟先谢过了。那没办法,我只能去服务器把php.ini配置修改了。经理说不让修改的,为了测试,偷偷改了--最后修改回来了。修改之后,测试,还是不行。还是到一分钟 就执行超时。真的很纳闷。不知道什么原因。求指教。那没办法。

这种办法行不通了,对一个5m的文件只能分行读取了。然后就是对代码的一通修改,分行读取是这样操作的,先ajax请求,然后每次读取2000条  然后对这2000条数据进行处理,插入数据库(文章最后介绍一个好用的分行读取函数)。然后每次ajax执行完,返回一个状态符,和本次读取到的行数,然后下次接着读。知道最后读取完。这中间还遇到一个问题:就是当我对每一行数据进行查重的时候遇到的,是这样的,我对得到的内容进行循环,然后查一下每行是否存在,当我判断$count是否大于0 的时候,当已存在的时候,我用continue,执行下一次循环。但是当我在导入10000条的时候,总是在8000条的时候报错说 服务器内部错误。很闷,不解问什么,结果只能用if  else代替了。纳闷。一个小提醒:插入数据库的时候 不要一条一条的插入,最好这样 inset  into  aaa(`xx`,`xxx`)values('111','111'),('222','222')。这样 速度会快很多。

行号读取函数,SplFileObject这个类库真的很好用推荐。有知道我的问题的,求大神指教。

function getFileLines($filename, $startLine, $endLine, $method = 'rb'){
      $content = array();
      $filename = DATA_PATH.DS.'import' . DS . $filename;
      $count = $endLine - $startLine;
      $fp = new SplFileObject($filename, $method);
      $fp->seek($startLine); // 转到第N行, seek方法参数从0开始计数
      for ($ii = 0; $ii             $content[] = $fp->current(); // current()获取当前行内容
            $fp->next(); // 下一行
      }
      return array_filter($content); // array_filter过滤:false,null,''
}

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/781156.htmlTechArticle介绍:就是想实现简单的ajax上传数据,但是当数据量较大的时候,问题就一个一个接着来了,其实数据也不是很大,就是csv格式数据 不到...
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
PHP Dependency Injection Container: A Quick StartPHP Dependency Injection Container: A Quick StartMay 13, 2025 am 12:11 AM

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Dependency Injection vs. Service Locator in PHPDependency Injection vs. Service Locator in PHPMay 13, 2025 am 12:10 AM

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHP performance optimization strategies.PHP performance optimization strategies.May 13, 2025 am 12:06 AM

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHP Email Validation: Ensuring Emails Are Sent CorrectlyPHP Email Validation: Ensuring Emails Are Sent CorrectlyMay 13, 2025 am 12:06 AM

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

How to make PHP applications fasterHow to make PHP applications fasterMay 12, 2025 am 12:12 AM

TomakePHPapplicationsfaster,followthesesteps:1)UseOpcodeCachinglikeOPcachetostoreprecompiledscriptbytecode.2)MinimizeDatabaseQueriesbyusingquerycachingandefficientindexing.3)LeveragePHP7 Featuresforbettercodeefficiency.4)ImplementCachingStrategiessuc

PHP Performance Optimization Checklist: Improve Speed NowPHP Performance Optimization Checklist: Improve Speed NowMay 12, 2025 am 12:07 AM

ToimprovePHPapplicationspeed,followthesesteps:1)EnableopcodecachingwithAPCutoreducescriptexecutiontime.2)ImplementdatabasequerycachingusingPDOtominimizedatabasehits.3)UseHTTP/2tomultiplexrequestsandreduceconnectionoverhead.4)Limitsessionusagebyclosin

PHP Dependency Injection: Improve Code TestabilityPHP Dependency Injection: Improve Code TestabilityMay 12, 2025 am 12:03 AM

Dependency injection (DI) significantly improves the testability of PHP code by explicitly transitive dependencies. 1) DI decoupling classes and specific implementations make testing and maintenance more flexible. 2) Among the three types, the constructor injects explicit expression dependencies to keep the state consistent. 3) Use DI containers to manage complex dependencies to improve code quality and development efficiency.

PHP Performance Optimization: Database Query OptimizationPHP Performance Optimization: Database Query OptimizationMay 12, 2025 am 12:02 AM

DatabasequeryoptimizationinPHPinvolvesseveralstrategiestoenhanceperformance.1)Selectonlynecessarycolumnstoreducedatatransfer.2)Useindexingtospeedupdataretrieval.3)Implementquerycachingtostoreresultsoffrequentqueries.4)Utilizepreparedstatementsforeffi

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Article

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

DVWA

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 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SecLists

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.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools