Senior
How to speed up data modification operations in a table with a million users, of whom only a thousand are active?
sobes.tech AI
Answer from AI
To speed up data modification operations in a table with a million users, of which only a thousand are active, it is advisable to focus on optimization specifically for active users:
- Use indexes on fields that allow quick identification of active users (for example, an index on the
active = truefield). - Perform updates only on the filtered set of active users to avoid touching the entire table.
- Consider partitioning the table, isolating active users into a separate partition or table.
- Use batch updates to reduce the number of transactions.
- If possible, apply lazy updates or update data asynchronously.
Example in SQL:
UPDATE users
SET last_login = NOW()
WHERE active = TRUE;
In Java, you can use batch update with JDBC:
String sql = "UPDATE users SET last_login = ? WHERE id = ?";
try (PreparedStatement ps = connection.prepareStatement(sql)) {
for (User user : activeUsers) {
ps.setTimestamp(1, new Timestamp(System.currentTimeMillis()));
ps.setLong(2, user.getId());
ps.addBatch();
}
ps.executeBatch();
}