For any application and product which deals with database performance is always matter. If we are inserting multiple rows in database at a time execution time always comes in picture.
Today I have compare execution time for inserting multiples rows at a time using createCommand and save() methods.
Here is my both action for inserting multiple rows
I have tried createMultipleInsertCommand also. But it take more time compared to sql query. As you told using sql query security leakage comes into picture. And one thing that i noticed that if i try to insert 20,000 records at a time it is not working. It is working up to 18,000 records. I dont know what wrong with that.
Ya, It can be possible… But I have not much idea about PDO. But it is enough right now. It rarely happened that we are inserting 10000+ record at a time.
Ya… By this way it is possible. But now we conclude that for inserting multiple data createMultipleInsertCommand is better solution instead of insert data in loop and save method. what you say??
I think the golden ratio is createMultipleInsertCommand, because there are three important things
Fast,Secure and Professional.
If you need more speed you have to absolutely sure that the data is purified and safe for your database and for users of your website, in this case you could user a loop to make your query and then executes.
In general case I prefer createMultipleInsertCommand…
i would recommend to use createMultipleInsertCommand as it is much faster then single query insertion.
i tried with 1.5 Lakhs rows with single query insertion it took around 8-9 mins and at the other side when i used
createMultipleInsertCommand function then you can check the result below :
Starting Time : 1404215549.9961
Total Rows : 152061
It took 264.22411 sec
i used some other method to insert data like to fetch data in $data array variable then divide it into parts then use bulk insert with this method it will reduce the overhead from PDO also it will take less memory as well.