English 中文(简体)
利用Mysql在相关表格上做多个INSERT
原标题:Using Mysql to do multiple INSERT on linked tables

我有两个表,一个与另一个表的主轴有关。 当I INSERT进入表A时,获得LAST_INSERT_ID,然后将INSERT列入表B。

但我有数百个记录要插入,我希望加快步伐。

在我的餐厅,你可以:

INSERT INTO table_a (v1, v2, c3) VALUE (0, 1, 2);

INSERT INTO table_a (v1, v2, v3) VALUE (4, 5, 6); 

INSERT INTO table_a (v1, v2, v3) VALUE (0, 1, 2), (4, 5, 6), 等, 以便更快地增加多个条目,但只有一个表格。

当然,后者要更快。 我很想知道,能否把这一行为作为我的榜样,与两个使用储存程序的相关表格重复,以及它是否会在业绩方面有类似的显著改善:

诸如:特别要求(0、1、2、4、5、6等);或类似。

我没有仓储程序的经验,因此,我会为这方面的想法捕鱼。

最佳回答

在进一步调查之后,似乎似乎表明,SP不会提供显著的快速改进,也不能接受INSERT INTO等大参数。

MySQL 储存程序诉复杂查询

But I still needed to insert a fairly large number of linked records in one so I did the following:

INSERT INTO a (x, y) VALUES (1,2), (3,4), (5,6), ... (N-1, N)

id = GET_LAST INSERT_ID

ids范围从id到id+ 只要我们使用非行表格:

MySQLLAST_INSERT_ID()使用多种记录,INSERT 声明

MySQL LAST_INSERT_ID() used with multiple records INSERT statement

之后

INSERT INTO b (a_id, z) VALUES (id,2), (id+1,4), (id+2,6), ... (id+N, 11) only gotcha is you need to know your mysql increment increment from replication.

问题回答

这里是MySql储存的程序,我们可以按照我们的定义制作数千份记录。

drop procedure batchInsertUser;
call batchInsertUser(2);

DELIMITER ;;
CREATE PROCEDURE batchInsertUser(totalRecs int)
BEGIN
  SET @createdRecs = 0;
  SET @maxDataId = (SELECT MAX(id) FROM userTable);
  WHILE @createdRecs < totalRecs DO
    SET @createdRecs = @createdRecs + 1;
    SET @maxDataId = @maxDataId + 1;
    SET @userName = CONCAT( batch , @maxDataId );
    INSERT INTO userTable (id, username, created_at, updated_at)
      VALUES (@maxUserId,  @userName, now(), now());
    INSERT INTO userProfile
      (user_id, gender, birthday, created_at, updated_at)
      VALUES
      (@maxUserId, @userName,  F ,  1999-01-10 , now(), now());
END ;;




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签