简体   繁体   English

SQL Server随机超时问题

[英]SQL Server random timeout issue

Hello SO , 你好

I am currently attempting to run a stored procedure that processes apx ~50k records. 我目前正在尝试运行一个处理apx〜50k记录的存储过程。 It sorts the records into different tables, deletes some records, etc. 它将记录分类到不同的表中,删除一些记录,等等。

However, a relatively long prepared statement will always result in the following error(s): 但是,相对较长的准备好的语句将始终导致以下错误:

> [2016-12-08 19:28:24] local.INFO: PDOException: SQLSTATE[HY000]:
> General error: 20003 Adaptive Server connection timed out [20003]
> (severity 6) [(null)] in
> /vendor/laravel/framework/src/Illuminate/Database/Connection.php:479
> Stack trace:
> #0 /vendor/laravel/framework/src/Illuminate/Database/Connection.php(479):
> PDOStatement->execute()
> #1 /vendor/laravel/framework/src/Illuminate/Database/Connection.php(762):
> Illuminate\Database\Connection->Illuminate\Database\{closure}(Object(Illuminate\Database\SqlServerConnection),
> 'EXEC dbo.cleanD...', Array)
> #2 /vendor/laravel/framework/src/Illuminate/Database/Connection.php(725):
> Illuminate\Database\Connection->runQueryCallback('EXEC dbo.cleanD...',
> Array, Object(Closure))
> #3 /vendor/laravel/framework/src/Illuminate/Database/Connection.php(480):
> Illuminate\Database\Connection->run('EXEC dbo.cleanD...', Array,
> Object(Closure))
> #4 /vendor/laravel/framework/src/Illuminate/Database/DatabaseManager.php(317):
> Illuminate\Database\Connection->statement('EXEC dbo.cleanD...')
> #5 /vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php(237):
> Illuminate\Database\DatabaseManager->__call('statement', Array)

What gets me is that, through the use of laravel chunk, I am able to insert ~50k records without incident: 我得到的是,通过使用laravel块,我可以插入约50k条记录而不会发生任何事件:

$data = $data->chunk(500);

foreach ($data as $rows) {
    Log::info("INSERT START: " . date('l jS \of F Y h:i:s A'));
    DB::table("the.db")->insert($rows->toArray());
}

But as soon as the stored procedure runs (which could take ~2-4 minutes) 但是,存储过程一旦运行(可能需要2-4分钟)

I get the above error. 我收到上述错误。 I've tried a ton of different solutions, including breaking the stored procedure into littler ones, but still I get the error. 我尝试了很多不同的解决方案,包括将存储过程分解成更小的解决方案,但是仍然出现错误。

Always the same error: 总是相同的错误:

General error: 20003 Adaptive Server connection timed out [20003] (severity 6) [(null)] in /vendor/laravel/framework/src/Illuminate/Database/Connection.php:479

I have a sneaking suspicion that somewhere (in laravel? php.ini?) that I might have to up a timeout limit for the database 我暗中怀疑在某个地方(在laravel?php.ini?中)我可能必须为数据库设置超时限制

Any thing helps, thanks in advance! 任何事情都会有所帮助,在此先感谢!

I can't verify this works but I seen in another question you use the option PDO::ATTR_TIMEOUT to forcefully set a higher timeout on it. 我无法验证此方法是否有效,但在另一个问题中,我看到您使用选项PDO::ATTR_TIMEOUT强制对其设置更高的超时时间。 I'm not sure what the default would be. 我不确定默认值是多少。

'sqlsrv' => [
    'driver'   => 'sqlsrv',
    'host'     => env('DB_HOST', 'localhost'),
    'database' => env('DB_DATABASE', 'forge'),
    'username' => env('DB_USERNAME', 'forge'),
    'password' => env('DB_PASSWORD', ''),
    'charset'  => 'utf8',
    'prefix'   => '',
    'options' => [
        PDO::ATTR_TIMEOUT => 240  // 240 seconds.
    ]
],

You may also want to take a look at Can I set a query timeout when using Zend_Db_Adapter_Pdo_Mssql? 您可能还想看看使用Zend_Db_Adapter_Pdo_Mssql时是否可以设置查询超时? .

In addition to the solution that @user3158900 offered, I know I mentioned this in the comments but just to reiterate them here (since comments may be deleted), you might want to consider using a SQL Server Job if the query can't be broken down more. 除了@ user3158900提供的解决方案之外,我知道我在注释中提到了此问题,但只是在这里重申(因为注释可能会被删除),如果查询无法中断,您可能要考虑使用SQL Server Job下降更多。 Jobs can do all kinds of things (integration packages, command line applications, scripts, etc.) in addition to running SQL scripts, so they're great for automating routine administrative-type tasks. 作业除了可以运行SQL脚本外,还可以执行各种操作(集成包,命令行应用程序,脚本等),因此,它们对于自动化常规管理型任务非常有用。

If you're moving away from SQL Server, Oracle has a job scheduler too, but I'm not as familiar with it. 如果您要离开SQL Server,Oracle也有一个作业调度程序 ,但是我对它并不熟悉。

Since you indicated that you're planning to move the logic out of SQL Server at some point, once you have time one possibility (if you happen to be using a Windows server, which I highly recommend since I own Microsoft stock :)) is to write a console application and use the Windows Task Scheduler to run it on your chosen schedule. 由于您已表示您打算在某个时候将逻辑移出SQL Server,因此一旦有时间,一种可能性(如果您恰好使用Windows服务器,我强烈建议您使用它,因为我拥有Microsoft库存:))编写控制台应用程序,并使用Windows Task Scheduler在您选择的时间表上运行它。 You can even have it run in the background without opening a console window. 您甚至可以在不打开控制台窗口的情况下在后台运行它。

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

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