Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySql INSERT vs PHP file_put_contents

Tags:

php

mysql

I have a rapidly growing, write-heavy PHP/MySql application that inserts new rows at a rate of a dozen or so per second into an INNODB table of several million rows.

I started out using realtime INSERT statements and then moved to PHP's file_put_contents to write entries to a file and LOAD DATA INFILE to get the data into the database. Which is the better approach?

Are there any alternatives I should consider? How can I expect the two methods to handle collisions and increased load in the future?

Thanks!

like image 262
user1259956 Avatar asked Oct 09 '22 11:10

user1259956


1 Answers

Think of LOAD DATA INFILE as a batch-method of inserting data. It eliminates the overhead of firing up an insert query for every statement therefore is much faster. However, you lose some of the control when handling errors. It's much easier to handle an error on a single insert query vs one row in the middle of a file.

like image 199
Mike B Avatar answered Oct 12 '22 01:10

Mike B