mirror of
https://github.com/plan-player-analytics/Plan.git
synced 2025-01-08 01:17:45 +01:00
Optimized activity graph queries
- Reduced queries from 40 to 8 with union selects
This commit is contained in:
parent
56ebf0d3f8
commit
a14c97002c
@ -117,12 +117,12 @@ public class ActivityIndexQueries {
|
||||
WHERE + "u." + UserInfoTable.SERVER_UUID + "=?" +
|
||||
AND + "u." + UserInfoTable.REGISTERED + "<=?";
|
||||
|
||||
String selectActivePlayerCount = SELECT + "COUNT(1) as count" +
|
||||
String selectCount = SELECT + "COUNT(1) as count" +
|
||||
FROM + '(' + selectIndexes + ") i" +
|
||||
WHERE + "i.activity_index>=?" +
|
||||
AND + "i.activity_index<?";
|
||||
|
||||
return new QueryStatement<Integer>(selectActivePlayerCount) {
|
||||
return new QueryStatement<Integer>(selectCount) {
|
||||
@Override
|
||||
public void prepare(PreparedStatement statement) throws SQLException {
|
||||
setSelectActivityIndexSQLParameters(statement, 1, playtimeThreshold, serverUUID, date);
|
||||
@ -140,14 +140,57 @@ public class ActivityIndexQueries {
|
||||
}
|
||||
|
||||
public static Query<Map<String, Integer>> fetchActivityIndexGroupingsOn(long date, UUID serverUUID, long threshold) {
|
||||
return db -> {
|
||||
Map<String, Integer> groups = new HashMap<>();
|
||||
groups.put("Very Active", db.query(fetchActivityGroupCount(date, serverUUID, threshold, ActivityIndex.VERY_ACTIVE, 5.1)));
|
||||
groups.put("Active", db.query(fetchActivityGroupCount(date, serverUUID, threshold, ActivityIndex.ACTIVE, ActivityIndex.VERY_ACTIVE)));
|
||||
groups.put("Regular", db.query(fetchActivityGroupCount(date, serverUUID, threshold, ActivityIndex.REGULAR, ActivityIndex.ACTIVE)));
|
||||
groups.put("Irregular", db.query(fetchActivityGroupCount(date, serverUUID, threshold, ActivityIndex.IRREGULAR, ActivityIndex.REGULAR)));
|
||||
groups.put("Inactive", db.query(fetchActivityGroupCount(date, serverUUID, threshold, -0.1, ActivityIndex.IRREGULAR)));
|
||||
return groups;
|
||||
String selectActivityIndex = selectActivityIndexSQL();
|
||||
|
||||
String selectIndexes = SELECT + "? as activity_group, activity_index" +
|
||||
FROM + UserInfoTable.TABLE_NAME + " u" +
|
||||
LEFT_JOIN + '(' + selectActivityIndex + ") s on s." + SessionsTable.USER_UUID + "=u." + UserInfoTable.USER_UUID +
|
||||
WHERE + "u." + UserInfoTable.SERVER_UUID + "=?" +
|
||||
AND + "u." + UserInfoTable.REGISTERED + "<=?";
|
||||
|
||||
String selectCount = SELECT + "activity_group, COUNT(1) as count" + FROM +
|
||||
'(' + selectIndexes + ") indexes" +
|
||||
WHERE + "COALESCE(indexes.activity_index,0)>=?" +
|
||||
AND + "COALESCE(indexes.activity_index,0)<?" +
|
||||
GROUP_BY + "activity_group";
|
||||
|
||||
String selectMultipleCounts = SELECT + '*' + FROM +
|
||||
'(' + selectCount +
|
||||
UNION + selectCount +
|
||||
UNION + selectCount +
|
||||
UNION + selectCount +
|
||||
UNION + selectCount + ") selectMultiple";
|
||||
|
||||
return new QueryStatement<Map<String, Integer>>(selectMultipleCounts) {
|
||||
@Override
|
||||
public void prepare(PreparedStatement statement) throws SQLException {
|
||||
set16ValuesForQuery(statement, 1, "Very Active", ActivityIndex.VERY_ACTIVE, 5.1);
|
||||
set16ValuesForQuery(statement, 17, "Active", ActivityIndex.ACTIVE, ActivityIndex.VERY_ACTIVE);
|
||||
set16ValuesForQuery(statement, 33, "Regular", ActivityIndex.REGULAR, ActivityIndex.ACTIVE);
|
||||
set16ValuesForQuery(statement, 49, "Irregular", ActivityIndex.IRREGULAR, ActivityIndex.REGULAR);
|
||||
set16ValuesForQuery(statement, 65, "Inactive", -1, ActivityIndex.IRREGULAR);
|
||||
}
|
||||
|
||||
private void set16ValuesForQuery(
|
||||
PreparedStatement statement, int index,
|
||||
String group, double above, double below
|
||||
) throws SQLException {
|
||||
statement.setString(index, group);
|
||||
setSelectActivityIndexSQLParameters(statement, index + 1, threshold, serverUUID, date);
|
||||
statement.setString(index + 12, serverUUID.toString());
|
||||
statement.setLong(index + 13, date);
|
||||
statement.setDouble(index + 14, above);
|
||||
statement.setDouble(index + 15, below);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Integer> processResults(ResultSet set) throws SQLException {
|
||||
Map<String, Integer> groups = new HashMap<>();
|
||||
while (set.next()) {
|
||||
groups.put(set.getString("activity_group"), set.getInt("count"));
|
||||
}
|
||||
return groups;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -20,6 +20,7 @@ import com.djrapitops.plan.data.store.mutators.ActivityIndex;
|
||||
import com.djrapitops.plan.db.access.Query;
|
||||
import com.djrapitops.plan.db.access.QueryStatement;
|
||||
import com.djrapitops.plan.db.sql.tables.SessionsTable;
|
||||
import com.djrapitops.plan.db.sql.tables.UserInfoTable;
|
||||
import com.djrapitops.plan.db.sql.tables.UsersTable;
|
||||
|
||||
import java.sql.PreparedStatement;
|
||||
@ -115,12 +116,12 @@ public class NetworkActivityIndexQueries {
|
||||
LEFT_JOIN + '(' + selectActivityIndex + ") q2 on q2." + SessionsTable.USER_UUID + "=u." + UsersTable.USER_UUID +
|
||||
WHERE + "u." + UsersTable.REGISTERED + "<=?";
|
||||
|
||||
String selectActivePlayerCount = SELECT + "COUNT(1) as count" +
|
||||
String selectCount = SELECT + "COUNT(1) as count" +
|
||||
FROM + '(' + selectIndexes + ") i" +
|
||||
WHERE + "i.activity_index>=?" +
|
||||
AND + "i.activity_index<?";
|
||||
|
||||
return new QueryStatement<Integer>(selectActivePlayerCount) {
|
||||
return new QueryStatement<Integer>(selectCount) {
|
||||
@Override
|
||||
public void prepare(PreparedStatement statement) throws SQLException {
|
||||
setSelectActivityIndexSQLParameters(statement, 1, playtimeThreshold, date);
|
||||
@ -137,14 +138,55 @@ public class NetworkActivityIndexQueries {
|
||||
}
|
||||
|
||||
public static Query<Map<String, Integer>> fetchActivityIndexGroupingsOn(long date, long threshold) {
|
||||
return db -> {
|
||||
Map<String, Integer> groups = new HashMap<>();
|
||||
groups.put("Very Active", db.query(fetchActivityGroupCount(date, threshold, ActivityIndex.VERY_ACTIVE, 5.1)));
|
||||
groups.put("Active", db.query(fetchActivityGroupCount(date, threshold, ActivityIndex.ACTIVE, ActivityIndex.VERY_ACTIVE)));
|
||||
groups.put("Regular", db.query(fetchActivityGroupCount(date, threshold, ActivityIndex.REGULAR, ActivityIndex.ACTIVE)));
|
||||
groups.put("Irregular", db.query(fetchActivityGroupCount(date, threshold, ActivityIndex.IRREGULAR, ActivityIndex.REGULAR)));
|
||||
groups.put("Inactive", db.query(fetchActivityGroupCount(date, threshold, -0.1, ActivityIndex.IRREGULAR)));
|
||||
return groups;
|
||||
String selectActivityIndex = selectActivityIndexSQL();
|
||||
|
||||
String selectIndexes = SELECT + "? as activity_group, activity_index" +
|
||||
FROM + UserInfoTable.TABLE_NAME + " u" +
|
||||
LEFT_JOIN + '(' + selectActivityIndex + ") s on s." + SessionsTable.USER_UUID + "=u." + UserInfoTable.USER_UUID +
|
||||
AND + "u." + UserInfoTable.REGISTERED + "<=?";
|
||||
|
||||
String selectCount = SELECT + "activity_group, COUNT(1) as count" + FROM +
|
||||
'(' + selectIndexes + ") indexes" +
|
||||
WHERE + "COALESCE(indexes.activity_index,0)>=?" +
|
||||
AND + "COALESCE(indexes.activity_index,0)<?" +
|
||||
GROUP_BY + "activity_group";
|
||||
|
||||
String selectMultipleCounts = SELECT + '*' + FROM +
|
||||
'(' + selectCount +
|
||||
UNION + selectCount +
|
||||
UNION + selectCount +
|
||||
UNION + selectCount +
|
||||
UNION + selectCount + ") selectMultiple";
|
||||
|
||||
return new QueryStatement<Map<String, Integer>>(selectMultipleCounts) {
|
||||
@Override
|
||||
public void prepare(PreparedStatement statement) throws SQLException {
|
||||
set12ValuesForQuery(statement, 1, "Very Active", ActivityIndex.VERY_ACTIVE, 5.1);
|
||||
set12ValuesForQuery(statement, 13, "Active", ActivityIndex.ACTIVE, ActivityIndex.VERY_ACTIVE);
|
||||
set12ValuesForQuery(statement, 25, "Regular", ActivityIndex.REGULAR, ActivityIndex.ACTIVE);
|
||||
set12ValuesForQuery(statement, 37, "Irregular", ActivityIndex.IRREGULAR, ActivityIndex.REGULAR);
|
||||
set12ValuesForQuery(statement, 49, "Inactive", -1, ActivityIndex.IRREGULAR);
|
||||
}
|
||||
|
||||
private void set12ValuesForQuery(
|
||||
PreparedStatement statement, int index,
|
||||
String group, double above, double below
|
||||
) throws SQLException {
|
||||
statement.setString(index, group);
|
||||
setSelectActivityIndexSQLParameters(statement, index + 1, threshold, date);
|
||||
statement.setLong(index + 9, date);
|
||||
statement.setDouble(index + 10, above);
|
||||
statement.setDouble(index + 11, below);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Integer> processResults(ResultSet set) throws SQLException {
|
||||
Map<String, Integer> groups = new HashMap<>();
|
||||
while (set.next()) {
|
||||
groups.put(set.getString("activity_group"), set.getInt("count"));
|
||||
}
|
||||
return groups;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user