简体   繁体   English

Powershell SQL Server插入 - 最佳实践

[英]Powershell SQL Server Insert - Best Practice

I have a script that iterates through a few thousand files in a directory on a daily basis and would like to update a SQL Server table with details of each file, as they are processed within the foreach loop. 我有一个脚本,每天遍历一个目录中的几千个文件,并希望更新一个SQL Server表,其中包含每个文件的详细信息,因为它们是在foreach循环中处理的。

I have this working already using the following within the foreach loop: 我已经在foreach循环中使用了以下工作:

Invoke-Sqlcmd -Query "INSERT INTO $dbTable (name, date, worknum, identifier) VALUES ('$name','$date','$worknum','$identifier')" 
              -ServerInstance $dbHost -Database $dbName -ErrorAction Stop

Although this works fine, I'd like to know if there would be any benefit to changing this method to establishing a SQL Server connection before the processing of the files starts and closing it at the end of the script? 虽然这很好用,但我想知道在文件处理开始之前将这种方法更改为建立SQL Server连接是否有任何好处,并在脚本结束时关闭它? Something like this.. 像这样的东西......

$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Server=$dbHost;Database=$dbName;Integrated Security=True;"

<foreach loop>

$SqlConnection.Close()

I'm not concerned with the speed that the script runs as its already pretty fast, just more with regard to not affecting DB performance. 我不关心脚本运行的速度,因为它已经非常快,更多的是关于不影响DB性能。

As stated in the comments, you will need to test against your instance configuration and existing workload to determine if a solution is performant or not. 如评论中所述,您需要针对实例配置和现有工作负载进行测试,以确定解决方案是否具有高性能。

I had a similar experience with a PowerShell "app" that took a list of account identifiers and then INSERT them into a table for us to further process. 我有一个类似于PowerShell“app”的经验,它获取了一个帐户标识符列表,然后将它们插入到表中供我们进一步处理。 The app was iterating over each ID and doing an INSERT for each ID originally. 该应用程序正在迭代每个ID并最初为每个ID执行INSERT。 This was OK for most users, but occasionally someone would put 100k+ ids in and the performance on the app was horrid! 这对大多数用户来说都没问题,但偶尔有人会放入100k + ID并且应用程序的性能很可怕! (But the SQL server kept performing as expected) Using SqlBulkCopy speed that process up immensely for the client side with no discernible impact on the SQL server as well. (但SQL服务器保持按预期执行)使用SqlBulkCopy速度,对客户端进行极大的处理,对SQL服务器也没有明显的影响。 (Only the folks with lots of records got the benefit though. There was no real change from <100 records though. ) (只有拥有大量记录的人才能获得好处。尽管如此,<100条记录没有真正的变化。)

The Write-DataTable and Out-DataTable are handy functions to have to make this easier. Write-DataTableOut-DataTable是方便的功能,必须使这更容易。

My feelings out of the way, best practice.... 我的感情,最佳实践....

Eugene Philipov has a good article on tests that they did on data load method performance between single value inserts, multi value inserts, and BulkCopy. Eugene Philipov有一篇关于测试的文章 ,他们在单值插入,多值插入和BulkCopy之间的数据加载方法性能方面做了很多。 They found that the number of columns you are inserting into has aa large affect on the operation's speed. 他们发现插入的列数对操作速度有很大影响。 The more columns, the less of a benefit you get from having multiple values in your insert or using bulk copy. 列越多,您在插入中使用多个值或使用批量复制时获得的好处就越少。 However, using a single insert per record was always slower (by execution time). 但是,每个记录使用一个插入总是较慢(按执行时间)。

Faster execution == less chance you will block/consume resources that are needed for your other workflows. 更快的执行==您将阻止/消耗其他工作流所需资源的机会减少。

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

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