I have 20+ supporting tables set up the following way
[common_column1, common_column2, unique_column]
I need to come up with a combined table or a view that would be set up like this
[common_column1, common_column2,
table1_unique_column, table2_unique_column, table3_unique_column, etc...]
I have 6 million + records in every supporting table. My combined table / view creation query looks like this:
select
a.common_column1, a.common_column2, a.unique_column, b.unique_column,
c.unique_column, d....
into combined_table
from table1 as a
left join table2 as b on (a.common_column1 = b.common_column1 and a.common_column2 = b.common_column2)
left join table3 as c on (a.common_column1 = c.common_column1 and a.common_column2 = c.common_column2)
left join table4 as d ...`
My primary object is to have the best read performance on the combined table / view, which I will be querying multiple times on all fields. Could you, please, suggest what would be better: to create a combined table and put indexes onto its every column or to put indexes onto every common column of every supporting table and then create a combined view? Or is there some other way to achieve my goal?
If you had no
LEFT JOIN, you could investigate the indexed views – but since you those left joins, those are out of the question.Using
LEFT JOINis messy and costly – and there’s really not much “magic” to speeding this up.Just make sure to have an index on each and every foreign key column(s) in the child tables that you use in your joins, and on your columns in the
WHEREclause(s) – that’s about all you can do, I’m afraid…But be aware: too many indices can be worse than no indices at all… pick them carefully!