Reputation: 13512
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?
Upvotes: 0
Views: 263
Reputation: 12031
You can insert multiple values in a single statement using the following syntax:
INSERT INTO tbl_name (a,b,c) VALUES(1,2,3),(4,5,6),(7,8,9);
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:
$dbh = new PDO("mysql:host=127.0.0.1;port=3306;dbname=test", "root", "");
$sth = $dbh->prepare("INSERT INTO links(title, href) VALUES(:title, :href);");
while ($link = getNextLink()) {
$sth->execute( array( "title" => $link->title, "href" => $link->href ) );
}
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):
ALTER TABLE links DISABLE KEYS
-- insert rows
ALTER TABLE links ENABLE KEYS
Upvotes: 2
Reputation: 265
A while or a for each loop is sufficient enough I would think for this, seeing as the information is going to be large and I imagine dynamic.
You can use one query but if you have hundreds or thousands of pieces of information, or if you are going to be constantly updating the database with new data, then an array with a for each or while loop is best for this. If its just a one off with only a few pieces of information then just write out the QUERY yourself as follows
mysql_query("INSERT INTO table (column1, column2) VALUES ('VALUE','VALUE'),('VALUE','VALUE')");
Upvotes: 0
Reputation: 20492
You can insert several records with a single statement:
<?php
// Our MySQL query
$sql = "INSERT INTO beautiful (name, age)
VALUES
('Helen', 24),
('Katrina', 21),
('Samia', 22),
('Hui Ling', 25),
('Yumie', 29)";
mysql_query( $sql, $conn );
?>
Source: http://www.desilva.biz/mysql/insert.html
Upvotes: 7