简体   繁体   English

从一张表中读取并插入到另一张中-一次一行

[英]Read from one table and insert into another - one row at a time

I am dealing with a huge database with millions of rows. 我正在处理具有数百万行的巨大数据库。 I would like to run an SQL statement through C#, which selects 1.2 million rows from one database, and inserts them into another after parsing and modifying some data. 我想通过C#运行一条SQL语句,该语句从一个数据库中选择120万行,然后在解析和修改某些数据后将它们插入到另一个数据库中。

I originally wanted to do so by first running the select statement and parsing the data by iterating through the MySqlDataReader object which contains the data. 我最初想通过首先运行select语句并通过遍历包含数据的MySqlDataReader对象来解析数据来做到这一点。 This would be a memory overhead, so I have decided to select one row, parse it and insert into the other database, and then move onto the next row. 这将占用内存,因此我决定选择一行,将其解析并插入另一个数据库,然后移至下一行。

How can this be done? 如何才能做到这一点? I have tried the SELECT....INTO syntax for a MySQL query, however this still seems to select all the data, and then inserts it after. 我已经尝试了MySQL查询的SELECT....INTO语法,但是这似乎仍然选择了所有数据,然后将其插入。

Use SqlBulkCopy Class to move data from one source to other 使用SqlBulkCopy类将数据从一个源移动到另一源

http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlbulkcopy%28v=vs.110%29.aspx http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlbulkcopy%28v=vs.110%29.aspx

I am not sure if you are able to add a new column to the existing table. 我不确定您是否能够向现有表中添加新列。 If you are able to add a new column, you can use the new column as a flag. 如果能够添加新列,则可以将新列用作标志。 It could be "TRANSFERED(boolean)". 可能是“ TRANSFERED(boolean)”。

You will select one row at a time with the condition TRANSFERED=FALSE and do the process. 您将一次选择条件为TRANSFERED = FALSE的一行,然后执行该过程。 After that row is processed, you should update as TRANSFERED=TRUE. 处理完该行后,应更新为TRANSFERED = TRUE。

Or, you must have a uniqe id column in your existing table. 或者,您必须在现有表中具有唯一ID列。 Create a temp table which will store the id of processed rows, that way you will know which rows are processed or not 创建一个临时表,该表将存储已处理行的ID,这样您将知道已处理哪些行

I am not quite sure what is your error. 我不太确定您的错误是什么。 For your case, I suggest you should use 'select top 1000 ' to get the data because insert row one by one is really slow. 对于您的情况,我建议您使用“ select top 1000”来获取数据,因为一开始插入一行确实很慢。 After that, you can use 'insert into query', it should be noted that sqlbulkcopy is just for sql server, I suggest you use the stringbuilder to make the sql query for if you use string, it will has a big overhead to concat the string. 之后,您可以使用“插入查询”,应该注意的是sqlbulkcopy仅用于sql服务器,建议您使用stringbuilder进行sql查询,如果使用字符串,则连接起来会产生很大的开销串。

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

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