PowerShell中的并行T-SQL执行

问题描述 投票:0回答:1

有人可以帮忙解决这个问题吗?我指的是互联网上一个并行执行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',因为它是一个空字符串。

sql-server powershell powershell-v3.0 powershell-workflow
1个回答
0
投票

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

  • 移动了第一个{自评论之后!
  • 删除$ sqlcmds =“”
  • 更改了在$ sqlcmds数组中聚合SQL的方式
  • 删除了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 'prod-sqlcms' -Database master -Query $sqlcmds;
    $dt_end = Get-Date; #end time
    $dt_end - $dt_start; # find execution duration
© www.soinside.com 2019 - 2024. All rights reserved.