PowerShell脚本的执行时间在第一次和第二次运行之间差别很大

时间:2017-04-01 09:38:05

标签: sql oracle powershell

我在执行一个小的PowerShell脚本时遇到了一些非常奇怪的事情,该脚本获取了本地Oralce DB的一些信息并将结果放入对象中。这个scipt的第一次执行大约需要30秒,连续执行只需2秒!

有人对此有解释吗? (它是在Oracle-DB中缓存吗?/任何方式来找出第二次运行时导致速度突发的原因?)

function Get-SQLConnection 
{
    Add-Type -Path $(Join-Path $PSScriptRoot "libs/Oracle.ManagedDataAccess.dll")
    $connectionString = 'User Id=FOO;Password=BAR;Data Source=TNSCONNID'
    return New-Object Oracle.ManagedDataAccess.Client.OracleConnection($connectionString)
}

function Get-SQLIssueQuery($version) 
{
    $version = $version.ToString()
    return "SELECT ...
            FROM ... WHERE $version ..."
}

function Get-IssuesForVersion($dbconn, $version) 
{
    Write-Verbose "Get-IssuesForVersion $($version.ToString())..."
    $issues = @{}
    $command=$dbconn.CreateCommand()
    $command.CommandText=$(Get-SQLIssueQuery $version)
    $reader=$command.ExecuteReader()

    while ($reader.Read()) {
        $text = $reader[0]
        [int]$key = $reader[2].Split("-")[1]
        $type = $reader[6]
        ...
        $issues[$key] = [Issue]::new($key, $type, ...)
    }
    Write-KSDetail "Get-IssuesForVersion '$($version.ToString())' returned $($issues.Count) issues..."
    return $issues
}

$timestart = $(Get-Date)
$conn = Get-SQLConnection
$conn.open()
Get-IssuesForVersion $conn "Version XYZ"
$conn.close()
$timeend = $(Get-Date) 
$duration = $timeend - $timestart # first run: approx 30 seconds

$timestart = $(Get-Date)
$conn = Get-SQLConnection
$conn.open()
Get-IssuesForVersion $conn "Version XYZ"
$conn.close()
$timeend = $(Get-Date) 
$duration = $timeend - $timestart # second run: approx 2 seconds

提前感谢!

0 个答案:

没有答案