Per mettere in join diverse tabelle (non molte) uso tabelle aggiuntive che contengono l'id di una e l'id dell'altra.
Ho fatto l'explain di una select con le due soluzioni, ma ho dei dubbi sulle differenze di performance (in rosso le differenze):
- unico indice su due colonne
id select_type table type possible_keys key key_len ref rows Extra 1 SIMPLE authors system PRIMARY NULL NULL NULL 1 1 SIMPLE slugs const slug,post_id slug 767 const 1 1 SIMPLE join_posts_vs_authors const idx idx 8 const,const 1 Using index 1 SIMPLE posts const PRIMARY PRIMARY 4 const 1 1 SIMPLE join_posts_vs_post_types ref idx idx 4 const 1 Using index 1 SIMPLE post_types eq_ref PRIMARY PRIMARY 4 test.join_posts_vs_post_types.post_type_id 1
- due indici distinti, uno per ogni colonna id
id select_type table type possible_keys key key_len ref rows Extra 1 SIMPLE authors system PRIMARY NULL NULL NULL 1 1 SIMPLE slugs const slug,post_id slug 767 const 1 1 SIMPLE posts const PRIMARY PRIMARY 4 const 1 1 SIMPLE join_posts_vs_authors ref author_id,post_id post_id 4 const 1 Using where 1 SIMPLE join_posts_vs_post_types ref post_id,post_type_id post_id 4 const 1 1 SIMPLE post_types eq_ref PRIMARY PRIMARY 4 test.join_posts_vs_post_types.post_type_id 1
Grazie
Ciao!