I have an array of information about links I’ve found on a webpage in a PHP script.
Each link needs to be inserted into a MySQL myisam table.
Right now, I loop through the array and run an insert query for each link.
Is there a more appropriate way to do this so that I’m using MySQL more efficiently?
You can insert multiple values in a single statement using the following syntax:
see Section 12.2.5 of the MySQL manual
This is especially advisable if the MySQL server is on a different host than the PHP server: in the example above, you need only a single network roundtrip rather than three.
If network latency is not an issue (if the MySQL server runs on the same server as PHP), use prepared statements like this:
Prepared statements are slightly faster when executed repeatedly, because the MySQL server has to parse the SQL and run the query planner/optimizer only once.
If the table has indices, you can disable them before inserting and enable them again after inserting, this makes inserting rows faster because the indices don’t have to be updated after every insert (only possible for MyISAM, see section 12.1.7 of the Mysql manual):