I’m working with a massive database table that has about 500 million records. The table structure looks like this:
record_id-BIGINT- Auto increment primary keycreated_date-DATETIME- Around 1.5M records daily, old data gets purged after 12 monthsuser_identifier-VARCHAR(60)- Contains user identificationsession_id-INT- Session identifier numberbegin_time-INT- Start timestamp in epoch formatfinish_time-INT- End timestamp in epoch format- Additional fields not important for this issue
I have a unique constraint on user_identifier + session_id combination, plus an index on the date field.
The table is getting too big so I want to add partitioning. Since most queries filter by date, using date-based partitioning makes sense. But since the date isn’t part of my unique constraint, this approach won’t work.
I’m considering two approaches:
First approach: Range partitioning with triggers
Maybe I could drop the unique constraint and use a BEFORE INSERT trigger instead. The trigger would look for existing records from the previous day with matching identifiers, update the end time if found, and skip the insert.
Not sure if MySQL allows updating the same table from within a trigger or canceling the insert operation.
Second approach: Hash partitioning on VARCHAR
Alternatively, I could use hash partitioning on the user_identifier field. But I can’t find examples of converting VARCHAR to INTEGER for hash partitioning.
I tried this but got an error:
ALTER TABLE user_sessions
PARTITION BY HASH (CONV(SUBSTRING(MD5(user_identifier), 1, 8), 16, 10))
PARTITIONS 16
MySQL says the partition function isn’t allowed. What’s the right way to handle this situation?