Add index and improve pubsub db query.

This commit is contained in:
FireMaskterK 2021-11-18 23:32:19 +00:00
parent 376e068264
commit 6ec517e0ef
No known key found for this signature in database
GPG key ID: 49451E4482CC5BCD
2 changed files with 5 additions and 7 deletions

View file

@ -34,12 +34,9 @@ public class Main {
Session s = DatabaseSessionFactory.createSession();
List<PubSub> pubSubList = s.createNativeQuery(
"select distinct pubsub.* from pubsub inner join users_subscribed on pubsub.id = users_subscribed.channel",
PubSub.class).getResultList();
pubSubList.removeIf(pubsub -> {
return System.currentTimeMillis() - pubsub.getSubbedAt() < TimeUnit.DAYS.toMillis(4);
});
"select distinct pubsub.* from pubsub inner join users_subscribed on pubsub.id = users_subscribed.channel where pubsub.subbed_at < :time",
PubSub.class).setParameter("time", System.currentTimeMillis() - TimeUnit.DAYS.toMillis(4))
.getResultList();
Collections.shuffle(pubSubList);

View file

@ -7,7 +7,8 @@ import javax.persistence.Index;
import javax.persistence.Table;
@Entity
@Table(name = "pubsub", indexes = { @Index(columnList = "id", name = "pubsub_id_idx") })
@Table(name = "pubsub", indexes = { @Index(columnList = "id", name = "pubsub_id_idx"),
@Index(columnList = "subbed_at", name = "pubsub_subbed_at_idx") })
public class PubSub {
@Id