Question: The following php script takes about 22 seconds to execute. Is it normal? If not what would cause it to execute slowly?

```
$conn = mysql_connect('localhost', 'root', '123');
mysql_select_db('mydb', $conn);
$time1 = time();
for ($i=1;$i<500;$i++) {
mysql_query("Insert into accounts(id, username, email, password) VALUES(\"$i\", \"$i\",\"$i\",NOW())");
}
print time() - $time1; // return ~22

```

Edit: Table structure:

```
CREATE TABLE IF NOT EXISTS `accounts` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(20) NOT NULL,
  `email` varchar(200) NOT NULL,
  `password` varchar(20) NOT NULL,
  `status` varchar(10) DEFAULT 'pending',
  `email_newsletter_status` varchar(3) DEFAULT 'out',
  `email_type` varchar(4) DEFAULT 'text',
  `email_favorite_artists_status` varchar(3) DEFAULT 'out',
  `created_date` datetime DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `username` (`username`),
  UNIQUE KEY `email` (`email`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=7500 ;

```

This code is just to test insert speed. Using the Zend framework(insert method of Zend\_Db) I get a similar result, so I think it's relevant to mysql and not php code

Edit2: I know that there is much better ways to execute this query, but I want to know why this one is so slow.

Answer: 
Rather than run multiple insert statements, batch them up and execute it as one statement

```
 INSERT INTO example
  (example_id, name, value, other_value)
VALUES
  (100, 'Name 1', 'Value 1', 'Other 1'),
  (101, 'Name 2', 'Value 2', 'Other 2'),
  (102, 'Name 3', 'Value 3', 'Other 3'),
  (103, 'Name 4', 'Value 4', 'Other 4');

```

This will speed up your insert considerably
If you're using a stock `my.cnf` your performance may be terrible. You may want to look at how your MySQL is configured and adjust parameters accordingly. Tuning InnoDB, which is the better engine, requires adjusting different values than MyISAM which is often the fast-but-unsafe default.

If these queries are taking measurable amounts of time to execute, try watching with the MySQL administrator tool or execute `SHOW PROCESSLIST` frequently to see what the engine is doing.

Inserting 500 records through the MySQL driver directly should take, at most, only a few seconds on such a simple table.