Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
09/19/2021 03:58:09 PM (3 years ago)
Author:
imath
Message:

Improve SQL performance when updating/deleting a list of notifications

Instead of looping into a list of notifications to run an update/delete query for each notification, update/delete a list of notifications using a single query. Use primarly this change for message thread notifications and bulk notification actions.

Mainly introduces 2 new static methods to BP_Notifications_Notification:

  • BP_Notifications_Notification::update_id_list() let you update a list of notifications using their notification IDs or component item IDs.
  • BP_Notifications_Notification::delete_id_list() let you delete a list of notifications using their notification IDs or component item IDs.

Props oztaser

Fixes #8426

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-notifications/bp-notifications-functions.php

    r13108 r13112  
    386386
    387387/**
     388 * Delete notifications by notification ids.
     389 *
     390 * @since 10.0.0
     391 *
     392 * @param  int[]     $ids IDs of the associated notifications.
     393 * @return int|false      The number of rows updated. False on error.
     394 */
     395function bp_notifications_delete_notifications_by_ids( $ids ) {
     396    return BP_Notifications_Notification::delete_by_id_list( 'id', $ids );
     397}
     398
     399/**
     400 * Delete notifications by item ids and user.
     401 *
     402 * @since 10.0.0
     403 *
     404 * @param  int       $user_id          ID of the user whose notifications are being deleted.
     405 * @param  int[]     $item_ids         IDs of the associated items.
     406 * @param  string    $component_name   Name of the associated component.
     407 * @param  string    $component_action Name of the associated action.
     408 * @return int|false                   The number of rows updated. False on error.
     409 */
     410function bp_notifications_delete_notifications_by_item_ids( $user_id, $item_ids, $component_name, $component_action ) {
     411    return BP_Notifications_Notification::delete_by_id_list(
     412        'item_id',
     413        $item_ids,
     414        array(
     415            'user_id'          => $user_id,
     416            'component_name'   => $component_name,
     417            'component_action' => $component_action
     418        )
     419    );
     420}
     421
     422/**
    388423 * Delete all notifications by type.
    389424 *
     
    581616            'component_name'   => $component_name,
    582617            'component_action' => $component_action,
     618        )
     619    );
     620}
     621
     622/**
     623 * Mark notifications read/unread by item ids and user.
     624 *
     625 * @since 10.0.0
     626 *
     627 * @param  int       $user_id          ID of the user whose notifications are being deleted.
     628 * @param  int[]     $item_ids         IDs of the associated items.
     629 * @param  string    $component_name   Name of the associated component.
     630 * @param  string    $component_action Name of the associated action.
     631 * @param  int|false $is_new           0 for read, 1 for unread.
     632 * @return int|false                   The number of rows updated. False on error.
     633 */
     634function bp_notifications_mark_notifications_by_item_ids( $user_id, $item_ids, $component_name, $component_action, $is_new = false ) {
     635    return BP_Notifications_Notification::update_id_list(
     636        'item_id',
     637        $item_ids,
     638        array(
     639            'is_new' => $is_new,
     640        ),
     641        array(
     642            'user_id'          => $user_id,
     643            'component_name'   => $component_name,
     644            'component_action' => $component_action
     645        )
     646    );
     647}
     648
     649/**
     650 * Mark notifications read/unread by notification ids.
     651 *
     652 * @since 10.0.0
     653 *
     654 * @param  int[]     $ids     IDs of the associated notification items.
     655 * @param  int|false $is_new  0 for read, 1 for unread.
     656 * @return int|false          The number of rows updated. False on error.
     657 */
     658function bp_notifications_mark_notifications_by_ids( $ids, $is_new = false ) {
     659    return BP_Notifications_Notification::update_id_list(
     660        'id',
     661        $ids,
     662        array(
     663            'is_new' => $is_new,
    583664        )
    584665    );
Note: See TracChangeset for help on using the changeset viewer.