I have a query like below where table150k has 150k records and table3m has 3m records. On our production servers, we have to run this query for a single record at a time very frequently. This costs a lot of cpu power.
select t.id, t1.field1 as f1, t2.field1 as f2, t3.field1 as f3, ..., t12.field1 as f12
from table150k t
inner join table3m t1 on t1.fk = t.id and t1.[type] = 1
inner join table3m t2 on t2.fk = t.id and t2.[type] = 2
inner join table3m t3 on t3.fk = t.id and t3.[type] = 3
...
inner join table3m t12 on t12.fk = t.id and t12.[type] = 12
where t.id = @id
When I remove inner joins from this query, it works fine. When they are included, our servers suffer cpu.
How should I optimize this query, data structure or scenario so that frequent fetches of data do not cost cpu as high?