How the PHP Read file speed optimization

Source: Internet
Author: User
Tags php read file
Hello, seniors.
Little brother to read a large log file to have 1.6 million lines
How to optimize the speed of reading files?
Because the younger brother wants to read and write into the database.
The younger brother now uses the Fopen,fget to read like this.
Please enlighten your predecessors.
Thank you.

Reply to discussion (solution)

Fgets read a line
If it's too slow, then:
Fread read a piece (2048 or larger)
Cut by line after reading
Processing when the last one is left unhandled, and then processed after the next read content is connected

The quickest way is to use the LoadFile in directive to load directly into the staging table before processing

Can seniors give the code a demonstration?
Thank you so much.

$FN = ' filename '; $fp = fopen ($FN, ' R '); $last = "; while (! feof ($fp)) {  $a = preg_split ("/[\r\n]+/", $last. Frenad ($FP, 204 8));  for ($i =0; $i <count ($a)-1; $i + +) {     processing of data  }  $last = $a [$i];} if ($last) processing of data

Reading a piece with fread can increase speed.

Thanks for the help of seniors.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.