The efficiency of pdo + mysql loop insert in php is extremely low, and the solution is

Source: Internet
Author: User
The efficiency of pdo + mysql loop insert in php is extremely low. in order to achieve office automation, I made a small item to import the student table to the database. Although the operation is normal, the efficiency is extremely low, baidu Times Network has not found a solution. I 'd like to ask for advice.
The functions to be implemented are:
1. import and display simultaneously. this is already achieved, but the effect is not satisfactory. when I want to display the processing result, the scroll bar will always be at the bottom to display the latest processed data, in the following code, I used the anchor location and found it very stupid ~~~ Is there a better way for this requirement?

2. during the import process, the first 1500 entries are very fast, and the display effect is very good. the scroll bar is scrolled to the bottom (very professional ), however, after about 1500 entries, the system becomes stuck. when we open the select count (*) from stutable database, we find that the record keeps increasing, but the page looks stuck, and the number of rows in the database is still increasing ~~~. In the past two days, Baidu said that InnoDB is a row lock and has transactions, so the efficiency is lower than MyIsam (but I need transactions, I cannot change the mysql engine on one server for several projects ). Some people also say that they use stored procedures or events, but if they use storage or events, they will not be able to see the effect of real-time processing. If you want to solve this problem, I still want to do a good job on the current page, because I cannot modify configuration files such as my. cnf ~~~~

I am suffering from the second problem for two days ~~~~ I still don't know if this problem is solved in the current version or in the database version ~~~
First run the code:
 
 Batch import
 
  Exec ("insert into stutable (stuname) values ('". $ stuname. "')") = "1") {return "imported successfully! ";} Else {return" import failed! ";}}// If ($ _ POST) {// File import if ($ _ FILES ["stuinfo"] ["error"]> 0) {echo" 
   ";} Else {$ extension = pathinfo ($ _ FILES [" stuinfo "] [" name "], PATHINFO_EXTENSION ); // get the extension of the uploaded file $ filename = "upload /". time (). ". ". $ extension; // The uploaded File name: move_uploaded_file ($ _ FILES ["stuinfo"] ["tmp_name"], $ filename ); // Move the uploaded temporary file to the specified directory $ file = fopen ($ filename, "r"); // open the file and read the information $ stuinfo = array (); while ($ data = fgetcsv ($ file) {array_push ($ stuinfo, implode ($ data) ;}fclose ($ file) ;}// traverse the array, insert data to the database print str_repeat ("", 4096); // to display the import progress at the same time, first output a bunch of null characters foreach ($ stuinfo as $ rs) {$ addresult = ""; $ addresult =" 
   
     "; // Directly call the addstu method to write data to the database $ addresult. ="  
   
Batch import student information
Name Import result
An error occurred while uploading the file. error message: ". $ _ FILES [" stuinfo "] [" error "]."
". $ Rs ." ". Addstu ($ rs )."

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.