I have a useruser
table and groupgroup
table, and a many to many relationship between them.
I have a votevote
table (one to many relation between groupgroup
and votevote
) And a subitemsubitem
table (one to many between votevote
and subitemsubitem
)
And I have a subitem actionssub_item_actions
table which is supposed to record which useruser
performed what kind of actionaction
on which subitemsubitem
. It has userid | grp_id | subitemid | vote_id | action type (enum)columns:
userid
grp_id
subitemid
vote_id
action type (enum)
For action type ,it it has the following values: delete update cast uncast
User can cast and uncast a subitem. For a user to 'belong' to a subitem , the latest cast or uncast action that the user has performed on a subitem should be of type cast. This means user has last cast on a subitem and has not uncast it yet.
I want to get all users that 'belong' to a sub item.
This is the query I am using :
select
select
act.*
from (
select sub_item_id,user_id user_id, max(created_at) as newestAction
from vote_sub_item_action use
where sub_item_id =10= 10
AND action_type IN ('ACTION_CAST_VOTE', 'action_uncast_vote')
GROUP BY user_id
) as x
inner join
vote_sub_item_action as act
on act.user_id = x.user_id
and act.created_at = newestAction
and act.action_type='action_cast_vote'action_type = 'action_cast_vote'
and act.sub_item_id =x= x.sub_item_id;
How should I index this table ?
I am using mysql (version 5.7)