Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Created October 29, 2023 02:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save code-boxx/55c1a448fdb456a1ff6de33c23b2b64b to your computer and use it in GitHub Desktop.
Save code-boxx/55c1a448fdb456a1ff6de33c23b2b64b to your computer and use it in GitHub Desktop.
Benchmark MYSQL Queries

BENCHMARK MYSQL QUERY PERFORMANCE

https://code-boxx.com/benchmark-measure-mysql-query-time/

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

1) Open the command line (or terminal)
2) Run "mysql --user root --database test"
3) SELECT * FROM `users`;
4) The query execution time should be right at the bottom.
MYSQL tools & managers should also show query execution time after running.
https://www.mysql.com/products/workbench/
https://www.phpmyadmin.net/
1) Open the command line (or terminal)
2) Run "mysql --user root --database test"
3) SET profiling = 1;
4) SELECT * FROM `users`;
5) Any other statements you want to run.
6) SHOW PROFILES;
7) SHOW PROFILE FOR QUERY 1;
1) Open the command line (or terminal)
2) Run "mysql --user root --database test"
3) BENCHMARK(1000, 1+1)
*) That is, BENCHMARK(LOOP, STATEMENT). STATEMENT must be scalar, return only one result.
<?php
// (A) CONNECT TO DATABASE
$dbhost = "localhost";
$dbname = "test";
$dbchar = "utf8";
$dbuser = "root";
$dbpass = "";
$dbset = "mysql:host=$dbhost;dbname=$dbname;charset=$dbchar";
$pdo = new PDO($dbset, $dbuser, $dbpass);
// (B) TEST SETTINGS
$sql = "SELECT * FROM `users`";
$params = null;
$runs = 30000;
// (C) TIME QUERY
$start = microtime(true);
$stmt = $pdo->prepare($sql);
for ($i=0; $i<$runs; $i++) { $stmt->execute($params); }
$end = microtime(true);
// (D) CLOSE & RESULTS
$taken = $end - $start;
$average = $taken / $runs;
echo "TOTAL - $taken seconds | AVERAGE - $average seconds";
https://dev.mysql.com/downloads/benchmarks.html
https://github.com/nuodb/dbt2
https://github.com/akopytov/sysbench
CREATE TABLE `users` (
`user_id` int(11) NOT NULL AUTO_INCREMENT,
`user_name` varchar(255) NOT NULL,
`user_email` varchar(255) NOT NULL,
PRIMARY KEY (`user_id`),
UNIQUE KEY `user_email` (`user_email`)
) ENGINE=InnoDB AUTO_INCREMENT=101 DEFAULT CHARSET=utf8;
INSERT INTO `users` VALUES (1,'lnolan','ijohnston@example.org'),(2,'bergstrom.vickie','rutherford.madilyn@example.net'),(3,'egrant','roberbrunner@example.com'),(4,'aabbott','constance.rutherford@example.org'),(5,'jazmyne72','hhuel@example.com'),(6,'lindgren.nannie','rahsaan39@example.org'),(7,'eileen.pollich','o\'keefe.trever@example.net'),(8,'clint33','olaf88@example.net'),(9,'mwaelchi','francis51@example.com'),(10,'janie.gerhold','kenneth19@example.com'),(11,'aliyah16','greg.runolfsdottir@example.org'),(12,'lfarrell','halvorson.hal@example.com'),(13,'hettie13','oking@example.com'),(14,'jstracke','keira25@example.net'),(15,'prosacco.annabelle','gorczany.margret@example.net'),(16,'npagac','lemke.aliyah@example.org'),(17,'smith.felton','pfahey@example.com'),(18,'ethelyn58','krajcik.gayle@example.com'),(19,'della.parker','carmella19@example.com'),(20,'vivienne.hickle','terrance.dooley@example.net'),(21,'bosco.maxie','drake.armstrong@example.org'),(22,'tania.fadel','ebergstrom@example.org'),(23,'sarai35','jgreen@example.org'),(24,'levi.nicolas','sebastian51@example.net'),(25,'bergnaum.fanny','frami.quinn@example.org'),(26,'schmitt.alysha','marcelina.brakus@example.com'),(27,'iyundt','ykeebler@example.net'),(28,'ghartmann','maximilian.cassin@example.org'),(29,'tdicki','kling.keagan@example.com'),(30,'dickens.clemens','abshire.mittie@example.net'),(31,'colby10','austen.kuvalis@example.net'),(32,'rosalyn42','hrice@example.com'),(33,'cameron77','mcclure.jessy@example.com'),(34,'paul.anderson','lulu.pfeffer@example.com'),(35,'kertzmann.lisette','keyon56@example.net'),(36,'corkery.sonya','jwalter@example.net'),(37,'israel36','schumm.angelina@example.org'),(38,'becker.mia','kbode@example.org'),(39,'yrau','dario83@example.org'),(40,'rbayer','elisa.hartmann@example.net'),(41,'alexandra.rice','warren41@example.com'),(42,'qborer','aimee.simonis@example.net'),(43,'lorena.wisozk','gjones@example.net'),(44,'deckow.tillman','flavie96@example.org'),(45,'btreutel','osinski.winifred@example.net'),(46,'julia.powlowski','hmoen@example.org'),(47,'kshlerin.brigitte','rowan.rosenbaum@example.com'),(48,'christiansen.santos','deborah54@example.net'),(49,'lucile00','vincenzo26@example.net'),(50,'alba.franecki','lehner.edward@example.org'),(51,'khaag','bergstrom.kian@example.net'),(52,'ebechtelar','mfeest@example.org'),(53,'towne.talia','madaline.leffler@example.com'),(54,'idickinson','olesch@example.net'),(55,'xferry','graham50@example.com'),(56,'selmer73','wkling@example.org'),(57,'jazmin.kutch','jrenner@example.com'),(58,'uzemlak','luettgen.nicola@example.org'),(59,'harvey.lola','vgleason@example.org'),(60,'stoltenberg.harold','kohler.theresa@example.net'),(61,'autumn88','llewellyn.daniel@example.org'),(62,'ibotsford','sunny83@example.net'),(63,'jameson.hoeger','oparker@example.net'),(64,'tnienow','zackery94@example.org'),(65,'polly28','cbogan@example.net'),(66,'treutel.cruz','clangworth@example.com'),(67,'alverta29','roxane82@example.com'),(68,'wbartell','catalina83@example.org'),(69,'waelchi.golden','theodora.oberbrunner@example.net'),(70,'thiel.javon','giovanna.padberg@example.net'),(71,'rowe.dee','durgan.loyal@example.net'),(72,'rstroman','jaden14@example.com'),(73,'wade.corwin','maximillia.runte@example.net'),(74,'cecile.tromp','edna53@example.com'),(75,'schoen.matteo','kschowalter@example.net'),(76,'elody.dicki','ykutch@example.com'),(77,'leffler.keon','ebba.streich@example.net'),(78,'xebert','lakin.bill@example.org'),(79,'randal13','delilah.mertz@example.com'),(80,'alejandra.hills','wilber79@example.com'),(81,'vicenta.cummerata','torrance.stroman@example.net'),(82,'denesik.brian','nat94@example.org'),(83,'zboncak.caleigh','howell.andres@example.org'),(84,'gilbert.mann','jeffry.rempel@example.org'),(85,'edickinson','marianna40@example.net'),(86,'ernie30','owalker@example.net'),(87,'lrunte','ugusikowski@example.org'),(88,'oswift','samson86@example.com'),(89,'kassandra.schaden','sofia.gorczany@example.org'),(90,'kgerhold','kaela.jenkins@example.com'),(91,'lwest','janiya80@example.net'),(92,'rice.christop','eden.murazik@example.net'),(93,'trunolfsson','hyatt.darian@example.org'),(94,'demard','zolson@example.org'),(95,'tmoore','hubert.beer@example.com'),(96,'erin29','braun.edmund@example.net'),(97,'ted20','adelbert.mitchell@example.org'),(98,'harmon72','armand.zieme@example.org'),(99,'kris.einar','walter.okuneva@example.net'),(100,'lora.kessler','donny.marquardt@example.net');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment