기존 INSERT: insert into table values(1,2,3);

  • 153초 ~ 161초

다중 INSERT: insert into table values(1,2,3),(3,4,5) ... ;

  • 약 11.5초 ~ 12초

auto commit = 0 일때 INSERT: start transaction; insert into table values(1,2,3);

  • 약 15.3초 ~ 16초

auto commit = 0 일때 다중 INSERT: start transaction; insert into table values(1,2,3),(3,4,5) ... ;

  • 약 3.3초~ 4초

 

PHP 는 $pdo->beginTransaction() 를 실행시키면 트랜잭션모드가 실행된다.

autocommit이 자동으로 false 로 변하면서 트랜잭션 내부에서 쿼리가 도는데

 

솔직히  사용했을경우 멀티플 insert를 하나 안 하나 큰 차이가 없을 줄 알았는데 차이가 심했다.

 

테이블 제약조건 이였나 유니크 키나 insert 하는 거에 있어서 if 문 쓰듯 체크하는 조건은 먼저 없애준 후에

INSERT 하고 그 후에 제약조건 다시 걸어주면 빠르다고 하던데 나중에 한번 테스트해봐야겠다. 

'MYSQL' 카테고리의 다른 글

트랜잭션할 때 적용이 안된다면 확인해야 할점  (0) 2023.04.11
mysql 스키마 조회 쿼리  (0) 2022.03.02
 
public function insertData_trans2(array $data, string $tablename): int
  {
    $cnt = 0;
    $this->beginTransaction();
    try {
      foreach ($data as $row) {
        $cnt += $this->insertData($row, $tablename);
      }
      $this->commit();
    } catch (Exception $e) {
      $this->rollback();
      throw $e;
    }
    return $cnt;
  }
public function insertData(array $data, string $tablename)
  {
    $columns = implode(", ", array_keys($data));
    $placeholders = implode(", ", array_fill(0, count($data), "?"));
    $values = array_values($data);
    $stmt = $this->conn->prepare("INSERT INTO $tablename ($columns) VALUES ($placeholders)");
    $stmt->execute($values);
    return $stmt->rowCount();
  }

아무리 에러를 일부러 터뜨리고 뭔짓을하더라도 데이터가 들어가길래 삽질하던 찰나

 

엔진이 문제인걸 알았다

 

MYISAM 으로 되어있었다.... Myisam 은 트랜잭션을 지원하지않는다.

InnoDB 로 변경하였더니 잘되었다.

'MYSQL' 카테고리의 다른 글

데이터 100,000 건 insert 테스트  (0) 2023.04.11
mysql 스키마 조회 쿼리  (0) 2022.03.02

*테이블 조회

SELECT 
    TABLE_SCHEMA,table_name, table_comment
FROM
    information_schema.tables
WHERE
    table_comment like '%%';

 

*칼럼조회

SELECT
    table_name, column_name, column_comment
FROM
    information_schema.columns
WHERE
    column_comment like '%처리량%';

+ Recent posts