I have question about the best solution to store number code
(example: 1234123412341234).
This code have always 16 chars and is numeric value!
I use InnoDB engine to store table
Currently i have type of column VarChar(16) but i have thoughts whether bigint is better for this.
Column code is not edited after added to database.
Bigint requires 8 Byte to store a number, while varchar(16) will require at minimum the double amount of Bytes. Depending on the character set used, even 4 times more. So type of bigint will save space on your disk but also reduce network traffic.
A varchar(16) will not be able to represent all bigint numbers. An unsigned bigint value of 0xFFFFFFFFFFFFFFFFFE can't be stored in a varchar(16) column.
On machine Level, a 16 Byte comparison usually happens in one cycle, while comparing strings is much more expensive, since varchar requires a byte to byte comparison.
You should choose the datatype based on the data you have.
For example a credit card number is a character string (even if it contains numbers), likewise a telephone number is a character string (as string can for example have leading zeros etc).
A number with numeric value should be stored as a number (a distance the to moon for example).
The key question to ask is what operations do you need to do on the data?
A numeric data type like BigInt can be used with operations like addition, subtraction, multiplication, division. Values will be normalised so that 1, 001, 1.0, etc will all be stored the same.
A string data type like VarChar can be used with operations like sub-string matches. It can store values like 0001 without normalising them.
It is very rare that one piece of data needs both those types of operations. For instance, you might want to look for all phone numbers beginning "1800", but would never need to divide a phone number by 3.
So when you say your data is a "number code", you need to think about whether this value is actually a number, or just a string which is all digits.
Related
What data type should use in a MySQL database to store 2 text files of code. If I intend to compare similarity later.
It's a MySQL database running on my Windows machine.
Also can you recommend an API that can compare code for me.
As per MySQL documentation
Values in VARCHAR columns are variable-length strings. The length can be specified as a value from 0 to 65,535. The effective maximum length of a VARCHAR is subject to the maximum row size (65,535 bytes, which is shared among all columns) and the character set used.
...
Values in CHAR and VARCHAR columns are sorted and compared according to the character set collation assigned to the column.
So, VARCHAR is stored inline with the table, whilst BLOB and TEXT types are stored off the table with the database holding the location of the data. Depending on how long your text is, TEXT might be defined as TINYTEXT, TEXT, MEDIUMTEXT, and LONGTEXT, the only difference is the maximum amount of data it holds.
TINYTEXT 256 bytes
TEXT 65,535 bytes
MEDIUMTEXT 16,777,215 bytes
LONGTEXT 4,294,967,295 bytes
To compare the two strings stored in TEXT (or any other string column) you might want to use STRCMP(expr1,expr2)
STRCMP() returns 0 if the strings are the same, -1 if the first argument is smaller than the second according to the current sort order, and 1 otherwise.
If you specify the desired output of the comparison, I might edit the answer.
EDIT
To compare two strings and calculate the difference percentage, you might want to use similar_text. As the official documentation states:
This calculates the similarity between two strings as described in Programming Classics: Implementing the World's Best Algorithms by Oliver (ISBN 0-131-00413-1). Note that this implementation does not use a stack as in Oliver's pseudo code, but recursive calls which may or may not speed up the whole process. Note also that the complexity of this algorithm is O(N**3) where N is the length of the longest string.
I have a database for an online game, and i need to store total score of user playing game. Each time user end a match, his score will be automatically updated.
I'm trying to keep this score as VARCHAR, then retrieve it from a script, parse to int, add last score, parse again to string, and update database. But as i can see, php is not able to manage big numbers. How can i do?
MySQL has BIGINT type. PHP itself works flawlessly with BIGINT.
Unless you want numbers way bigger than that, I have no idea why would you use anything else.
Note: unsigned BIGINT is twice as big as BIGINT
unsigned BIGINT can store numbers up to: 18,446,744,073,709,551,615
signed BIGINT can store numbers up to: 9,223,372,036,854,775,807 (or with 8 at the end on the negative side)
So basically, You can store numbers up to around 1,8*10^19. Need more? Then look at BC at the other answer here.
For comparison:
INT can store up to 2,147,483,647 or twice as much when unsigned, which means that BIGINT is roughly 4,294,967,295 times larger than INT. Yes. BIGINT is basically very close to INT^2
You need to use BC:
You can work with numbers as strings in PHP using BC, so they are arbitrary big, as you are already using in your database.
Best regards.
I plan to be storing strings that have a maximum size of 4,500 VarChar, but MOST of the entries will be under 200 characters. Is MySql smart enough to optimize?
My current solution is to use 5 tables, data_small, data_medium, data_large, etc and insert based on the length of the string. The other solution would be to save files to disk, which would mean a second hit to the database, but result in a smaller return.
MySQL would do fine as would most every RDBMS for that matter. When you specify a field as type CHAR() the number of characters is always used regardless of how many characters are in your string. For instance: If you have Char(64) field and you insert 'ABCD' then the field is still 64 bytes (assuming non-unicode).
When using VARCHAR(), however, the cell only uses as many bytes as are in the string, plus the number of bytes necessary to store the size of the string. So: If you have VARCHAR(64) and insert 'ABCD' you will only use 5 bytes. 4 for the characters 'ABCD' and one for the number of characters '4'.
Your extremely varying string lengths are exactly the reason we have VARCHAR(), so feel free to use VARCHAR(4500) and rest assured you will only be using as much space as necessary to store the characters in the string, and a little bit extra for the length.
Somewhat related: This is why it's not a great idea to use VARCHAR() for fields that don't have varying length strings being inserted. You are wasting space storing the size of the string when it's already known. For instance, telephone numbers of the form x-xxx-xxx-xxxx should just use Char(14) since it will always take up 14 characters and only 14 bytes are necessary. If you used VARCHAR(14) instead you would actually end up using 15 bytes.
Data type in php (that I should set for a column in phpmyadmin) for entering both character and decimal numbers. Ex. 0.09 or n.a. or 100 (1) all such things can be feed in to the column.
A VARCHAR type can be used. This will store a string allowing your characters or decimals. Be sure to set an appropriate length - not too short or the data will be truncated.
If you find yourself storing lots of different types of data in a single field, then it may suggest you could use a better table/db design.
I would personally store that data in two different fields:
one INT for whole numbers
and one DEC(m,n) for decimal numbers * m is number of all digits and n is number of digits behind the decimal separator.
For example: DEC(5,2) data for this would be 123.45
If you need it to be in one field use VARCHAR but this is not my recommend, since it is not good to store integers in varchar type field.
Though a varchar is not recommended, but if you are using a varchar make sure you typecast the variable in your code while fetching.
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Importance of varchar length in MySQL table
When using VARCHAR (assuming this is the correct data type for a short string) does the size matter? If I set it to 20 characters, will that take up less space or be faster than 255 characters?
Yes, is matter when you indexing multiple columns.
Prefixes can be up to 1000 bytes long (767 bytes for InnoDB tables). Note that prefix limits are measured in bytes, whereas the prefix length in CREATE TABLE statements is interpreted as number of characters. Be sure to take this into account when specifying a prefix length for a column that uses a multi-byte character set.
source : http://dev.mysql.com/doc/refman/5.0/en/column-indexes.html
In a latin1 collation, you can only specify up 3 columns of varchar(255).
While can specify up to 50 columns for varchar(20)
In-directly, without proper index, it will slow-down query speed
In terms of storage, it does not make difference,
as varchar stand for variable-length strings
In general, for a VARCHAR field, the amount of data stored in each field determines its footprint on the disk rather than the maximum size (unlike a CHAR field which always has the same footprint).
There is an upper limit on the total data stored within all fields of an index of 900 bytes (900 byte index size limit in character length).
The larger you make the field, the more likely people will try to use for purposes other than what you intended - and the greater the screen real-estate required to show the value - so its good practice to try to pick the right size, rather than assuming that if you make it as large as possible it will save you having to revisit the design.
The actual differences are:
TINYTEXT and other TEXT fields are stored separately from in-memory row inside MySQL heap, whereas VARCHAR() fields add up to 64k limit (so you can have more than 64k in TINYTEXTs, whereas you won't with VARCHAR).
TINYTEXT and other 'blob-like' fields will force SQL layer (MySQL) to use on-disk temporary tables whenever they are used, whereas VARCHAR will be still sorted 'in memory' (though will be converted to CHAR for the full width).
InnoDB internally doesn't really care whether it is tinytext or varchar. It is very easy to verify, create two tables, one with VARCHAR(255), another with TINYINT, and insert a record to both. They both will take single 16k page - whereas if overflow pages are used, TINYTEXT table should show up as taking at least 32k in 'SHOW TABLE STATUS'.
I usually prefer VARCHAR(255) - they don't cause too much of heap fragmentation for single row, and can be treated as single 64k object in memory inside MySQL. On InnoDB size differences are negligible.
In the documentation of MySQL:
http://dev.mysql.com/doc/refman/5.0/en/char.html
You have a table that indicates the bytes of a VARCHAR(4) (vs a CHAR(4)).
A simple VARCHAR(4) without string, only 1 byte. Then, a simple VARCHAR(255) without string is 1byte. A VARCHAR(4) with 'ab' is 3 bytes, and a VARCHAR(255) with 'ab' is 3 bytes. It's the same, but with the lenght limit :)
This will have no effect on performance. In this case the constraint merely helps ensure data integrity.
If you set it to 20, it will save only the first 20 characters. So yes, it will take up less space than 255 characters :).
The required storage space for VARCHAR is as follows:
VARCHAR(L), VARBINARY(L) — L + 1 bytes if column values require 0 – 255 bytes, L + 2 bytes if values may require more than 255 bytes
So VARCHAR does only require the space for the string plus one or two additional bytes for the length of the string.