简体   繁体   English

MySQL INSERT或SELECT

[英]MySQL INSERT or SELECT

I have 2 tables, table_a and table_b. 我有2个表table_a和table_b。

CREATE TABLE `table_a` (
`ID` int(11) unsigned NOT NULL AUTO_INCREMENT,
 `val` varchar(64) NOT NULL,
 PRIMARY KEY (`ID`),
 UNIQUE KEY `val` (`val`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1

CREATE TABLE `table_b` (
 `ref_a` int(11) unsigned NOT NULL,
 `data` int(11) unsigned NOT NULL,
 UNIQUE KEY `unique_index` (`ref_a`,`data`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1

I would like to MASS INSERT into Table B with ref_a value referring to ID of Table A. 我想MASS INSERT到表B中,ref_a值指的是表A的ID。

This is what I'm trying to accomplish: 这就是我想要完成的事情:

SELECT ID FROM table_a WHERE val = "123"

If the value doesn't exist, then insert the value 如果该值不存在,则插入该值

INSERT INTO table_a(val) VALUES("123")

Now that I got the ID (assume it's "1"), I want to insert into table_b: 现在我得到了ID(假设它是“1”),我想插入table_b:

INSERT INTO table_b(ref_a, data) VALUES(1, 75)

The problem comes in when I want to do it in bulk. 当我想批量进行时,问题出现了。 Will my performance go down if I alternate between SELECTS and INSERTS instead of doing a bulk insert, then bulk select? 如果我在SELECTS和INSERTS之间交替而不是进行批量插入,那么我的表现是否会下降,然后批量选择?

I could do: 我可以:

INSERT INTO table_b(ref_a, data) VALUES((SELECT ID FROM table_a WHERE value="123"), 75)

but, what if the value is missing and I need to do an insert prior. 但是,如果缺少值并且我需要事先插入,该怎么办?

I cannot BEGIN TRANSACTION and COMMIT giving that I need to retrieve the ID of table A after an insert. 我不能BEGIN TRANSACTION和COMMIT,因为我需要在插入后检索表A的ID。

I can also do: 我也可以这样做:

  • Bulk insert into table A. 批量插入表A.
  • Select all IDs from table A. 从表A中选择所有ID。
  • Insert into table B using selected IDs. 使用选定的ID插入表B。

But im looking for an alternative way. 但我正在寻找另一种方式。

What would be the MOST EFFICIENT way to accomplish what im trying to do? 什么是最有效的方式来实现我想要做的事情?

First make sure you have a UNIQUE INDEX on value column in table_a, then do a bulk INSERT IGNORE 首先确保table_a中的值列上有UNIQUE INDEX ,然后执行批量INSERT IGNORE

INSERT IGNORE INTO table_a(value) VALUES("123"), ("345"), ("afasd"), ("#$#$%"), ...

Now you are sure that all the values are in table_a and you can safely use your bulk table_b insert method. 现在您确定所有值都在table_a中,并且您可以安全地使用批量table_b插入方法。

I suggest you to use a stored procedure that will do the checking for a right insert: stored routine 我建议你使用一个存储过程来检查正确的插入: 存储例程

It will be something like 它会是这样的

CREATE PROCEDURE insertdata(IN i_value VARCHAR(32), IN i_data INT(11) )
BEGIN
DECLARE v_insertID INT DEFAULT -1;

SELECT ID INTO v_insertID  FROM table_a WHERE value = i_value;

IF -1 = v_insertID
THEN
   INSERT INTO table_a(value) VALUES(i_value);
   SELECT ID INTO v_insertID  FROM table_a WHERE value = i_value;
END IF;

IF -1 != v_insertID
THEN
   INSERT INTO table_b(ref_a, data) VALUES(v_insertID, i_data);
END IF;

END

We check if an entry with given value exists, if not we create it, we retrieve the create entry ID and then insert the data in the table_b (if the entry was really created) 我们检查是否存在具有给定值的条目,如果不是我们创建它,我们检索创建条目ID,然后将数据插入table_b(如果条目是真的创建的)

So you only have to call the routine from php for each entry. 所以你只需要从php为每个条目调用例程。 It will be far more optimized than doing all those test in php (which will require more than one mysql call) 它将比在php中进行所有那些测试(这将需要多个mysql调用)更加优化

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM