Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
291 views
in Technique[技术] by (71.8m points)

What's the reason why Django has SmallIntegerField?

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?

question from:https://stackoverflow.com/questions/1516610/whats-the-reason-why-django-has-smallintegerfield

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...