Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
09/19/2021 03:58:09 PM (5 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/tests/phpunit/testcases/notifications/functions.php

    r12605 r13112  
    542542        $this->assertEqualSets( [ $n1 ], wp_list_pluck( $found, 'id' ) );
    543543    }
     544
     545    /**
     546     * @ticket BP8426
     547     */
     548    public function test_bp_notifications_mark_notifications_by_ids() {
     549        $u = self::factory()->user->create();
     550
     551        $n = self::factory()->notification->create(
     552            array(
     553                'component_name'    => 'barfoo',
     554                'component_action'  => 'new_bar',
     555                'item_id'           => 98,
     556                'user_id'           => $u,
     557            )
     558        );
     559
     560        for ( $i = 101; $i < 111; ++$i ) {
     561            self::factory()->notification->create(
     562                array(
     563                    'component_name'    => 'foobar',
     564                    'component_action'  => 'new_foo',
     565                    'item_id'           => $i,
     566                    'user_id'           => $u,
     567                )
     568            );
     569        }
     570
     571        $unread = wp_list_pluck(
     572            BP_Notifications_Notification::get(
     573                array(
     574                    'user_id'           => $u,
     575                    'component_name'    => 'foobar',
     576                    'component_action'  => 'new_foo',
     577                    'is_new'            => 1,
     578                )
     579            ),
     580            'id'
     581        );
     582
     583        bp_notifications_mark_notifications_by_ids( $unread );
     584
     585        $read = wp_list_pluck(
     586            BP_Notifications_Notification::get(
     587                array(
     588                    'user_id'           => $u,
     589                    'component_name'    => 'foobar',
     590                    'component_action'  => 'new_foo',
     591                    'is_new'            => 0,
     592                )
     593            ),
     594            'id'
     595        );
     596
     597        $n_get = BP_Notifications_Notification::get(
     598            array(
     599                'id' => $n,
     600                'component_name'    => 'barfoo',
     601                'component_action'  => 'new_bar',
     602            )
     603        );
     604
     605        $n_obj = reset( $n_get );
     606
     607        $this->assertEquals( $unread, $read );
     608        $this->assertEquals( $n, $n_obj->id );
     609        $this->assertTrue( 1 === (int) $n_obj->is_new );
     610    }
     611
     612    /**
     613     * @ticket BP8426
     614     * @group delete_notifications_by_ids
     615     */
     616    public function test_bp_notifications_delete_notifications_by_ids() {
     617        $u = self::factory()->user->create();
     618
     619        $n = self::factory()->notification->create(
     620            array(
     621                'component_name'    => 'barfoo',
     622                'component_action'  => 'new_bar',
     623                'item_id'           => 98,
     624                'user_id'           => $u,
     625            )
     626        );
     627
     628        for ( $i = 101; $i < 111; ++$i ) {
     629            self::factory()->notification->create(
     630                array(
     631                    'component_name'    => 'foobar',
     632                    'component_action'  => 'new_foo',
     633                    'item_id'           => $i,
     634                    'user_id'           => $u,
     635                )
     636            );
     637        }
     638
     639        $unread = wp_list_pluck(
     640            BP_Notifications_Notification::get(
     641                array(
     642                    'user_id'           => $u,
     643                    'component_name'    => 'foobar',
     644                    'component_action'  => 'new_foo',
     645                    'is_new'            => 1,
     646                )
     647            ),
     648            'id'
     649        );
     650
     651        bp_notifications_delete_notifications_by_ids( $unread );
     652
     653        $deleted = wp_list_pluck(
     654            BP_Notifications_Notification::get(
     655                array(
     656                    'user_id'           => $u,
     657                    'component_name'    => 'foobar',
     658                    'component_action'  => 'new_foo',
     659                    'is_new'            => 1,
     660                )
     661            ),
     662            'id'
     663        );
     664
     665        $n_get = BP_Notifications_Notification::get(
     666            array(
     667                'id' => $n,
     668                'component_name'    => 'barfoo',
     669                'component_action'  => 'new_bar',
     670            )
     671        );
     672
     673        $n_obj = reset( $n_get );
     674
     675        $this->assertEmpty( $deleted );
     676        $this->assertEquals( $n, $n_obj->id );
     677        $this->assertTrue( 1 === (int) $n_obj->is_new );
     678    }
    544679}
Note: See TracChangeset for help on using the changeset viewer.