developer tip

많은 양의 테스트 데이터로 데이터베이스 테이블 채우기

copycodes 2020. 12. 30. 08:17
반응형

많은 양의 테스트 데이터로 데이터베이스 테이블 채우기


많은 양의 테스트 데이터가있는 테이블을로드해야합니다. 성능 및 확장 테스트에 사용됩니다.

데이터베이스 테이블에 대해 100,000 행의 임의 / 정크 데이터를 쉽게 만들 수있는 방법은 무엇입니까?


저장 프로 시저를 사용할 수도 있습니다 . 다음 표를 예로 고려하십시오.

CREATE TABLE your_table (id int NOT NULL PRIMARY KEY AUTO_INCREMENT, val int);

그런 다음 다음과 같은 저장 프로 시저를 추가 할 수 있습니다.

DELIMITER $$
CREATE PROCEDURE prepare_data()
BEGIN
  DECLARE i INT DEFAULT 100;

  WHILE i < 100000 DO
    INSERT INTO your_table (val) VALUES (i);
    SET i = i + 1;
  END WHILE;
END$$
DELIMITER ;

호출하면 10 만 개의 레코드가 있습니다.

CALL prepare_data();

다중 행 복제 (데이터 복제)의 경우 다음을 사용할 수 있습니다.

DELIMITER $$
CREATE PROCEDURE insert_test_data()
BEGIN
  DECLARE i INT DEFAULT 1;

  WHILE i < 100000 DO
    INSERT INTO `table` (`user_id`, `page_id`, `name`, `description`, `created`)
    SELECT `user_id`, `page_id`, `name`, `description`, `created`
    FROM `table`
    WHERE id = 1;
    SET i = i + 1;
  END WHILE;
END$$
DELIMITER ;
CALL insert_test_data();
DROP PROCEDURE insert_test_data;

데이터를 더 많이 제어하려면 다음과 같이 시도하십시오 (PHP에서).

<?php
$conn = mysql_connect(...);
$num = 100000;

$sql = 'INSERT INTO `table` (`col1`, `col2`, ...) VALUES ';
for ($i = 0; $i < $num; $i++) {
  mysql_query($sql . generate_test_values($i));
}
?>

여기서 generate_test_values ​​함수는 "( 'val1', 'val2', ...)"와 같은 형식의 문자열을 반환합니다. 시간이 오래 걸리면 일괄 처리하여 db 호출을 너무 많이하지 않도록 할 수 있습니다. 예 :

for ($i = 0; $i < $num; $i += 10) {
  $values = array();
  for ($j = 0; $j < 10; $j++) {
    $values[] = generate_test_data($i + $j);
  }
  mysql_query($sql . join(", ", $values));
}

각각 10 개의 행을 추가하는 10000 개의 쿼리 만 실행합니다.


다음은 순수한 수학 및 SQL을 사용한 솔루션입니다.

create table t1(x int primary key auto_increment);
insert into t1 () values (),(),();

mysql> insert into t1 (x) select x + (select count(*) from t1) from t1;
Query OK, 1265 rows affected (0.01 sec)
Records: 1265  Duplicates: 0  Warnings: 0

mysql> insert into t1 (x) select x + (select count(*) from t1) from t1;
Query OK, 2530 rows affected (0.02 sec)
Records: 2530  Duplicates: 0  Warnings: 0

mysql> insert into t1 (x) select x + (select count(*) from t1) from t1;
Query OK, 5060 rows affected (0.03 sec)
Records: 5060  Duplicates: 0  Warnings: 0

mysql> insert into t1 (x) select x + (select count(*) from t1) from t1;
Query OK, 10120 rows affected (0.05 sec)
Records: 10120  Duplicates: 0  Warnings: 0

mysql> insert into t1 (x) select x + (select count(*) from t1) from t1;
Query OK, 20240 rows affected (0.12 sec)
Records: 20240  Duplicates: 0  Warnings: 0

mysql> insert into t1 (x) select x + (select count(*) from t1) from t1;
Query OK, 40480 rows affected (0.17 sec)
Records: 40480  Duplicates: 0  Warnings: 0

mysql> insert into t1 (x) select x + (select count(*) from t1) from t1;
Query OK, 80960 rows affected (0.31 sec)
Records: 80960  Duplicates: 0  Warnings: 0

mysql> insert into t1 (x) select x + (select count(*) from t1) from t1;
Query OK, 161920 rows affected (0.57 sec)
Records: 161920  Duplicates: 0  Warnings: 0

mysql> insert into t1 (x) select x + (select count(*) from t1) from t1;
Query OK, 323840 rows affected (1.13 sec)
Records: 323840  Duplicates: 0  Warnings: 0

mysql> insert into t1 (x) select x + (select count(*) from t1) from t1;
Query OK, 647680 rows affected (2.33 sec)
Records: 647680  Duplicates: 0  Warnings: 0

테이블 사이에 외래 키 유효성 검사가없는 거의 "모든"데이터베이스에 삽입 할 수있는 루비 스크립트를 만들고 임의의 데이터를 삽입하므로 일부 데이터로 데이터베이스를 벤치마킹 할 수 있습니다. 이 GIST-> https://gist.github.com/carlosveucv/137ea32892ef96ab496def5fcd21858b 에서 나중에 (여유 시간이있을 때) gem을 만들 것입니다.


filldb 시도

스키마를 게시하거나 기존 스키마를 사용하고 더미 데이터를 생성하고이 사이트에서 내보내고 데이터베이스에서 가져올 수 있습니다.

참조 URL : https://stackoverflow.com/questions/3766282/fill-database-tables-with-a-large-amount-of-test-data

반응형