将枚举存储为整数与字符串之间是否存在差异?

时间:2022-12-16 05:28:31

Should I bother to store options like title, gender, and salutation as integers, or is it not relevant and I can store them as strings?

我是否应该将标题,性别和称呼等选项存储为整数,或者它是否相关,我可以将它们存储为字符串?

class User < ActiveRecord::Base
  enumerize :gender, in: [:male, :female]

  TITLE = { dr: 0, prof: 1, prof_dr: 2 }
  def title
    TITLE.key(read_attribute(:title))
  end

  def title=(t)
    write_attribute(:title, TITLE[t.to_sym])
  end
end

Let's say I have 2000 users and 6 possible options for each of them.

假设我有2000个用户,每个用户有6个可能的选项。

3 个解决方案

#1


0  

In the past when storage space was costly there were reasons to store strings as numbers. In this day and age except for embedded applications (which should probably not be running rails) there is not a need, especially given your example database size of 2000 users.

在过去,当存储空间昂贵时,有理由将字符串存储为数字。在这个时代,除了嵌入式应用程序(可能不应该运行rails)之外,没有必要,特别是考虑到您的示例数据库大小为2000个用户。

Also, if you store your strings as a number you will have the following downsides:

此外,如果您将字符串存储为数字,则会有以下缺点:

  • Increased code complexity
  • 代码复杂性增加
  • Database that can't be "read" without the code (Having done some database ports and written rails as a front end for back end data analysis these both become important.)
  • 没有代码就无法“读取”的数据库(将一些数据库端口和写入的轨道作为前端进行后端数据分析这些都变得很重要。)

For performance concerns make sure you have your database keys setup correctly.

对于性能问题,请确保正确设置数据库密钥。

#2


0  

Integers work better on the database level but make sure you remember what each of the numbers stand for.

整数在数据库级别上工作得更好,但要确保记住每个数字代表的含义。

Strings are much easier to work with because humans remember words better than numbers which is why we use URL's instead of IP's.

字符串更容易使用,因为人们记住单词比数字更好,这就是我们使用URL而不是IP的原因。

#3


0  

As others have mentioned, integers have a performance benefit. Whether it's enough to matter is another question.

正如其他人所提到的,整数具有性能优势。是否足够重要是另一个问题。

I would suggest doing whatever feels natural to you in writing code and developing an API. This will make your code intuitive to yourself (and others if applicable) and make you more productive in development.

我建议你在编写代码和开发API时做任何自然的事情。这将使您的代码对您自己(以及其他适用的)更直观,并使您在开发中更高效。

#1


0  

In the past when storage space was costly there were reasons to store strings as numbers. In this day and age except for embedded applications (which should probably not be running rails) there is not a need, especially given your example database size of 2000 users.

在过去,当存储空间昂贵时,有理由将字符串存储为数字。在这个时代,除了嵌入式应用程序(可能不应该运行rails)之外,没有必要,特别是考虑到您的示例数据库大小为2000个用户。

Also, if you store your strings as a number you will have the following downsides:

此外,如果您将字符串存储为数字,则会有以下缺点:

  • Increased code complexity
  • 代码复杂性增加
  • Database that can't be "read" without the code (Having done some database ports and written rails as a front end for back end data analysis these both become important.)
  • 没有代码就无法“读取”的数据库(将一些数据库端口和写入的轨道作为前端进行后端数据分析这些都变得很重要。)

For performance concerns make sure you have your database keys setup correctly.

对于性能问题,请确保正确设置数据库密钥。

#2


0  

Integers work better on the database level but make sure you remember what each of the numbers stand for.

整数在数据库级别上工作得更好,但要确保记住每个数字代表的含义。

Strings are much easier to work with because humans remember words better than numbers which is why we use URL's instead of IP's.

字符串更容易使用,因为人们记住单词比数字更好,这就是我们使用URL而不是IP的原因。

#3


0  

As others have mentioned, integers have a performance benefit. Whether it's enough to matter is another question.

正如其他人所提到的,整数具有性能优势。是否足够重要是另一个问题。

I would suggest doing whatever feels natural to you in writing code and developing an API. This will make your code intuitive to yourself (and others if applicable) and make you more productive in development.

我建议你在编写代码和开发API时做任何自然的事情。这将使您的代码对您自己(以及其他适用的)更直观,并使您在开发中更高效。