Skip to content
This repository has been archived by the owner on Sep 12, 2018. It is now read-only.

Commit

Permalink
Notification: Make getting receivers faster
Browse files Browse the repository at this point in the history
The field access produced a query as follows:

    select int_.notification_event_id c0, t0.id c1 from n4user t0 left
    outer join notification_event_n4user int_ on int_.n4user_id = t0.id
    where (int_.notification_event_id) in (?);

and it is very slow because "where (int_.notification_event_id) in (?)"
does not use an index. See the plan:

    PLAN
    SELECT
    INT_.NOTIFICATION_EVENT_ID AS C0,
    T0.ID AS C1
    FROM PUBLIC.N4USER T0
    /* PUBLIC.N4USER.tableScan */
    LEFT OUTER JOIN PUBLIC.NOTIFICATION_EVENT_N4USER INT_
    /* PUBLIC.FK_NOTIFICATION_EVENT_N4USER__02_INDEX_3:
     * N4USER_ID = T0.ID */
    ON INT_.N4USER_ID = T0.ID
    WHERE INT_.NOTIFICATION_EVENT_ID = 249412

The new query is 150 times faster because it uses an index for every 'where'
clause. See the plan for new query:

    PLAN
    SELECT
        N4USER.ID
    FROM PUBLIC.N4USER
        /* PUBLIC.FK_USER_ID_INDEX_6: ID IN(SELECT
            N4USER_ID
        FROM PUBLIC.NOTIFICATION_EVENT_N4USER
            /++ PUBLIC.PRIMARY_KEY_3: NOTIFICATION_EVENT_ID = 221467 ++/
        WHERE NOTIFICATION_EVENT_ID = 221467)
        */
    WHERE ID IN(
        SELECT
            N4USER_ID
        FROM PUBLIC.NOTIFICATION_EVENT_N4USER
            /* PUBLIC.PRIMARY_KEY_3: NOTIFICATION_EVENT_ID = 221467 */
        WHERE NOTIFICATION_EVENT_ID = 221467)
  • Loading branch information
Yi EungJun committed Apr 13, 2015
1 parent cb0a4e4 commit ea7febc
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
15 changes: 15 additions & 0 deletions app/models/NotificationEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,21 @@ public class NotificationEvent extends Model {
@OneToOne(mappedBy="notificationEvent", cascade = CascadeType.ALL)
public NotificationMail notificationMail;

/**
* Returns receivers.
*
* This is much faster than field access to {@link #receivers}.
*
* @return receivers
*/
public Set<User> findReceivers() {
String sql = "select n4user.id from n4user where id in (select n4user_id " +
"from notification_event_n4user where " +
"notification_event_id = '" + id + "')";

return User.find.setRawSql(RawSqlBuilder.parse(sql).create()).findSet();
}

public String getOldValue() {
return oldValue;
}
Expand Down
2 changes: 1 addition & 1 deletion app/models/NotificationMail.java
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ public void addReferences() {
* @see <a href="https://github.com/nforge/yobi/blob/master/docs/technical/watch.md>watch.md</a>
*/
private static void sendNotification(NotificationEvent event) {
Set<User> receivers = event.receivers;
Set<User> receivers = event.findReceivers();

// Remove inactive users.
Iterator<User> iterator = receivers.iterator();
Expand Down

0 comments on commit ea7febc

Please sign in to comment.