我遇到PowerShell中的脚本在执行MySQL SELECT查询后挂起的问题,需要执行的时间超过10分钟。
$query = "SELECT S.DESCRIPTION, date_format(N.TIMESTAMP, '%H'), count(*)
FROM NOTIFICATIONS N, SENDING_TYPE S
WHERE N.ID_SENDING_TYPE = S.ID_SENDING_TYPE
AND N.TIMESTAMP > date_format ('"+$fecha+"', '%Y%m%d')
GROUP BY S.DESCRIPTION, date_format(N.TIMESTAMP, '%H')"
示例 - $fecha="20170630"
我使用我的函数执行查询(这是PowerShell挂起的地方)
$resultado = Get-QueryMysql -Servidor $MySQLHost -Port $port -UserName $MySQLAdminUserName -Password $MySQLAdminPassword -DataBase $MySQLDatabase -Query $query
这里是函数Get-QueryMysql
:
function Get-QueryMysql {
[CmdletBinding()]
[Alias()]
[OutputType([int])]
Param(
# Servidor Mysql
[Parameter(Mandatory=$true)]
[String]$Servidor,
# Puerto Oracle
[Parameter(Mandatory=$true)]
[String]$Port,
# ServiceName
[Parameter(Mandatory=$true)]
[String]$DataBase,
# Usuario
[Parameter(Mandatory=$true)]
[String]$UserName,
# Password
[Parameter(Mandatory=$true)]
[String]$Password,
# Query
[Parameter(Mandatory=$true)]
[String]$Query,
# If Allow zero datetime
[Parameter(Mandatory=$false)]
[switch]$AllowZeroDateTime,
# Reintentos
# Query
[Parameter(Mandatory=$false)]
[int]$Reintentos=10,
[switch]$AntiguoMysql
)
Begin {
# Load the ODP assembly
if ($AntiguoMysql.IsPresent) {
Add-Type -Path C:\TRABAJO\REPORTES\modules\MySQL\mysql.data.dll -IgnoreWarnings
} else {
$null = [System.Reflection.Assembly]::LoadWithPartialName("MySql.Data")
}
}
Process {
# Cadena de conexion
$ConnectionString = "server=" + $Servidor + ";port=$port;uid=" + $UserName + ";pwd=" + $Password + ";database=" + $DataBase
if ($AllowZeroDateTime.IsPresent) {
$ConnectionString = "server=" + $Servidor + ";port=$port;uid=" + $UserName + ";pwd=" + $Password + ";database="+$DataBase+";Allow Zero Datetime=True"
}
$cont = 0
for ($i=0; $i-lt $Reintentos; $i++) {
$MysqlConnection = New-Object MySql.Data.MySqlClient.MySqlConnection
$MysqlConnection.ConnectionString = $ConnectionString
$MysqlConnection.Open()
$Command = New-Object MySql.Data.MySqlClient.MySqlCommand($Query, $MysqlConnection)
$DataAdapter = New-Object MySql.Data.MySqlClient.MySqlDataAdapter($Command)
$DataSet = New-Object System.Data.DataSet
$RecordCount = $dataAdapter.Fill($DataSet, "data")
if ($DataSet) {
break;
}
}
if (!$dataSet) {
Write-Host "ERROR: Conectando a la BBDD"
return $null
}
# Datos obtenidos
return $DataSet.Tables[0]
}
End {
# Cerrando Conexiones
if ($MysqlConnection) {
$MysqlConnection.Close()
$MysqlConnection.Dispose()
}
}
}
查询是按时间戳字段过滤的SELECT。我试图改变表的结构,但我在生产环境中的要求被拒绝了。所以我需要这样做,我的脚本可以工作,我只有很长时间的查询问题。
我尝试使用script-block命令,但它不起作用。
当进程正在执行时,cpu是0.5%和20 mb的ram(没有增量)。
当我从DBMS(Navicat)执行查询时,需要10分钟,在某些情况下会超过10分钟。我用SHOW PROCESSLIST
检查了MySQL数据库,几分钟后脚本执行的查询成功完成,但PowerShell脚本挂起。
执行以下行后脚本挂起:
$RecordCount = $dataAdapter.Fill($dataSet, "data")
加载另一个连接器后:
[void][system.reflection.Assembly]::LoadFrom("C:\modules\MySQL\6.9.8\mysql.data.dll")
脚本没有挂起,但现在它发生了错误异常:
Exception "Fill" arguments "2": "Fatal error encountered during command execution."
+ $RecordCount = $dataAdapter.Fill($dataSet, "data")
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : MySqlException
在数据库中查看超时变量,设置为8小时:
interactive_timeout=28800
wait_timeout=28800
SHOW PROCESSLIST
命令显示连接处于Sending Data
状态。
错误异常仅在查询超过1分钟或更长时间内发生..
答案 0 :(得分:0)