Engineering

How to Automate SQL Query to Run at a Specific Schedule

November 11, 2023

Automate SQL Query to Run at a Specific Schedule

TL;DR

This article presents two methods to automate SQL queries to run at specific schedules

Method #1: Conventional Method with Python and Cron:

  1. Create a Python script to connect to the database and execute the SQL query.
  2. Schedule the script using Cron Jobs, specifying the desired frequency.
  3. Implement logging and monitoring for script execution and errors.

Method #2: Automating through Locale:

  1. Connect your database to Locale.
  2. Set up frequency to automatically run SQL queries.
  3. Configure notifications to keep track of query execution, including Slack messages.

While the conventional Python and Cron method provides a robust framework, it requires technical expertise and ongoing maintenance. In contrast, Locale.ai offers a simpler, no-code solution, streamlining the automation process and enhancing collaboration within teams.

You might want to schedule SQL queries to receive regular reports and updates, check your data flow, or maintain your database. One common scenario many database administrators encounter is the necessity to delete records older than a certain period, say 7 days, from a table. This allows you to avoid sitting on a mound of outdated records that do nothing but clog up your system. Particularly in fast-paced environments, where data is being added rapidly, it’s crucial to have an automated system that sweeps the older data regularly.

In this detailed article, we will look at two methods of automating SQL Query to run at your specified schedule - a conventional method with Python & Cron and an easier method with Locale.

  1. Method #1: Conventional Method of automating through Python and Cron
  2. Method #2: Automating Swiftly through Locale

Method #1: Conventional Method of automating through Python and Cron

This conventional method relies on a cron job to trigger a Python script at a specified schedule. The script connects to the database, executes the SQL query to discard records older than 7 days from a designated table, and then severs the database connection. While this method is robust, it does presuppose a familiarity with Python programming, an understanding of crontab syntax, and the ability to manage and monitor system scripts. Here's a step-by-step breakdown of how this method unfolds:

Draft Your Script

Initially, you need to craft a Python script to bridge a connection to your database. This script should encapsulate the necessary libraries (like psycopg2 for PostgreSQL or mysql-connector-python for MySQL), credentials for database access, and the SQL query destined for execution.


import psycopg2

# Database connection
conn = psycopg2.connect(
    dbname='your_dbname',
    user='your_username',
    password='your_password',
    host='your_host'
)

# Create a cursor object
cur = conn.cursor()

# SQL query to delete records older than 7 days
query = """
    DELETE FROM your_table
    WHERE your_date_column < NOW() - INTERVAL '7 days';
"""

# Execute the query
cur.execute(query)

# Commit the changes and close the connection
conn.commit()
cur.close()
conn.close()


Test Your Script

Before scheduling, validate your script's functionality by executing it manually and inspecting the database to ascertain the deletion of old records.

Schedule with Cron

Upon validation, it's time to schedule your script using cron. Launch your crontab file with crontab -e and append a new line embodying the desired schedule alongside the command to run your script. For instance, to initiate the script every day at 3am, you would add:


0 3 * * * /usr/bin/python3 /path/to/your/script.py

If you're new to crontab syntax or need a refresher, CronGuru is an excellent resource to help you nail down the correct scheduling expression.

Logging and Monitoring

With your script on auto-pilot, it's indispensable to log each execution to keep a tab on the successful runs and any potential errors. Integrate a logging framework like Python’s built-in logging module to capture information about each run.


import logging

# Configure logging
logging.basicConfig(filename='/path/to/your/logfile.log', level=logging.INFO)

# ... rest of your script ...

# Log the execution
logging.info('Script executed successfully')


Additionally, ensure to routinely monitor these logs, rectify any issues, and possibly notify the administrators in case of recurring or critical errors.

Maintenance

As your environment morphs—say, the database credentials alter or the database migrates to a fresh server—vigilance in updating your script accordingly is crucial to uphold the automatic cleanup.

This method lays down a solid framework to automate the deletion of outdated records from your database. However, it demands a fair chunk of setup, rigorous testing, and ongoing maintenance. Especially when the script or the scheduling warrants tweaking, the overhead can add up quickly. Also, it’s complex to deduce whether a query has run successfully and you might need constant monitoring of the logs to know whether it has run successfully or failed.

In the following section, Let’s see how Locale significantly trims down this process, rendering the automation of SQL queries a sheer breeze.

Method #2: Automating Swiftly through Locale

If you’re looking for a better way which is without all the unnecessary complex setups, use Locale. Locale is a no-code platform which significantly simplifies automating the SQL query and maintaining it. Automating SQL query through locale is an easy deal, Let’s see how you can automate through Locale.

Step 1: Log in to Locale, Go to Org Setting and Connect your database from the data source.

Step 2: Now go to Alerts → Create New Alerts. Choose your database → Click on Setup data to be monitored.

Database Connection setup on Locale platform

Step 3:  Write your own SQL Query and click Run to verify the data. Once you are satisfied, click Done.

SQL Query to set the alert condition that will autorun and clear records in the database for a specified amount of time

In the above example, if you want to clear some of the records in the database for a specified amount of time, you can use the below SQL script:


WITH deleted AS (
    DELETE FROM your_table 
    WHERE your_date_column < NOW() - INTERVAL '7 days'
    RETURNING *
)
SELECT NOW() AS deletion_time, COUNT(*) AS deleted_rows 
FROM deleted;

Step 4: Now set up your schedule based on your requirement, this would automatically run the query based on the frequency you set. Click on Run the query at frequency and choose your schedule

Scheduling alert runs in Locale by setting alert frequency

Step 5: With Locale, You can also get notified on you preferred communication channel about the success or failure of the query run. To configure the Slack message, Click on Configure Incidents and enter your message in the Incident Title.

Configure incidents on Locale to provide better context to the users receiving alerts

Step 6: Final Step, Set up whom or on which channel the Slack alert should be sent. Go to the Setup Notification section and click on Add Notification then select Send Slack Message. Select whether you want the Slack message to be sent to a channel or a user. You can also preview and send test notifications.

Configuring Slack alerts to define whom to send alerts on Locale

Save the alert. It will automatically run itself on the pre-set schedule and trigger a Slack notification.

Sample Slack alerts for successful SQL query run

Conclusion

In this detailed guide, we looked at two methods of how you can automate your SQL query to run at a specific time. Here we looked at the conventional approach of writing the script in Python & automating it through cron and with Locale which provides a simpler and easier method to achieve this.

The conventional approach, involving Python scripts and cron jobs, provides a robust framework for executing SQL queries at designated intervals. It demands a certain level of technical commitment for your automation process. Moreover, it comes with the overhead of maintenance, necessitating constant monitoring of status and adjustments. Traditional methods of scheduling SQL queries often involve complex scripting, meticulous setup, and continuous monitoring, posing challenges for users seeking a straightforward solution.

On the other hand, Locale offers a streamlined, no-code solution, simplifying the entire automation process. This method eliminates the need for intricate scripting and complex setup, making it easy to set up and maintain. With Locale, you not only simplify your automation efforts but also enhance collaboration within your team. With seamless Slack integrations and customizable notifications, you can ensure that everyone stays informed about the status of your automated queries. Moreover, if you want to expand sending reports/data to other destinations like Gmail, WhatsApp, MS Teams etc, you get to do that with 100+ integration options.

Excited to get started? Secure early access – speak with us.