简体   繁体   English

计算后更新数百万行

[英]Updating millions of Row after Calculation

I am looking for advice on how should I do following:我正在寻求有关我应该如何执行以下操作的建议:

  1. I have a table in SQL server with about 3 -6 Million Records and 51 Columns.我在 SQL 服务器中有一个表,大约有 3 -6 百万条记录和 51 列。
  2. only one column needs to be updated after calculating a value from 45 columns data been taken in mathematical calculation.从数学计算中所取的 45 列数据中计算出一个值后,只需更新一列。
  3. I already have maths done through C#, and I am able to create Datatable out of it [with millions record yes].我已经通过 C# 完成了数学计算,并且我能够从中创建数据表 [有数百万条记录是]。

Now I want to update them into database with most efficient manner.现在我想以最有效的方式将它们更新到数据库中。 Options I know are我知道的选项是

  1. Run update query with every record, as I use loop on data reader to do math and create DataTable.对每条记录运行更新查询,因为我在数据读取器上使用循环来进行数学运算并创建 DataTable。
  2. Create A temporary table and use SQLBulkCopy to copy data and later use MERGE statement创建临时表并使用 SQLBulkCopy 复制数据,然后使用 MERGE 语句
  3. Though it is very HARD to do, but can try to make Function within SQL to do all math and just run simple update without any condition to update all in once.虽然这很难做到,但是可以尝试在 SQL 中使用 Function 来完成所有数学运算,并且只需运行简单的更新,无需任何条件即可一次性全部更新。

I am not sure which method is faster one or better one.我不确定哪种方法更快或更好。 Any idea?任何的想法?

EDIT : Why I am afraid of using Stored Procedure编辑:为什么我害怕使用存储过程

  1. First I have no idea how i wrote it, I am pretty new to do this.首先,我不知道我是怎么写的,我是新手。 Though maybe it is time to do it now.虽然现在也许是时候去做了。
  2. My Formula is Take one column, apply one forumla on them, along with additional constant value [which is also part of Column name], then take all 45 columns and apply another formula.我的公式是取一列,对它们应用一个论坛,以及额外的常量值 [这也是列名称的一部分],然后取所有 45 列并应用另一个公式。
  3. The resultant will be stored in 46th column.结果将存储在第 46 列中。

Thanks.谢谢。

如果您有一个字段包含来自数据库中其他字段的计算,最好将其设为计算字段或通过触发器维护它,以便在任何来源更改数据时,都会保留计算。

You can create a .net function which can be called directly from sql here is the link how to create one http://msdn.microsoft.com/en-us/library/w2kae45k%28v=vs.90%29.aspx .您可以创建一个可以直接从 sql 调用的 .net 函数,这里是如何创建一个http://msdn.microsoft.com/en-us/library/w2kae45k%28v=vs.90%29.aspx的链接。 After you created the function run the simple update statement创建函数后,运行简单的更新语句

你不能在 c# 中创建一个标量值函数,并将它作为计算列的一部分调用吗?

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

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