Django有SmallIntegerField的原因是什么? [英] What's the reason why Django has SmallIntegerField?

查看:1053
本文介绍了Django有SmallIntegerField的原因是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很想知道为什么它被提供。该字段是数据库依赖的,这不会使它完全不可靠吗?



我想在一个模型中存储出生年份,有点像

  class Person(models.Model):
name = models.CharField(max_length = 256)
born = models.IntegerField )

当然这需要很少的空间,它应该总是4字符长,所以一个PositiveSmallIntegerField可能适合,但为什么我应该选择它而不是正常的IntegerField?

解决方案

许多RDBM的性能可能严重依赖行大小。虽然纯粹主义的方法可能会说应用程序应该完全独立于底层数据结构,但是像使用较小的整数一样,许多行的改进可能会使表格大小刮掉千兆字节,这使得更多的表适合内存,这大大提高了性能。这是 ABCs 的 B a>。



我将使用一个这样的小整数来表示一个总是具有< 100行的表的主键,特别是当它存储为表中的外键我希望增长非常大。虽然大小是实现定义的,但至少可以假定它大于127。


I'm wonder why it's provided. The field is database dependent, doesn't that make it totally unreliable to use?

I want to store birth year in a model, kinda like

class Person(models.Model):
  name = models.CharField(max_length=256)
  born = models.IntegerField()

Of course this requires very little space, it should always be 4 "characters" long, so a PositiveSmallIntegerField would maybe fit, but why should I choose it instead of normal IntegerField?

解决方案

Performance on many RDBMs can be heavily dependent on row size. While a "purist" approach might say that the application should be completely independent of the underlying data structure, over many rows improvements like using a smaller integer could shave gigabytes off table size, which makes more of the table fit in memory, which drastically improves performance. It's the Brief part of the ABCs.

I would use a small integer like this for say, a primary key on a table that would always have <100 rows, especially when this is stored as a foreign key in a table I expect to grow very large. While the size is implementation defined, it's safe to assume that it is greater than 127 at the very least.

这篇关于Django有SmallIntegerField的原因是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆