Let's say I have a very large lookup table that looks like this:
CREATE TABLE `MyLookup` (
`FKToTableA` bigint(20) NOT NULL,
`FKToTableB` bigint(20) NOT NULL,
`Count` bigint(20) NOT NULL,
`Bytes` bigint(20) NOT NULL,
`Packets` bigint (20) NOT NULL,
) ENGINE=InnoDB;
The first 2 attributes are FKs to Table A and B respectively. But doesn't need to be. the FKs can be dropped if it's more optimal not to have it. FKToTableA and FKToTableB is the candidate key for this table. Thus FKToTableA and FKToTableB could be a composite primary key if needed.
My question is what's the most optimal way of indexing this table? Storage size and insert times are not a concern.
Most of the time my queries will be joining on to MyLookup on both FK columns and SUM the Count, Bytes and Packets columns.
select
a.something, b.something, SUM(c.Count), SUM(c.Bytes), SUM(b.Packets)
from
A a
inner join MyLookup c on a.Id = c.FKToTableA
inner join B b on b.Id = c.FKToTableB
where
a.something = 1 and
a.Time >= 'blah' and
a.Time <= 'blah'
group by
a.something, b.something
I see three options.
Put a HASH index on FKToTableA and FKToTableB.
Put a composite BTREE index on all columns.
Put a Primary Key on FKToTableA and FKToTableB.
I'm kind of leaning towards 1. From what I gather HASH indexes are great at equality comparisons and that's all joins are is a big equality comparison is it not? Still has to go hit the table again to sum the other columns though which may be a bottle neck. Not sure.
Or maybe it's possible to have a hash index with Count, Bytes, Packets? Not sure how composite hash indexes works though.
Has anyone dealt with this type of thing before and can shed some knowledge and suggestions?