Realmente vale a pena normalizar o caminho “Toxi”? (3NF)
Estou nos estágios iniciais do design do meu banco de dados, então nada está finalizado e estou usando o design de 3 tabelas "TOXI" para meus threads que possuem tags opcionais, mas não posso deixar de sentir que a junção é não é realmente necessário e talvez eu precise apenas confiar em uma coluna de tags simples no meuposts
tabela onde eu posso apenas armazenar um varchar de algo como<tag>, <secondTag>
.
Então, para recapitular:
vale a pena o problema das junções extras à esquerda nas 2 tabelas de tags em vez de apenas ter uma coluna de tags na minhaposts
mesa.existe uma maneira de otimizar minha consulta?EsquemaCREATE TABLE `posts` (
`post_id` INT UNSIGNED PRIMARY AUTO_INCREMENT,
`post_name` VARCHAR(255)
) Engine=InnoDB;
CREATE TABLE `post_tags` (
`tag_id` INT UNSIGNED PRIMARY AUTO_INCREMENT,
`tag_name` VARCHAR(255)
) Engine=InnoDB;
CREATE TABLE `post_tags_map` (
`map_id` INT PRIMARY AUTO_INCREMENT,
`post_id` INT NOT NULL,
`tags_id` INT NOT NULL,
FOREIGN KEY `post_id` REFERENCES `posts` (`post_id`),
FOREIGN KEY `post_id` REFERENCES `post_tags` (`tag_id`)
) Engine=InnoDB;
Dados de amostraINSERT INTO `posts` (`post_id`, `post_name`)
VALUES
(1, 'test');
INSERT INTO `post_tags` (`tag_id`, `tag_name`)
VALUES
(1, 'mma'),
(2, 'ufc');
INSERT INTO `posts_tags_map` (`map_id`, `post_id`, `tags_id`)
VALUES
(1, 1, 1),
(2, 1, 2);
Consulta atualSELECT
posts.*,
GROUP_CONCAT( post_tags.tag_name order by post_tags.tag_name ) AS tags
FROM posts
LEFT JOIN posts_tags_map
ON posts_tags_map.post_id = posts.post_id
LEFT JOIN post_tags
ON posts_tags_map.tags_id = posts_tags.tag_id
WHERE posts.post_id = 1
GROUP BY post_id
ResultadoE SE existem tags:
post_id post_name tags 1 test mma, ufc