有没有办法在sql server中编写异步sql查询

时间:2021-01-09 23:39:00

I'm working on a sproc that needs to run several independent select statements. So let's say I have 3 select statements like this:

我正在研究需要运行多个独立select语句的sproc。所以我要说我有3个这样的选择语句:

select * from x into #x
select * from y into #y
select * from z into #z

Let's say each select statement takes 2s to complete. If I run the sproc as shown above then the total query time would be 6s. However, if I was able to run the queries asyncronously/simultaneously then I could exec the sproc in 2s total. Is there a way to do something like this in T-SQL?:

假设每个select语句需要2s才能完成。如果我如上所示运行sproc,那么总查询时间将是6s。但是,如果我能够异步/同时运行查询,那么我可以在2s内执行sproc。有没有办法在T-SQL中做这样的事情?:

select * from x into #x async
select * from y into #y async
select * from z into #z async

2 个解决方案

#1


0  

As a transactional database system, SQL Server does not have a concept of performing operations asynchronously. If you need to do such a thing you should perform that logic in your application layer.

作为事务数据库系统,SQL Server没有异步执行操作的概念。如果您需要执行此类操作,则应在应用程序层中执行该逻辑。

#2


0  

You can run asynchronously by creating multiple jobs though it is workaround way,

您可以通过创建多个作业来异步运行,尽管它是解决方法,

See Below code

见下面的代码

EXEC msdb.dbo.sp_add_job  
    @job_name = N'Job 1' ;

EXEC msdb.dbo.sp_add_jobstep  
    @job_name = N'Job 1',  
    @step_name = N'Load tablex',  
    @subsystem = N'TSQL',  
    @command = N'select * into tabley from y'



EXEC msdb.dbo.sp_add_job  
    @job_name = N'Job 2' ;

EXEC msdb.dbo.sp_add_jobstep  
    @job_name = N'Job 2',  
    @step_name = N'Load tablex',  
    @subsystem = N'TSQL',  
    @command = N'select * into tabley from y'

Instantiate both these jobs or schedule so that it can run asynchronously

实例化这些作业或计划,以便它可以异步运行

#1


0  

As a transactional database system, SQL Server does not have a concept of performing operations asynchronously. If you need to do such a thing you should perform that logic in your application layer.

作为事务数据库系统,SQL Server没有异步执行操作的概念。如果您需要执行此类操作,则应在应用程序层中执行该逻辑。

#2


0  

You can run asynchronously by creating multiple jobs though it is workaround way,

您可以通过创建多个作业来异步运行,尽管它是解决方法,

See Below code

见下面的代码

EXEC msdb.dbo.sp_add_job  
    @job_name = N'Job 1' ;

EXEC msdb.dbo.sp_add_jobstep  
    @job_name = N'Job 1',  
    @step_name = N'Load tablex',  
    @subsystem = N'TSQL',  
    @command = N'select * into tabley from y'



EXEC msdb.dbo.sp_add_job  
    @job_name = N'Job 2' ;

EXEC msdb.dbo.sp_add_jobstep  
    @job_name = N'Job 2',  
    @step_name = N'Load tablex',  
    @subsystem = N'TSQL',  
    @command = N'select * into tabley from y'

Instantiate both these jobs or schedule so that it can run asynchronously

实例化这些作业或计划,以便它可以异步运行