简体   繁体   中英

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.
  2. only one column needs to be updated after calculating a value from 45 columns data been taken in mathematical calculation.
  3. I already have maths done through C#, and I am able to create Datatable out of it [with millions record yes].

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.
  2. Create A temporary table and use SQLBulkCopy to copy data and later use MERGE statement
  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.

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.
  3. The resultant will be stored in 46th column.

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 . After you created the function run the simple update statement

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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