Windows 服务还是 SQL 作业?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/250518/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 11:32:40  来源:igfitidea点击:

Windows Service or SQL Job?

sqlwindowssql-server-2005windows-services

提问by Aaron Palmer

I have an archiving process that basically deletes archived records after a set number of days. Is it better to write a scheduled SQL job or a windows service to accomplish the deletion? The database is mssql2005.

我有一个归档过程,它基本上会在设定的天数后删除归档记录。写一个定时的SQL作业还是windows服务来完成删除比较好?数据库是mssql2005。

Update:

更新:

To speak to some of the answers below, this question is regarding an in house application and not a distributed product.

为了回答下面的一些答案,这个问题是关于内部应用程序而不是分布式产品。

采纳答案by Rune Grimstad

It depends on what you want to accomplish. Do you want to store the deleted archives somewhere? Log the changes? An SQL Job should perform better since it is run directly in the database, but it is easier to give a service acces to resources outside the database. So it depends on what you want to do,,,

这取决于你想完成什么。您想将已删除的档案存储在某处吗?记录更改?SQL 作业应该执行得更好,因为它直接在数据库中运行,但更容易让服务访问数据库外的资源。所以这取决于你想做什么,,,

回答by Jimoc

I would think a scheduled SQL job would be a safer solution since if the database is migrated to a new machine, someone doing the migration might forget that there is a windows service involved and forget to start/install it on the new server.

我认为预定的 SQL 作业将是一个更安全的解决方案,因为如果将数据库迁移到新机器,执行迁移的人可能会忘记涉及 Windows 服务而忘记在新服务器上启动/安装它。

回答by Joel Coehoorn

In the past we've had a number of SQL Jobs run. Recently, however, we've been moving to calling those processes from .Net code as a client application run from a windows schedule task, for two reasons:

过去,我们运行了许多 SQL 作业。然而,最近我们开始从 .Net 代码中调用这些进程,作为从 Windows 计划任务运行的客户端应用程序,有两个原因:

  1. It's easier to implement features like logging this way.
  2. We have other batch jobs that don't run in the database, and therefore must be in windows scheduled tasks. This way all the batch jobs of any type will be listed in one place.
  1. 以这种方式实现日志记录等功能更容易。
  2. 我们还有其他不在数据库中运行的批处理作业,因此必须在 Windows 计划任务中。这样,任何类型的所有批处理作业都将列在一个地方。

回答by Stephen Wrighton

Please note that regardless of how you do it, for this task you do not want a service. Services run all day, and will consume a bit of the server's ram all day.

请注意,无论您如何操作,对于此任务,您都不需要服务。服务整天运行,并且会消耗一点服务器的内存。

In this, you have a task you need to run, and run once a day, every day. As such, you'd either want a job in SQL Server or as Joel described an application (console or winforms) that was setup on a schedule to execute and then unload from the server's memory space.

在这里,您有一个需要运行的任务,并且每天运行一次。因此,您要么想要 SQL Server 中的作业,要么像 Joel 描述的那样,按计划设置一个应用程序(控制台或 winforms)来执行,然后从服务器的内存空间中卸载。

回答by Corey Trager

Is this for you/in house, or is this part of a product that you distribute.

这是给您/内部的,还是您分发的产品的一部分。

If in house, I'd say the SQL job. That's just another service too.

如果在内部,我会说 SQL 工作。那也只是另一种服务。

If it's part of a product that you distribute, I would consider how the installation and support will be.

如果它是您分发的产品的一部分,我会考虑安装和支持的方式。

回答by Mike L

To follow on Corey's point, if this is externally distributed, will you need to support SQL Express? If not, I'd go with the SQL job directly. Otherwise, you'll have to get more creative as SQL Express does not have the SQL Agent that comes with the full versions of SQL 2005 (as well as MSDE). Without the SQL Agent, you'll need another way to automatically fire the job. It could be a windows service, a scheduled task (calling a .NET app, powershell script, VBscript, etc.), or you could try to implement some trigger in SQL Server directly.

按照 Corey 的观点,如果这是外部分发的,您是否需要支持 SQL Express?如果没有,我会直接进行 SQL 工作。否则,您将不得不获得更多创意,因为 SQL Express 没有 SQL 2005(以及 MSDE)完整版本附带的 SQL 代理。如果没有 SQL 代理,您将需要另一种方法来自动触发作业。它可以是 Windows 服务、计划任务(调用 .NET 应用程序、powershell 脚本、VBscript 等),或者您可以尝试直接在 SQL Server 中实现一些触发器。