Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8974991
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:46:39+00:00 2026-06-15T18:46:39+00:00

So here is my problem, I have written a stored procedure to do the

  • 0

So here is my problem, I have written a stored procedure to do the following task. In table events there are events that might potentially exist for venues that no longer exist. Not all events are tied to a venue, but the ones that are have an integer value in their venue id field otherwise it is NULL (or potentially zero but that is accounted for). Periodically, venues get deleted from our system, when that happens it is not possible to delete all of the events associated with that venue at that exact time. Instead, a task is run periodically at a later time that deletes every event that has a venue id that no longer references an existing record in the venues table. I have written a stored procedure for this and it seems to work.

This is the stored procedure:

DROP PROCEDURE IF EXISTS delete_synced_events_orphans;
DELIMITER $$
CREATE PROCEDURE delete_synced_events_orphans()
BEGIN 

    DECLARE event_count int(11) DEFAULT 0;
    DECLARE active_event_id int(11) DEFAULT 0;
    DECLARE active_venue_id int(11) DEFAULT 0;
    DECLARE event_to_delete_id int(11) DEFAULT NULL;

    CREATE TEMPORARY TABLE IF NOT EXISTS possible_events_to_delete (
        event_id int(11) NOT NULL,
        venue_id_temp int(11) NOT NULL
    ) engine = memory;

    # create an "array" which is a table that holds the events that might need deleting
    INSERT INTO possible_events_to_delete (event_id, venue_id_temp) SELECT `events`.`id`, `events`.`venue_id` FROM `events` WHERE `events`.`venue_id` IS NOT NULL AND `events`.`venue_id` <> 0;
    SELECT COUNT(*) INTO `event_count` FROM `possible_events_to_delete` WHERE 1;

    detector_loop: WHILE `event_count` > 0 DO
        SELECT event_id INTO active_event_id FROM possible_events_to_delete WHERE 1 LIMIT 1;
        SELECT venue_id_temp INTO active_venue_id FROM possible_events_to_delete WHERE 1 LIMIT 1;

        # this figures out if there are events that need to be deleted
        SELECT `events`.`id` INTO event_to_delete_id FROM `events`, `venues` WHERE `events`.`venue_id` <> `venues`.`id` AND `events`.`id` = active_event_id AND `events`.`venue_id` = active_venue_id;

        #if no record meets that query, the active event is safe to delete
        IF (event_to_delete_id <> 0 AND event_to_delete_id IS NOT NULL) THEN
            DELETE FROM `events` WHERE `events`.`id` = event_to_delete_id;
            #INSERT INTO test_table (event_id_test, venue_id_temp_test) SELECT `events`.`id`, `events`.`venue_id` FROM `events` WHERE `events`.`id` =  event_to_delete_id;
        END IF;

        DELETE FROM possible_events_to_delete WHERE `event_id` = active_event_id AND `venue_id_temp` = active_venue_id;
        SET `event_count` = `event_count` - 1;

    END WHILE;

END $$
DELIMITER ;

Here is the table structure for the two tables in question:

CREATE TABLE IF NOT EXISTS events (
    id int(11) NOT NULL,
    event_time timestamp NOT NULL,
    venue_id_temp int(11) NOT NULL
);

CREATE TABLE IF NOT EXISTS venues (
    event_id int(11) NOT NULL,
    venue_id_temp int(11) NOT NULL
);

The stored procedure works as written, but I want to know about ways that it could be made to run better. It seems like its doing a lot of extra processing to achieve its goal. Are there better ways I could query the data at hand, or are there other more useful commands and key words I could use that I just don’t know about, which would allow me to complete this task better (fewer lines less computation). I am still learning how to use stored procedures, so I am using them to complete tasks as pragmatically as possible, I want to understand how this specific query could be made to better use the full range of features in MySQL to its advantage. Thank you folks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T18:46:40+00:00Added an answer on June 15, 2026 at 6:46 pm

    Everithing is much simpler:

    DROP PROCEDURE IF EXISTS delete_synced_events_orphans;
    DELIMITER $$
    CREATE PROCEDURE delete_synced_events_orphans()
    BEGIN 
    
      DELETE
        FROM `events`
        WHERE `venue_id` IS NOT NULL AND `venue_id` <> 0
          AND `venue_id` NOT IN (SELECT `id` FROM `venues`)
      ;
    
    END $$
    DELIMITER ;
    

    That’s it. 🙂

    You think imperatively, trying to say MySQL how to complete your task. But SQL is a declarative language, designed for saying what to do.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is the problem: I have two columns in a table that, for each
I have a stored procedure written in T-SQL and want to use a table
I have written the following piece of code ( sql clr stored procedure )
Here's my problem: I have an unmanaged dll that I made.I'm calling one of
I have written aa insert query in a stored procedure... INSERT INTO `ascencom_soa2`.`rardata` (`threatDescription`,`rtpControlObjective`,
I have problem adding arraylist to list view, will explain about my problem here..
First, sorry for my bad english, I'm French. Here the problem : I have
Here's the problem: I have a data-bound list of items, basically a way for
Here is my problem : I have a list of messages which I can
Here is my problem: I have an array of model class(Let's say, 'addressModel' with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.