Mysql表插入性能

I have table with following schema: http://i.imgur.com/jn2nrFT.png

Its MySQL, running on InnoDB, on basic Digital Ocean host ( 512 MB, 1 CPU ). I am using PHP/Doctrine, and in my code, i do something like

for( $i = 0; $i < 5000; ++$i ) {
    $row = findRowInTable();
    if (! $row ) {
        insertNewRow();
    }
}
$em->flush();

It's slow, it takes something like 20s when table is empty, and 50s when table have 5000 rows.

How can I optimize it? I will be adding at most 5K rows, but I need to get this process under 120 secs even if table will be 200K rows long

You could try to find the rows that are not in database, outside the loop, try to find the rows with a single query, and let php iterate.

$rowsARR = findRowsNotInTable();
for( $i = 0; $i < sizeof($rowsARR); ++$i ) {
       insertTheRow($rowsARR[i]);
}
$em->flush();