One of the common tasks requested by our support customers is to optimize slow queries. We normally ask for the table structure(s), the problematic query and sample data to be able to reproduce the problem and resolve it by modifying the query, table structure, or global/session variables. Sometimes, we are given access to the server to test the queries on their live or test environment. But, more often than not, customers will not be able to provide us access to their servers or sample data due to security and data privacy reasons. Hence, we need to generate the test data ourselves.
A convenient way of generating test data is visiting http://generatedata.com which provides a web form where you can provide the columns and its corresponding data types, and turn them into test data. The website is capable of generating data in various formats such as Excel, HTML and JSON but for MySQL, you can choose either the CSV or SQL format.
The website limits you to generate up to a maximum of 100 rows. Fortunately, the web application is available for download so you can install it in your test server to generate data up to 100,000 rows instead.
Below are instructions on installing generatedata on CentOS 6:
1. Install Apache, PHP, MySQL and wget
# yum -y install mysql-server mysql httpd php php-mysql wget
2. Ensure Apache and MySQL runs on startup
# chkconfig httpd on
# chkconfig mysqld on
3. Start Apache and MySQL
# service httpd start
# service mysqld start
4. Create a MySQL user and database for the application
mysql> GRANT ALL PRIVILEGES ON generatedata.* TO generatedata@localhost IDENTIFIED BY ‘my-weak-password-please-do-not-use-this-password-in-a-production-environment’;
mysql> FLUSH PRIVILEGES;
mysql> CREATE DATABASE generatedata;
5. Download generatedata source and place it in the web directory
# wget https://github.com/benkeen/generatedata/archive/3.0.8.tar.gz -O 3.0.8.tar.gz
# tar xzvf 3.0.8.tar.gz
# mv generatedata-3.0.8 /var/www/html/generatedata
6. Make the application readable and writable by Apache
# chown -R apache:apache /var/www/html/generatedata
7. Open your browser to access the web application. In my case, it’s http://192.168.1.91/generatedata. It will prompt you to supply mysql credentials. Supply the MySQL credentials and click the “Continue” button to proceed.
8. The next screen will prompt you to create a settings.php file which will be used to store the custom settings for the application which include access credentials to MySQL. Click the “Create File” button to proceed.
Below are samples of what generatedata can produce:
For our example, let’s use the employees table structure from the Employees sample database.
1. Create the employees table under the test database
# mysql test
mysql> CREATE TABLE employees (
emp_no INT NOT NULL,
birth_date DATE NOT NULL,
first_name VARCHAR(14) NOT NULL,
last_name VARCHAR(16) NOT NULL,
gender ENUM ('M','F') NOT NULL,
hire_date DATE NOT NULL,
PRIMARY KEY (emp_no)
2. Enter the columns and corresponding datatypes in the web form. In this example, using the appropriate data type for each column is straightforward:
3. Export the data in MySQL Insert format. Under the EXPORTS TYPES section, select SQL tab and enter the following information:
4. Click the “Generate” button to generate the desired data. You can now copy the generated output and paste this in the MySQL console. On the other hand, you can also choose to download the generated rows as a file.
You can also opt to generate CSV output and use LOAD DATA INFILE to import the CSV data to your table. Under the EXPORT TYPES section, select the CSV tab. Inside the Generate frame, select “Prompt to download” and click the “Generate” button.
Upload the CSV file under the /tmp directory of your MySQL server and import it via the MySQL console:
# mysql test
LOAD DATA INFILE '/tmp/dataDec-11-2013.csv' INTO TABLE employees FIELDS TERMINATED BY '|' IGNORE 1 LINES;
Generating more than 100,000 rows at a time
Suppose you want to generate more than 100,000 rows, you can modify settings.php under /var/www/html/generatedata/settings.php and append $maxGeneratedRows = <maxGeneratedRows>; to the configuration file. The example below increases the maximum number of generated rows to 150,000.
$dbHostname = 'localhost';
$dbName = 'generatedata';
$dbUsername = 'generatedata';
$dbPassword = 'my-weak-password-please-do-not-use-this-password-in-a-production-environment';
$dbTablePrefix = 'gd_';
$encryptionSalt = 'ZPd';
$maxGeneratedRows = 150000;
There is just one major caveat to generating test data. Nothing compares to the actual data for reproducing query performance problems because its data and index cardinality will be entirely different from the generated test data. So, even if you’re able to fix the problem in your test environment, the solution may or may not apply to your production environment.