This topic describes how to generate test data in batches by using shell scripts, stored procedures, and OceanBase Developer Center (ODC).
Prerequisites
- You have deployed an OceanBase cluster and created a MySQL tenant. For more information about how to deploy an OceanBase cluster, see Deployment overview.
- You have the
CREATE,INSERT, andSELECTprivileges. For more information about how to view the privileges of the current user, see View user privileges. If you do not have these privileges, contact the administrator to obtain them. For more information about how to grant privileges to a user, see Grant direct privileges.
Generate test data in batches by using shell scripts
You can write a shell script to generate SQL scripts in batches, which effectively simplifies the process of inserting a large amount of test data and avoids the need to manually write complex SQL statements. This method allows you to generate a large amount of test data as needed, improving efficiency and reducing the workload of manual operations.
Procedure
- Create a test database and test tables.
- Create a shell script.
- Run an SQL script.
- View the data.
Step 1: Create a test database and a test table
Use a database management tool (such as a CLI tool or a GUI tool) to create a database for storing test data and create corresponding test table schemas in the database.
Connect to the prepared MySQL tenant.
Here is an example:
obclient -hxxx.xxx.xxx.xxx -P2881 -uroot@mysql001 -p****** -ACreate a test database.
Here is an example:
Execute the following SQL statement to create a test database named
test_sql_file_db.CREATE DATABASE test_sql_file_db;For more information about how to create a database, see Create a database.
Create a test table.
Here is an example:
Execute the following SQL statement to create a test table named
test_sql_file_db.test_sql_file_tbl1.CREATE TABLE test_sql_file_db.test_sql_file_tbl1 (id INT, name VARCHAR(50), email VARCHAR(50));For more information about how to create a table, see Create a table.
Step 2: Create a shell script
Use a text editor to create a shell script file. You can add the .sh extension to the file name. In the shell script, use the output redirection symbols (> or >>) to write the generated test data to an SQL script file. During a loop or traversal, write the data in the form of SQL statements (INSERT) to the SQL script file.
Open the terminal.
Create a shell script file.
Use the
viorvimeditor to create a new shell script file.Here is an example:
Run the following command to write a shell script named
generate_sql.sh.vi generate_sql.shEnter the edit mode.
Press the i or Insert key to enter insert mode in the
viorvimeditor. You can edit the file content in the insert mode.Write the logic of the shell script.
In edit mode, write the logic and commands for the shell script. These commands can include shell commands, conditional statements, loop structures, and functions.
Here is an example:
The
generate_sql.shscript is as follows:#!/bin/bash # Define the name of the SQL file. SQL_FILE="insert_test_sql_file_tbl1.sql" # Create an SQL file. touch $SQL_FILE # Define an SQL statement. INSERT_SQL="INSERT INTO test_sql_file_tbl1 (id, name, email) VALUES " # Generate 100,000 user records in a loop. for ((i=1; i<=100000; i++)) do user_id=$i user_name="user_$i" user_email="user_$i@example.com" values="($user_id, '$user_name', '$user_email')" if (($i == 100000)) then INSERT_SQL="$INSERT_SQL$values;" else INSERT_SQL="$INSERT_SQL$values, " fi done # Write the SQL statement to the SQL file. echo $INSERT_SQL >> $SQL_FILENote
- The script will generate an SQL file named
insert_test_sql_file_tbl1.sqland insert 100,000 user records into the file. You can modify the SQL statements and the number of user records generated in a loop as needed. - Before you insert a large amount of data, check the resource usage of the relevant servers to avoid data insertion failures or performance degradation due to high resource usage.
- The script will generate an SQL file named
Save the file.
Press the Esc key to exit the insert mode and enter the
:wqcommand to save the file and exit theviorvimeditor.Run the shell script file.
Execute the created shell script to generate an SQL script in the terminal.
Here is an example:
Run the following command to execute the created shell script. This command will generate a SQL script named
insert_test_sql_file_tbl1.sqlin the current directory, which contains 100,000INSERTstatements.sudo bash generate_sql.sh
Step 3: Run the SQL script
You can use the CLI to import the data from the SQL script file.
Note
For more information about how to execute an SQL script, see Import data from an SQL file to OceanBase Database.
obclient -h$host -u$user_name -P$port -p$password -D$database_name < $sql_file
The parameters are described as follows:
$host: the IP address for connecting to OceanBase Database. For connection through OceanBase Database Proxy (ODP), use the IP address of an ODP. For direct connection, use the IP address of an OBServer node.$port: the port for connecting to OceanBase Database. For connection through ODP, the default value is2883, which can be customized when ODP is deployed. For direct connection, the default value is2881, which can be customized when OceanBase Database is deployed.$database_name: the name of the database to access.Notice
The user used to connect to the tenant must have the
CREATE,INSERT, andSELECTprivileges on the database. For more information about user privileges, see Privilege types in MySQL mode.$user_name: the tenant account. For connection through ODP, the tenant account can be in theusername@tenant name#cluster nameorcluster name:tenant name:usernameformat. For direct connection, the tenant account is in theusername@tenant nameformat.$password: the password of the account.$sql_file: the name of the SQL script file.Note
When you execute an SQL script file, specify the absolute path of the SQL script file.
Here is an example:
Run the following command to connect to a specified OceanBase Database server and import all INSERT statements in the SQL script file to a database named test_sql_file_db one by one, so as to insert 100,000 records into the insert_test_sql_file_tbl1 table.
obclient -hxxx.xxx.xxx.xxx -uroot@mysql001 -P2881 -p****** -Dtest_sql_file_db < /home/admin/test_data/insert_test_sql_file_tbl1.sql
Step 4: View the data
Execute the following SQL statement to view the number of rows in the test_sql_file_db.test_sql_file_tbl1 table.
obclient [(none)]> SELECT count(*) FROM test_sql_file_db.test_sql_file_tbl1;
The return result is as follows:
+----------+
| count(*) |
+----------+
| 100000 |
+----------+
1 row in set
Generate test data in batches by using a stored procedure
Generating test data in batches by using a stored procedure is an effective method. You can write a stored procedure to automatically generate a large amount of test data.
Procedure
- Create a test database and a test table.
- Create a stored procedure.
- Call the stored procedure.
- View the data.
Step 1: Create a test database and test tables
You can use a database management tool such as the CLI tool or a GUI-based tool to create a database for storing test data and create corresponding test table schemas in the database.
Connect to the prepared MySQL tenant.
Here is an example:
obclient -hxxx.xxx.xxx.xxx -P2881 -uroot@mysql001 -p****** -ACreate a test database.
Here is an example:
Execute the following SQL statement to create a test database named
test_db.obclient [(none)]> CREATE DATABASE test_db;Execute the following SQL statement to switch to the
test_dbdatabase.obclient [(none)]> use test_db;The return result is as follows:
Database changed obclient [test_db]>Create a test table.
Here is an example:
Execute the following SQL statement to create a table named
test_pro_tbl1, which contains four fields:idis an integer field defined as the primary key and uses auto-increment for numbering.create_timeis a datetime field that represents the creation time of the row. It is set to the current time by default using DEFAULT CURRENT_TIMESTAMP.nameis a character field with a maximum length of 50 characters.enrollment_dateis a date field that is used to store date data.
obclient [test_db]> CREATE TABLE test_pro_tbl1 ( id INT NOT NULL AUTO_INCREMENT, create_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, name VARCHAR(50), enrollment_date DATE, PRIMARY KEY(id));
Step 2: Create a stored procedure
Specify a custom delimiter.
Here is an example:
Use the
DELIMITERstatement to specify//as the custom delimiter.DELIMITER //Create a stored procedure.
Here is an example:
Execute the following SQL statement to create a stored procedure named
pro_generate_data. The input parameternspecifies the number of records to insert. A loop andINSERTstatements are used to generate and insert data. In this example,test_pro_tbl1is the target table, whilenameandenrollment_dateare the fields where the data is inserted. The loop counteriis used to generate values, with theCONCATfunction creating names and theDATE_ADDfunction generating dates.CREATE PROCEDURE pro_generate_data(IN n INT) BEGIN DECLARE i INT DEFAULT 1; WHILE i <= n DO INSERT INTO test_pro_tbl1 (name, enrollment_date) VALUES (CONCAT('Name', i), DATE_ADD('2022-01-01', INTERVAL i DAY)); SET i = i + 1; END WHILE; END; //For more information about how to create a stored procedure, see Stored procedures.
Restore to the default delimiter, which is semicolon (;).
DELIMITER ;
Step 3: Call the stored procedure
Use the CALL statement to call the stored procedure to execute the test data generation logic. You can pass parameters to the stored procedure to specify the number of data records to be generated.
Here is an example:
Execute the following SQL statement to call the pro_generate_data stored procedure and set the value of the input parameter to 100,000, indicating that 100,000 data records need to be inserted.
obclient [test_db]> CALL pro_generate_data(100000);
Note
You can increase or decrease the size of the input parameter to control the amount of test data. When adjusting the parameter size, you need to consider database performance and storage space limitations to avoid generating excessive data that could cause database crashes or insufficient storage space.
Step 4: View the data
Execute the following SQL statement to view the number of data rows in the test_pro_tbl1 table.
obclient [test_db]> SELECT count(*) FROM test_pro_tbl1;
The return result is as follows:
+----------+
| count(*) |
+----------+
| 100000 |
+----------+
1 row in set
Generate test data in batches by using ODC
ODC is an enterprise-level database development platform tailored for OceanBase Database. For more information about ODC, see What is ODC?
ODC provides the data mocking feature that can generate data based on field types in a table. This can meet your requirement for generating a large amount of data during database performance tests or feature verification. For more information about the data mocking feature provided by ODC, see Data mocking.
References
- For more information about how to connect to a database, see Overview of connection methods.
- For more information about how to drop a table, see Drop a table.
- For more information about how to delete data, see Delete data.