【问题标题】:Parallel T-SQL execution in PowerShellPowerShell 中的并行 T-SQL 执行
【发布时间】:2019-01-31 20:54:41
【问题描述】:

谁能帮助解决这个问题?我指的是互联网上的一个示例,用于并行执行 T-SQL 语句。

https://www.mssqltips.com/sqlservertip/3539/complete-common-sql-server-database-administration-tasks-in-parallel-with-powershell-v3-workflow/

我希望能够同时在同一个实例上执行相同的 T-SQL,以进行关于锁定的概念验证工作。为了做到这一点,我调整了脚本,以便我可以通过更改

来执行任意数量的迭代
while ($counter -le 5)

这是完整的脚本。基本上,主要语句可以是您想要的任何 T-SQL,这将填充 $sqlcmds 以使该语句通过尽可能多的迭代。

Import-Module sqlps -DisableNameChecking;
Set-Location c:
# create a workflow to run multiple sql in parallel
workflow Run-PSQL #PSQL means Parallel SQL {
    Param(
        [Parameter(Mandatory=$true)]
        [string]$ServerInstance,

        [Parameter(Mandatory=$false)]
        [string]$Database,

        [Parameter(Mandatory=$true)]
        [string[]]$Query # a string array to hold t-sqls
    )

    foreach -parallel ($q in $query)  {
        Invoke-Sqlcmd -ServerInstance $ServerInstance -Database $Database -Query $q -QueryTimeout 60000;
    }
} # Run-PSQL

# prepare a bunch of sql commands in a string arrary

#####new bit to make it dynamic sql multiple times
[string[]]$sqlcmds
$sqlcmds = ""
$counter = 0
do {
    "Starting Loop $Counter"

    $PrimaryStatement = '"SELECT TOP 1 * FROM sys.objects"'
    if ($counter -eq 5) {
        $sqlcmds = $sqlcmds + "$PrimaryStatement"
        Write-Host "this is what sqlcmds is $sqlcmds loop 5"
    } else {
        $sqlcmds = $sqlcmds + "$PrimaryStatement,``"
        Write-Host "this is what sqlcmds is now $sqlcmds"
    }

    $counter++
} while ($counter -le 5)

# now we can run the workflow and measure its execution duration
$dt_start = Get-Date; #start time
Run-PSQL -Server &&&&&&& -Database master -Query $sqlcmds;
$dt_end = Get-Date; #end time
$dt_end - $dt_start; # find execution duration

执行此操作时,我收到以下消息:

Run-PSQL:无法将参数绑定到参数“Query”,因为它是一个空字符串。

【问题讨论】:

  • 我没有工作流方面的经验,但显而易见的问题是:$sqlcmds 是否有价值?弹性语法是否也适用于工作流?在这种情况下,-Server 是否会绑定到 $ServerInstance,尽管它只是一个前缀?
  • “关于锁定的概念验证工作” - 如果您打开 SSMS 并开始交易,这有点简单......
  • 我设法让它工作。感谢那些回复的人。我避免了 powershell 中的工作流程并行并完成了工作。本质上我不能使用 SSMS 的原因是因为我需要创建 100 多个触发 sql 语句的会话

标签: sql-server powershell powershell-3.0 powershell-workflow


【解决方案1】:

我必须做一些小的更正,下面是似乎可以按预期工作的最终代码

  • 移动了第一个 { 因为它得到了评论!
  • 删除了 $sqlcmds = ""
  • 更改了 SQL 在数组 $sqlcmds 中的聚合方式
  • 删除了内部的 if/else,因为它似乎没有服务于 目的
  • 更改了打印的文本

    Import-Module sqlps -DisableNameChecking;
    Set-Location c:
    # create a workflow to run multiple sql in parallel
    workflow Run-PSQL #PSQL means Parallel SQL 
    {
        Param(
            [Parameter(Mandatory=$true)]
            [string]$ServerInstance,

            [Parameter(Mandatory=$false)]
            [string]$Database,

            [Parameter(Mandatory=$true)]
            [string[]]$Query #a string array to hold t-sqls
        )

        foreach -parallel ($q in $query)  {
            Invoke-Sqlcmd -ServerInstance $ServerInstance -Database $Database -Query $q -QueryTimeout 60000;
        }
    } # Run-PSQL

    # prepare a bunch of sql commands in a string arrary

    ##### new bit to make it dynamic sql multiple times
    [string[]]$sqlcmds = @()

    $counter = 0
    do {
        "Starting Loop $Counter"

        $PrimaryStatement = 'SELECT TOP 1 * FROM sys.objects'

        $sqlcmds += "$PrimaryStatement"
        Write-Host ("this is what sqlcmds has [$($sqlcmds.Count)] statements at loop counter [$Counter]")

        $counter++
    } while ($counter -le 5)

    # now we can run the workflow and measure its execution duration
    $dt_start = Get-Date; #start time
    Run-PSQL -Server 'myserver\myinstance' -Database master -Query $sqlcmds;
    $dt_end = Get-Date; #end time
    $dt_end - $dt_start; # find execution duration

【讨论】:

    猜你喜欢
    • 2021-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-18
    • 2020-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多