简体   繁体   English

在mysql中将int值存储为varchar是否会严重影响性能?

[英]Does storing int value as varchar in mysql affect performance heavily?

I'm working on a website which should be multilingual and also in some products number of fields may be more than other products (for example may be in the future a products have an extra feature which old products doesn't have it). 我正在一个应该使用多种语言的网站上工作,而且在某些产品中,某些领域的数量可能比其他产品更多(例如,将来某个产品可能具有旧产品所没有的额外功能)。 because of this problem I decided to have a product table with common fields which all products can have and in all languages are same (like width and height) and add another three tables for storing extra fields as below: 由于这个问题,我决定创建一个产品表,其中包含所有产品可以具有的通用字段,并且所有语言都相同(例如宽度和高度),并添加另外三个表来存储额外的字段,如下所示:

field (id,name)
field_name(field_id,lang_id,name)
field_value(product_id, field_id, lang_id, value)

by doing this I can fetch all the values from one table but the problem is that values can be in different types, for example it could be a number or a text. 通过这样做,我可以从一个表中获取所有值,但是问题是值可以具有不同的类型,例如它可以是数字或文本。 I checked on an open source project "Drupal" and in that they create a table for each field type and by doing joins they will retrieve a node data. 我检查了一个开源项目“ Drupal”,他们为每种字段类型创建了一个表,并通过进行联接将检索节点数据。 I want to know which way will impact the performance more? 我想知道哪种方式会对性能产生更大的影响? having a table for each extra field or storing all of their value in one table and convert their type on the fly by casting? 在每个额外字段中都有一张表,或者将所有值存储在一个表中,并通过强制转换即时转换其类型?

thank you in advance 先感谢您

Yes, but no. 是的,但是没有。 You are storing your data in an entity-attribute-value form (EAV). 您正在以实体属性值格式(EAV)存储数据。 This is rather inefficient in general. 一般而言,这效率很低。 Here are some issues: 这里有一些问题:

  • As you have written it, you cannot do type checking. 如您所写,您不能进行类型检查。
  • You cannot set-up foreign key relationships in the database. 您不能在数据库中设置外键关系。
  • Fetching the results for a single row requires multiple join s or a group by . 提取单个行的结果需要多个join或一个group by
  • You cannot write indexes on a specific column to speed access. 您不能在特定列上写索引以加快访问速度。

There are some work-arounds. 一些解决方法。 You can get around the typing issue by having separate columns for different types. 您可以通过为不同类型设置单独的列来解决输入问题。 So, the data structure would have: 因此,数据结构将具有:

  • Name 名称
  • Type 类型
  • ValueString 值字符串
  • ValueInt ValueInt
  • ValueDecimal 十进制值

Or whatever types you want to support. 或您要支持的任何类型。

There are some other "tricks" if you want to go this route. 如果您想走这条路线,还有其他一些“技巧”。 The most important is to decimal align the numbers. 最重要的是使数字十进制对齐。 So, instead of storing '1' and '10' , you would store ' 1' and '10' . 因此,除了存储'1''10' ,您还可以存储' 1''10' This makes the value more amenable to ordering. 这使得该值更易于订购。

When faced with such a problem, I often advocate a hybrid approach. 面对这样的问题,我经常提倡一种混合方法。 This approach would have a fixed record with the important properties all nicely located in columns with appropriate types and indexes -- columns such as: 这种方法将具有固定的记录,记录中所有重要的属性都很好地位于具有适当类型和索引的列中,例如:

  • ProductReleaseDate 产品发布日期
  • ProductDescription 产品描述
  • ProductCode 产品代码

And whatever values are most useful. 而任何值都是最有用的。 An EAV table can then be used for additional properties that are optional. 然后,可以将EAV表用于可选的其他属性。 This generally balances the power of the relational database to handle structured data along with the flexibility of an EAV approach to support variable columns. 通常,这平衡了关系数据库处理结构化数据的功能以及支持可变列的EAV方法的灵活性。

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

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