简体   繁体   English

如何定期截断 Microsoft Azure 上的 SQL 数据库

[英]How to truncate SQL database on Microsoft Azure periodically

I have a SQL database running on Microsoft Azure.我有一个在 Microsoft Azure 上运行的 SQL 数据库。 To preventing it from getting too big, I have to truncate it periodically (eg, a day or two).为了防止它变得太大,我必须定期截断它(例如,一两天)。 truncate table xxx is the SQL that I need to execute. truncate table xxx是我需要执行的 SQL。

So what is the easiest way to achieve this?那么实现这一目标的最简单方法是什么? I prefer not writing any C# code unless I have to do so.我宁愿不编写任何 C# 代码,除非我必须这样做。 Can I use a web job which continuously running a truncate SQL statement?我可以使用连续运行截断 SQL 语句的 Web 作业吗? Or can I use a built-in functionality of SQL database on the Azure to achieve this?或者我可以使用 Azure 上的 SQL 数据库的内置功能来实现这一点吗? Thanks!谢谢!

Actually, you could use an Azure Function ("TimeTrigger" type) to periodically purge your tables, here is an example of code to use inside a C# TimeTrigger Azure Function to get a connexion to your Azure Sql Database and execute "delete" sql queries :实际上,您可以使用 Azure 函数(“TimeTrigger”类型)来定期清除您的表,以下是在 C# TimeTrigger Azure 函数中使用的代码示例,以获取与 Azure Sql 数据库的连接并执行“删除”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();
         }
    }
}

Then you can configure the timer logic from the Azure Azure Function "integrate" space with a cron expression.然后,您可以使用cron表达式从 Azure Azure 函数“集成”空间配置计时器逻辑。

SQL Azure does not yet have any sort of SQL Agent functionality so you'll have to create a web job (or some JavaScript that executes the SQL you need executed) and then use Azure Scheduler to schedule the job. SQL Azure 还没有任何类型的 SQL 代理功能,因此您必须创建一个 Web 作业(或一些执行您需要执行的 SQL 的 JavaScript),然后使用 Azure 调度程序来安排作业。

You can also use Azure Automation that involves Powershell to do the same thing.您还可以使用涉及 Powershell 的 Azure 自动化来执行相同的操作。

You can create a Elastic database pool and include your database in the pool.once your are done doing this,you can run set of tasks or queries against all databases in the pool or single database..您可以创建一个弹性数据库池并将您的数据库包含在池中。完成此操作后,您可以对池中的所有数据库或单个数据库运行一组任务或查询。

More on this here..更多关于这里..

https://azure.microsoft.com/en-in/documentation/articles/sql-database-elastic-jobs-overview/ https://azure.microsoft.com/en-in/documentation/articles/sql-database-elastic-jobs-overview/

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

相关问题 如何在Microsoft Azure的Sql数据库中存储数据 - How To store data in Sql database in Microsoft Azure 如何自动将本地SQl数据库与Microsoft Azure Sql数据库同步? - How to auto sync local SQl Database with Microsoft Azure Sql database? 如何使用 ASP.NET 备份 Microsoft Azure SQL 数据库 - How To Backup Microsoft Azure SQL Database Using ASP.NET 如何使用c#将对话消息记录到Microsoft botframework sdk v4中的Azure SQL数据库中 - how to log conversation messages into azure sql database in microsoft botframework sdk v4 using c# 我们可以将 Microsoft Azure Bot 连接到 Azure SQL 数据库并从 DB 读取数据并将其用作机器人的回复吗? - Can we connect Microsoft Azure Bot to Azure SQL Database and read data from the DB and use it as a reply for the Bot? 使用ASP.NET MVC应用程序定期插入Azure数据库 - Periodically insertion into Azure database with ASP.NET MVC application 如何允许客户端IP访问Microsoft Azure数据库? - How to allow client IP access to Microsoft Azure database? 实体框架多个上下文和Microsoft Azure。 如何更新数据库? - Entity Framework multiple context and Microsoft Azure. How to update database? 如何在winforms中定期自动备份MySql数据库? - How to auto backup MySql database periodically in winforms? Microsoft Azure中的SQL连接错误 - SQL Connection Errors in Microsoft Azure
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM