I have a table UserRecord which has columns id, userId, recordType, recordCount, with INDEX on userId and auto-incrementing id. The table is to record different user behaviors and count the number of times of the behavior.
The create table looks like following: (Edit: I am not allowed to change the table structure due to project limitation)
CREATE TABLE `UserRecord` (
`id` bigint(20) unsigned NOT NULL AUTO INCREMENT,
`userId` bigint(20) unsigned NOT NULL,
`recordType` int(3) unsigned NOT NULL,
`recordCount` int(10) unsigned NOT NULL,
PRIMARY KEY (`id`),
KEY `userId` (`userId`)
);
I know in advance that there are 10 recordTypes (behavior types) in total, but some user behaviors are very unlikely so some users might not ever perform once such behavior. I am wondering whether I should generate all the 10 rows whenever a new user is created in order for the rows to have consecutive ids, or insert a record whenever it is first touched.
Method 1: I generate all the 10 rows when a new user is created, so the table size is always going to be 10 times the number of users, but every single user will have their records with consecutive ids.
Method 2: I generate a record only when a user first perform the recordType behavior. In this way the table on average will have size around 3 times the number of users, but records for the same user (of different types) might have ids very different, like 10000 and 1000000.
Now consider when I select the following query on a large scale:
select * from UserRecord where userId=x;
Remainder: userId has an INDEX on it. My question is, will the first method be significantly faster than the second, or not?
I would like to understand this from a theoretical aspect rather than experimental result.