php - Slow performance MySql -
i'm trying out performance of system i'm building, , it's slow, , don't know why or if should slow. i'm testing how many single insert can database , around 22 per second. sounds slow , when tried inserts singel big sql-query can insert 30000 records in 0.5 seconds. in real life inserts made different users in system overhead of connecting, sending query, parsing query etc. there. have tried far:
- mysqli little code possible. = 22 insert per second
- pdo little code possible. = 22 insert per second
- changing connection host localhost 127.0.0.1 = 22 insert per second
- mysqli without statement object , check sql-injection = 22 insert per second
so seams wrong here.
system specs:
- intel i5
- 16 gig ram
- 7200 rpm diskdrive
software:
- windows 10
- xampp, new mariadb
- db engine innodb.
the code used tests:
$amounttoinsert = 1000; //$fakedata array randomly generated emails $fakedata = getfakedata($amounttoinsert); $db = new databasehandler(); ($i = 0; $i < $amounttoinsert; $i++) { $db->insertuser($fakeusers[$i]); } $db->closeconnection();
the class calls database:
class databasehandler { private $dbhost = 'localhost'; private $dbusername = 'username'; private $dbpassword = 'password'; private $dbname = 'dbname'; private $dbport = 3306; private $mdb; private $isconnected = false; public function __construct() { $this->mdb = new mysqli($this->dbhost, $this->dbusername , $this->dbpassword, $this->dbname , $this->dbport); $this->isconnected = true; } public function closeconnection() { if ($this->isconnected) { $threadid = $this->mdb->thread_id; $this->mdb->kill($threadid); $this->mdb->close(); $this->isconnected = false; } } public function insertuser($user) { $this->mdb->autocommit(true); $querystring = 'insert `users`(`email`, `company_id`) ' .'values (?, 1)'; $stmt = $this->mdb->prepare($querystring); $stmt->bind_param('s', $user); if ($stmt->execute()) { $stmt->close(); return 1; } else { $stmt->close(); return 0; } } }
the "user" table has 4 columns following structure:
- id int unsigned primary key
- email varchar(60)
- company_id int unsigned index
- guid text
i'm @ loss here , don't know next. in right direction appreciated.
like it's explained in comments, it's innodb blame. default engine cautious , doesn't utilize disk cache, make sure data indeed has been written on disk, before returning success message. have 2 options.
most of time don't care confirmed write. can configure mysql setting mysql option zero:
innodb_flush_log_at_trx_commit = 0
as long it's set way, innodb writes fast myisam.
another option wrapping writes in single transaction. require single confirmation writes, reasonable fast too.
of course, it's sane prepare query once multiple inserts speed gain negligible compared issue above. doesn't count neither explanation nor remedy such issue.
Comments
Post a Comment