如何定期截断 Microsoft Azure 上的 SQL 数据库
How to truncate SQL database on Microsoft Azure periodically
我在 Microsoft Azure 上有一个 SQL 数据库 运行。为了防止它变得太大,我必须定期(例如,一两天)截断它。
truncate table xxx
是我需要执行的SQL。
那么实现这一目标的最简单方法是什么?除非必须这样做,否则我宁愿不编写任何 C# 代码。我可以使用连续 运行 截断 SQL 语句的 Web 作业吗?或者我可以使用 Azure 上 SQL 数据库的内置功能来实现这一点吗?谢谢!
SQL Azure 还没有任何类型的 SQL 代理功能,因此您必须创建一个 Web 作业(或一些 JavaScript 来执行 SQL 你需要执行),然后使用 Azure Scheduler 来安排作业。
您也可以使用涉及 Powershell 的 Azure 自动化来做同样的事情。
您可以创建一个弹性数据库池并将您的数据库包含在 pool.once 中,您已完成此操作,您可以 运行 对池中的所有数据库或单个数据库执行一组任务或查询..
更多信息请点击此处..
https://azure.microsoft.com/en-in/documentation/articles/sql-database-elastic-jobs-overview/
实际上,您可以使用 Azure 函数("TimeTrigger" 类型)定期清除您的表,这里是一个代码示例,可在 C# TimeTrigger Azure 函数中使用以连接到您的 Azure Sql 数据库并执行 "delete" sql 查询:
#r "System.Data"
using System;
using System.Data.SqlClient;
public static async Task Run(TimerInfo myTimer, TraceWriter log)
{
string userName = "*******";
string passWord = "********";
var connectionString = $"Server=tcp:work-on- sqlazure.database.windows.net,1433;Data Source=work-on-sqlazure.database.windows.net;Initial Catalog=VideoStore;Persist Security Info=False;User ID={userName};Password={passWord};Pooling=False;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;";
using(SqlConnection cns = new SqlConnection(connectionString))
{
cns.Open();
var truncateUserTable = "DELETE FROM Video";
using(SqlCommand cmd = new SqlCommand(truncateUserTable, cns))
{
int rowsDeleted = await cmd.ExecuteNonQueryAsync();
}
}
}
然后您可以使用 cron 表达式从 Azure Azure 函数 "integrate" space 配置计时器逻辑。
我在 Microsoft Azure 上有一个 SQL 数据库 运行。为了防止它变得太大,我必须定期(例如,一两天)截断它。
truncate table xxx
是我需要执行的SQL。
那么实现这一目标的最简单方法是什么?除非必须这样做,否则我宁愿不编写任何 C# 代码。我可以使用连续 运行 截断 SQL 语句的 Web 作业吗?或者我可以使用 Azure 上 SQL 数据库的内置功能来实现这一点吗?谢谢!
SQL Azure 还没有任何类型的 SQL 代理功能,因此您必须创建一个 Web 作业(或一些 JavaScript 来执行 SQL 你需要执行),然后使用 Azure Scheduler 来安排作业。
您也可以使用涉及 Powershell 的 Azure 自动化来做同样的事情。
您可以创建一个弹性数据库池并将您的数据库包含在 pool.once 中,您已完成此操作,您可以 运行 对池中的所有数据库或单个数据库执行一组任务或查询..
更多信息请点击此处..
https://azure.microsoft.com/en-in/documentation/articles/sql-database-elastic-jobs-overview/
实际上,您可以使用 Azure 函数("TimeTrigger" 类型)定期清除您的表,这里是一个代码示例,可在 C# TimeTrigger Azure 函数中使用以连接到您的 Azure Sql 数据库并执行 "delete" sql 查询:
#r "System.Data"
using System;
using System.Data.SqlClient;
public static async Task Run(TimerInfo myTimer, TraceWriter log)
{
string userName = "*******";
string passWord = "********";
var connectionString = $"Server=tcp:work-on- sqlazure.database.windows.net,1433;Data Source=work-on-sqlazure.database.windows.net;Initial Catalog=VideoStore;Persist Security Info=False;User ID={userName};Password={passWord};Pooling=False;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;";
using(SqlConnection cns = new SqlConnection(connectionString))
{
cns.Open();
var truncateUserTable = "DELETE FROM Video";
using(SqlCommand cmd = new SqlCommand(truncateUserTable, cns))
{
int rowsDeleted = await cmd.ExecuteNonQueryAsync();
}
}
}
然后您可以使用 cron 表达式从 Azure Azure 函数 "integrate" space 配置计时器逻辑。