Answer the question
In order to leave comments, you need to log in
Storing typed data in a database
Hello, maybe the title is not clear enough, but I did not know how best to indicate the essence of my question. And here she is. Quite often, the database has to store data that belongs to a certain type. Well, for example, an article can be both news and a blog post. In this case, we create a field in the database that is responsible for the type of object. So, does it make sense to assign an id to each type and insert into the database not the word "blog", but the corresponding "1". It is obvious that the word is clearer and more convenient, but what will be more profitable if there are millions of records?
Answer the question
In order to leave comments, you need to log in
The number is indexed better, for understanding it is necessary to indicate comments to the columns and to the table, and in the code to define readable constants for such fields
There is not much point in such an optimization. It will only affect the size of the files on the disk, it will not affect the size of the index.
If the same table has text fields, then the difference will be completely invisible.
Those. do you want to optimize the "record type" column, which takes several bytes (and can actually store an int there if the column is enum), while the adjacent column is of type text with a variable length of the order of several kilobytes? Well, yes, if you're lucky, you'll win some fractions of a percent.
To be honest, I don’t know how it is implemented inside MySQL, but in Oracle there is such a situation that integer is far from one byte (and can reach up to 9, as far as I remember, though Oracle’s supported bit depth is also impressive).
As an example to think about:
CREATE TABLE TEST (ID INTEGER, vc VARCHAR2(10 CHAR));
INSERT INTO TEST VALUES (1,'1');
SELECT DUMP(ID),
DUMP(vc)
FROM TEST;
DUMP(ID) DUMP(VC)
1 Typ=2 Len=2: 193.2 Typ=1 Len=1: 49
Pay attention to the lengths.
And short strings are indexed for unique entries, as a rule, not worse, I disagree. It's another matter if you plan to use range predicates... This is where "moments" may arise, but I think then you wouldn't be asking your question.
Of course, all of the above is primarily applicable to Oracle, but I think that the direction of thought “to check” is understandable, but you can’t trust anyone anyway :)
Let's just think together. First, it is not necessary to use full int - how many types of articles do you have? TinyInt USIGNED - won't override? Secondly, if you have a varchar string, what is the length? If there are four characters, you may not even notice. And above - imnsho, you need to use a number.
I also assume with a high probability that you will need a selection - and not only for this field, but rather an index for several. Also, size will be important here.
Obviously, the word is clearer and more convenient, but what will be more profitable
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question