Okay
  Public Ticket #2738247
Question Database optimization automatically
Closed

Comments

  • Myra started the conversation

    Hi,

    We just started with 2 reservation systems for 23 stores on a multi site. We expect a lot of appointments and use. (not only the amount of visitors and their appointments but also all +/- 30 employees in the backend adding appointments and customers).

    To keep the systems / sites running smooth / fast, I wanted to ask if it's possible to automatically delete appointments from the past (let's say 2 weeks).

    It should be possible with a Mysql query/rule  (cronjobs), right? Do you have it for me?

    Do you have more suggestions/advice regarding the loading time? (Caching isn't an option I assume?)

    Regards, Myra

  • Myra replied

    In addition:

    the sales associates are using the backend of WordPress to manage their appointments. They wanted that because that is a calendar.

    Will the front panel load faster (shorter loading time)?

  • [deleted] replied

    Hi, Myra!

    Thank you for your inquiry.

    Please add this WP action to "wp-content/mu-plugins/functions.php":

    add_action('wp_ajax_wpamelia_delete_appointments', 'delete_old_amelia_appointments');
    function delete_old_amelia_appointments() {
        global $wpdb;     $pastAppointmentsIds = $wpdb->get_col(
            'SELECT id FROM wp_amelia_appointments WHERE bookingStart < DATE_ADD(NOW(), INTERVAL -2 WEEK)'
        );     foreach ($pastAppointmentsIds as $key => $id) {
            $pastAppointmentsBookingsIds = $wpdb->get_col(
                "SELECT id FROM wp_amelia_customer_bookings WHERE appointmentId = {$id}"
            );         if ($pastAppointmentsBookingsIds) {
                $bookingIdsQuery = '(' . implode(', ', $pastAppointmentsBookingsIds) . ')';             $wpdb->query("DELETE FROM wp_amelia_payments WHERE customerBookingId IN {$bookingIdsQuery}");
                $wpdb->query("DELETE FROM wp_amelia_customer_bookings_to_extras WHERE customerBookingId IN {$bookingIdsQuery}");
            }         $wpdb->query("DELETE FROM wp_amelia_customer_bookings WHERE appointmentId = {$id}");         $wpdb->query("DELETE FROM wp_amelia_appointments WHERE id = {$id}");
        }
    }

    It will be executed as Ajax Call.

    You should alter the code in order to prevent execution in other ways than via Cron.

    Best Regards. 

  • Myra replied

    Hi Liza,

    thanks so much! Will this file be overwritten when updating Amelia?

    What do you mean by "You should alter the code in order to prevent execution in other ways than via Cron." Do you mean 'must' or 'can' ? Can I use this code without crashing the site? What other ways are there besides Cron?

  • [deleted] replied

    Hi, Myra!

    By "alter the code' our developer meant that you can change the logic or add some other funcionality to it.

    That's the code that was added by our developer:

    add_action('wp_ajax_nopriv_wpamelia_delete_appointments', 'delete_old_amelia_appointments');
    function delete_old_amelia_appointments()
    {
        if (!isset($_GET['token'], $_GET['weeksCount']) || $_GET['token'] !== 'some_long_token') {
            die;
        }     global $wpdb;     $weeksCount = $_GET['weeksCount'];     $pastAppointmentsIds = $wpdb->get_col(
            "SELECT id FROM wp_amelia_appointments WHERE bookingStart < DATE_ADD(NOW(), INTERVAL -{$weeksCount} WEEK)"
        );     foreach ($pastAppointmentsIds as $key => $id) {
            $pastAppointmentsBookingsIds = $wpdb->get_col(
                "SELECT id FROM wp_amelia_customer_bookings WHERE appointmentId = {$id}"
            );         if ($pastAppointmentsBookingsIds) {
                $bookingIdsQuery = '(' . implode(', ', $pastAppointmentsBookingsIds) . ')';
                $wpdb->query("DELETE FROM wp_amelia_payments WHERE customerBookingId IN {$bookingIdsQuery}");
                $wpdb->query("DELETE FROM wp_amelia_customer_bookings_to_extras WHERE customerBookingId IN {$bookingIdsQuery}");
            }         $wpdb->query("DELETE FROM wp_amelia_customer_bookings WHERE appointmentId = {$id}");
            $wpdb->query("DELETE FROM wp_amelia_appointments WHERE id = {$id}");
        }
    }

    In case of update, you can add it to "/wp-content/mu-plugins/functions.php" (If the file doesn't exist, you can create it). After that, you need to add Cron Job to fire GET request e.g.: https://example/wp-admin/admin-ajax.php?action=wpamelia_delete_appointments&token=some_long_token&weeksCount=2. This URL will execute the code. Our developer added token and weeksCount parameters to the URL (weeksCount is for the number of weeks before which appointments will be deleted and token is for preventing others to execute the code).

    Hope you will find this explanation clear.

    Best Regards.