简体   繁体   English

mysql数据库设计一张表很多键值行的性能?

[英]Mysql database design one table many key value rows performance?

Most of the content types and categories of a project I'm working on has many tables related to each content, pages, blog_articles, blog_tags, recipes, recipes_categories, ads, banners, covers, customers etc. 我正在处理的项目的大多数内容类型和类别都有许多与每种内容,页面,blog_articles,blog_tags,食谱,cipes_categories,广告,横幅,封面,客户等相关的表。

What all this tables have in common and repeated in each table is title, slug, description, only few of them have other fields, like recipes has ingredients|methods|level_of_difficulty and so on. 所有这些表的共同点是在表中重复出现的是标题,子句,描述,只有少数几个表具有其他字段,例如配方具有配料,难度等。

What I was thinking is to get rid of many tables and narrow it down to only two tables, kind of like wordpress for posts and postmeta. 我当时想的是摆脱很多表格,将其缩小到只有两个表格,有点像wordpress的post和postmeta。

For example 例如

content_table
-------------
id
parent_id
type
title
slug
body

content_meta
-------------
id
content_id
type
key
value

In the content_meta I was thinking to do something like this 我在content_meta中正在考虑做这样的事情

content_meta
----------------------------------------------------------
| id | content_id | type   | key         | value
----------------------------------------------------------
| 1  | 1          | banner | image       | img.jpg
| 2  | 1          | banner | link_to     | google.com
| 3  | 2          | recipe | ingredient  | milk
| 4  | 2          | recipe | method      | stir it 
| 5  | 2          | recipe | difficulty  | medium

I'm very well aware that for only few types of content the content meta table is going to grow quick 我非常清楚,对于仅几种类型的内容,内容元表将迅速增长

What do you think of this type of design from performance point of view and should I drop it? 从性能的角度来看,您如何看待这种设计?我应该放弃它吗?

It would be a better idea to either merge some tables or remove the redundant columns. 合并一些表或删除冗余列是一个更好的主意。

The content_meta idea could make for some pretty nasty queries later on, that concept would likely used when the "key" is dynamic. content_meta想法稍后可能会引起一些非常讨厌的查询,该概念很可能在“键”是动态的时使用。 You'll have to rely on multiple subqueries (or JOINs) for each field in the content_meta table, instead of a single query. 对于content_meta表中的每个字段,您将不得不依赖多个子查询(或JOIN),而不是单个查询。

SELECT title, body, ingredient, method FROM content_table 
INNER JOIN recipes ON content_table.content_id = recipes.content_id

VS

SELECT title, body, rIngredient.value 'ingredient', rMethod.value 'method' FROM content_table
LEFT JOIN content_meta rIngredient ON content_table.content_id = rIngredient.content_id AND type = 'recipe' AND key = 'ingredient'
LEFT JOIN content_meta rMethod ON content_table.content_id = rMethod.content_id AND type = 'recipe' AND key = 'method'

You may want to look into database normalization: https://en.wikipedia.org/wiki/Database_normalization 您可能要研究数据库规范化: https : //en.wikipedia.org/wiki/Database_normalization

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM