Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fast way to insert millions record into sql server using php

Tags:

php

sql-server

I have text file with 2,000,000 line and want to insert each line in a database row so, inserting 2,000,000 record into sql server using php file. What is the fastest way to do that?

For testing I am using this code:

for ( $i=1 ; $i<=2000000 ; $i++)
    {
        $sql = "INSERT INTO BlastSequenceDim (Seq_id) VALUES ('$i')";
        $stmt = sqlsrv_query( $conn, $sql);
        if( $stmt === false ) 
        {
            die( print_r( sqlsrv_errors(), true));
        }
    }

But, it takes a lot of time. Can this query executes in seconds?

Thanks,

like image 358
Alaa Avatar asked Dec 19 '25 19:12

Alaa


1 Answers

Please use Bulk insert this will significantly improve the performance and save time .

learn more about Bulk insert query in SQL

You can also save time by cutting out PHP and directly accessing the SQL database, this would be common practice to initially populate a db.

Another link

like image 158
Shadi Zidan Avatar answered Dec 22 '25 12:12

Shadi Zidan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!