Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Created October 29, 2023 02:47
Show Gist options
  • 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','[email protected]'),(2,'bergstrom.vickie','[email protected]'),(3,'egrant','[email protected]'),(4,'aabbott','[email protected]'),(5,'jazmyne72','[email protected]'),(6,'lindgren.nannie','[email protected]'),(7,'eileen.pollich','o\'[email protected]'),(8,'clint33','[email protected]'),(9,'mwaelchi','[email protected]'),(10,'janie.gerhold','[email protected]'),(11,'aliyah16','[email protected]'),(12,'lfarrell','[email protected]'),(13,'hettie13','[email protected]'),(14,'jstracke','[email protected]'),(15,'prosacco.annabelle','[email protected]'),(16,'npagac','[email protected]'),(17,'smith.felton','[email protected]'),(18,'ethelyn58','[email protected]'),(19,'della.parker','[email protected]'),(20,'vivienne.hickle','[email protected]'),(21,'bosco.maxie','[email protected]'),(22,'tania.fadel','[email protected]'),(23,'sarai35','[email protected]'),(24,'levi.nicolas','[email protected]'),(25,'bergnaum.fanny','[email protected]'),(26,'schmitt.alysha','[email protected]'),(27,'iyundt','[email protected]'),(28,'ghartmann','[email protected]'),(29,'tdicki','[email protected]'),(30,'dickens.clemens','[email protected]'),(31,'colby10','[email protected]'),(32,'rosalyn42','[email protected]'),(33,'cameron77','[email protected]'),(34,'paul.anderson','[email protected]'),(35,'kertzmann.lisette','[email protected]'),(36,'corkery.sonya','[email protected]'),(37,'israel36','[email protected]'),(38,'becker.mia','[email protected]'),(39,'yrau','[email protected]'),(40,'rbayer','[email protected]'),(41,'alexandra.rice','[email protected]'),(42,'qborer','[email protected]'),(43,'lorena.wisozk','[email protected]'),(44,'deckow.tillman','[email protected]'),(45,'btreutel','[email protected]'),(46,'julia.powlowski','[email protected]'),(47,'kshlerin.brigitte','[email protected]'),(48,'christiansen.santos','[email protected]'),(49,'lucile00','[email protected]'),(50,'alba.franecki','[email protected]'),(51,'khaag','[email protected]'),(52,'ebechtelar','[email protected]'),(53,'towne.talia','[email protected]'),(54,'idickinson','[email protected]'),(55,'xferry','[email protected]'),(56,'selmer73','[email protected]'),(57,'jazmin.kutch','[email protected]'),(58,'uzemlak','[email protected]'),(59,'harvey.lola','[email protected]'),(60,'stoltenberg.harold','[email protected]'),(61,'autumn88','[email protected]'),(62,'ibotsford','[email protected]'),(63,'jameson.hoeger','[email protected]'),(64,'tnienow','[email protected]'),(65,'polly28','[email protected]'),(66,'treutel.cruz','[email protected]'),(67,'alverta29','[email protected]'),(68,'wbartell','[email protected]'),(69,'waelchi.golden','[email protected]'),(70,'thiel.javon','[email protected]'),(71,'rowe.dee','[email protected]'),(72,'rstroman','[email protected]'),(73,'wade.corwin','[email protected]'),(74,'cecile.tromp','[email protected]'),(75,'schoen.matteo','[email protected]'),(76,'elody.dicki','[email protected]'),(77,'leffler.keon','[email protected]'),(78,'xebert','[email protected]'),(79,'randal13','[email protected]'),(80,'alejandra.hills','[email protected]'),(81,'vicenta.cummerata','[email protected]'),(82,'denesik.brian','[email protected]'),(83,'zboncak.caleigh','[email protected]'),(84,'gilbert.mann','[email protected]'),(85,'edickinson','[email protected]'),(86,'ernie30','[email protected]'),(87,'lrunte','[email protected]'),(88,'oswift','[email protected]'),(89,'kassandra.schaden','[email protected]'),(90,'kgerhold','[email protected]'),(91,'lwest','[email protected]'),(92,'rice.christop','[email protected]'),(93,'trunolfsson','[email protected]'),(94,'demard','[email protected]'),(95,'tmoore','[email protected]'),(96,'erin29','[email protected]'),(97,'ted20','[email protected]'),(98,'harmon72','[email protected]'),(99,'kris.einar','[email protected]'),(100,'lora.kessler','[email protected]');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment