From ea7febcbac2f8f19179270d3d1a83122792b9c66 Mon Sep 17 00:00:00 2001 From: Yi EungJun Date: Mon, 13 Apr 2015 17:51:03 +0900 Subject: [PATCH] Notification: Make getting receivers faster 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) --- app/models/NotificationEvent.java | 15 +++++++++++++++ app/models/NotificationMail.java | 2 +- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/app/models/NotificationEvent.java b/app/models/NotificationEvent.java index da9dd1154..4f8776c46 100644 --- a/app/models/NotificationEvent.java +++ b/app/models/NotificationEvent.java @@ -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 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; } diff --git a/app/models/NotificationMail.java b/app/models/NotificationMail.java index 8c0239b29..1479a611a 100644 --- a/app/models/NotificationMail.java +++ b/app/models/NotificationMail.java @@ -221,7 +221,7 @@ public void addReferences() { * @see