简体   繁体   English

MySQL从一个表到另一个PHP传输8000+行

[英]mySQL transfer 8000+ rows from one table to another PHP

I am working on a script in which I want to transfer 8000+ rows from one table to another table in another database with selected columns. 我正在处理一个脚本,该脚本中我想将8000+行从一个表转移到具有选定列的另一个数据库中的另一个表。 By far I have thought to fetch the selected columns of the entire source table using PDOStatement::fetchAll() as a $result array and then prepare a mySQL insert statement using PDO::prepare() method as 到目前为止,我已经考虑过使用PDOStatement::fetchAll()作为$result数组来获取整个源表的选定列,然后使用PDO::prepare()方法准备mySQL插入语句,如下所示:

INSERT INTO targetTable (targetColumn1,targetColumn2...) VALUES 
(sourceRow1ForColumn1,sourceRow1ForColumn2,..),
(sourceRow2ForColumn1,sourceRow2ForColumn2,..),
(sourceRow3ForColumn1,sourceRow3ForColumn2,...),...

But I think that this is not an efficient way and there should be an optimal way to do what I want to do. 但是我认为这不是一种有效的方法,应该有一种最佳方法来完成我想做的事情。 Is there any better way I can do what I want to do? 有什么更好的办法可以做我想做的事吗?

If it's going to be done one time, you do not need to care of the performance. 如果要一次完成,则无需关心性能。

put set_time_limit(0); 把set_time_limit(0); at the beginning of your PHP file. 在您的PHP文件的开头。

Then, connect to your first DB. 然后,连接到您的第一个数据库。 read the data from the source table and store in an array. 从源表中读取数据并存储在数组中。

Now, disconnect and connect to the new DB and insert the array data to the target table 现在,断开连接并连接到新数据库,然后将阵列数据插入目标表

And for caring the duplicate keys, you can use INSERT OR UPDATE ON DUPLICATE in your query. 为了照顾重复键,您可以在查询中使用INSERT或UPDATE ON DUPLICATE。

If your records are going to be huge, you might have problem with the memory. 如果您的记录非常庞大,则可能是内存有问题。 So, it's better to get the count before storing into the array. 因此,最好在存储到数组之前获取计数。 Then, do the process in a loop and at the end of each loop, free up the array by unset(). 然后,循环执行该过程,并在每个循环结束时通过unset()释放数组。 So, you split the records and keep your memory usage in a proper way. 因此,您可以拆分记录并以适当的方式保持内存使用。

The optimal way to do this would be to use an INSERT ... SELECT statement. 最佳方法是使用INSERT ... SELECT语句。

INSERT INTO targetTable (targetColumn1,targetColumn2)
SELECT expr1, expr2 FROM one_table

expr1 and expr2 represent expressions that return the values that you want to insert into targetColumn1 and targetColumn2 columns, respectively. expr1expr2表示分别返回要插入到targetColumn1targetColumn2列中的值的表达式。

The expressions could be as simple reference to a colum name in the source table. 这些表达式可以作为对源表中列名称的简单引用。


If the tables are in different databases (schemas) on the same MySQL instance, you can qualify the table name with the name of the database. 如果表位于同一MySQL实例上的不同数据库(方案)中,则可以使用数据库名称来限定表名。 (An unqualified table name refers to a table in the current database (ie USE mydatabase.) (不合格的表名是指当前数据库中的表(即USE mydatabase。)

If your current database is the target database, qualify the name table in the FROM clause 如果当前数据库是目标数据库,请在FROM子句中限定名称表

INSERT INTO targetTable (targetColumn1,targetColumn2)
SELECT expr1, expr2 FROM anotherdb.one_table
                         ^^^^^^^^^^

If the current database contains the source table, qualify the name of the targetTable. 如果当前数据库包含源表,请限定targetTable的名称。

Or just qualify both table names, and it doesn't matter what database is current. 或仅限定两个表名,而当前的数据库无关紧要。


Just connect to MySQL as a user that has sufficient privileges on each table. 只需以对每个表具有足够特权的用户身份连接到MySQL。

GRANT SELECT ON anotherdb.one_table TO myuser@'%';
GRANT INSERT ON targetdb.targetTable TO myuser@'%';

If the insert attempts to insert a row that violates a unique constraint in the targetTable, the same thing would happen with an INSERT ... SELECT as would with the INSERT ... VALUES statement... the statement will fail. 如果插入尝试插入违反targetTable中唯一约束的行,则INSERT ... SELECT和INSERT ... VALUES语句都会发生相同的情况,该语句将失败。

If you want to disregard errors about duplicates, and just skip over those rows, then use an INSERT IGNORE ... SELECT atatement. 如果您想忽略有关重复项的错误,而只是跳过这些行,请使用INSERT IGNORE ... SELECT属性。 (The IGNORE keywords causes MySQL not to skip over the row, rather than throwing an error when a unique key constraint is violated. (IGNORE关键字使MySQL不会跳过该行,而不是在违反唯一键约束时抛出错误。

Or, you could write the SELECT statement to exclude rows that already exist in the target table. 或者,您可以编写SELECT语句以排除目标表中已经存在的行。

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

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