Bigger than a char but smaller than a blob - mysql

Char's are great because they are fixed size and thus make for a faster table. They are however limited to 255 characters. I want to hold 500 characters but a blob is variable length and that's not what I want.
Is there some way to have a fixed length field of 500 characters in MySQL or am I going to have to use 2 char fields?

I would suggest using a varchar(500). Even though varchar isn't a fixed length, the database should reserve the correct amount of space. You shouldn't notice any performance difference using varchar(500) over 2xchar(255).
You're also probably going to cause extra overhead by joining two char fields together.

I would suggest using a varchar(500)
... if you have MySQL 5.0.3 or higher. In previous versions, VARCHAR was restricted to 255 characters.
Also, CHAR and VARCHAR do not work the same regarding trailing spaces. Be sure to read 10.4.1. The CHAR and VARCHAR Types (this is for MySQL 5.0).

You're worrying too much about internal implementation details. Don't pre-optimize.
Go with VARCHAR(500)

Related

MySQL data type for custom GUID [duplicate]

I have a small social networking website, with posts and comments.
I decided to let users create posts with any number of charŲ³ they want, and I think the best Datatype for storing such things is Text.
But for comments, in most cases users write only one or a few lines of text. That's why I think that I have to limit comment string length to something like 3000 or even 6000 chars maximum.
So, what Datatype should I use for my comments ?
Varchar or Text?
There are various places where VARCHAR and TEXT do or do not differ. Some things I say:
If the maximum length is more than 512 characters, go with TEXT.
Never use TINYTEXT -- it has only negatives relative to VARCHAR(255).
Don't use VARCHAR(255); pick a reasonable max instead of 255. (This is a minor optimization relating to temp table in complex queries.)
Use CHAR only for things that are truly fixed length. In almost all such case, tack on CHARACTER SET ascii (or latin1). (Else it will take more space than you expect.
Use CHARACTER SET ut8mb4. (Exceptions are becoming more an more rare.)
(Sorry, I digress. Back on topic...)
I indexing, in layout of InnoDB rows (there are 4 different ROW_FORMATs), etc, VARCHAR(513) will be essentially indistinguishable from TEXT.
One of the few arguments for VARCHAR is that it limits the store to the length given. But how often is that important?
Values in VARCHAR columns are variable-length strings. The length can be specified as a value from 0 to 65,535, which would seem big enough to handle your user's expected comment length.
The four TEXT types are TINYTEXT, TEXT, MEDIUMTEXT, and LONGTEXT. The maximum amount of data that can be stored for each data type is approximately:
TINYTEXT 256 bytes
TEXT 65,535 bytes ~64kb
MEDIUMTEXT 16,777,215 bytes ~16MB
LONGTEXT 4,294,967,295 bytes ~4GB
See also When to use TEXT in mysql instead of VARCHAR
Use one of the four type of TEXT available [TINYTEXT, TEXT, MEDIUMTEXT, and LONGTEXT] depending on your requirements. http://dev.mysql.com/doc/refman/5.7/en/storage-requirements.html

What is the difference between varchar(3) and varchar(300)

We know that varchar is a variable length data type then what is the difference between varchar(3) and varchar(300) in mysql ???
In SQL varchar is a string which varies in length. Traditionally, you specify an upper limit for this string. Here are some things to know about varchar:
Strings which are shorter than the specified limit do not take extra space: they only take up the required space.
If the string is longer than the limit, the whole record is rejected, both when you try to INSERT a record, and when you try to UPDATE a record.
Traditionally the upper upper limit was 255 characters. Modern databases no longer have this limit.
Some databases, such as PostgreSQL recommend that you no longer specify the length this way. Instead they recommend an unlimited string (varchar without the length) and limit it using a CHECK CONSTRAINT instead.
Most modern databases handle strings much more efficiently than in the past, so there is less need to be restrictive about the size of the string.
The short answer to your question is that both varchar(3) and varchar(300) are limited-length strings, and both will allow shorter strings without penalty. However clearly one is much shorter than the other.
In addition to #Manngo's answer, varchar(300) has an extra byte of overhead than varchar(3) because its max is over 255. From the MySQL docs...
In contrast to CHAR, VARCHAR values are stored as a 1-byte or 2-byte length prefix plus data. The length prefix indicates the number of bytes in the value. A column uses one length byte if values require no more than 255 bytes, two length bytes if values may require more than 255 bytes.
Basically, the length of the string must be stored. 1 byte can only hold 0 to 255, so 2 bytes are required to store a length that can go up to 300.
Suppose I want to insert a single character in my column in that scenario what is difference between both ?? Why we use varchar(1) why not varchar(100)??
If the column is only supposed to ever store a single character, use char(1), a single byte, to ensure the integrity of the data (varchar(1) is two bytes). That ensures anyone using the column will only ever get what they expect and don't have to do their own data validation.
Since the size of a varchar is only a max, specifying a smaller one won't make the table use any less disk (except as noted above about >255). Avoid adding arbitrary limits and business rules to your columns. For example, if you're going to store a URL or email address, there's little reason not to allow varchar(255). Limits based on business rules and UX concerns should be handed at a higher layer and not limited by the database schema.

Need help in MySQL DB Design (minimizing the overhead)

I am trying to design a mysql DB
but dunno what to do to minimize the overhead to a minimum, I have uniq needs.
one of the fields could be as long as 60kb or short as 100 bytes, what kind and lenghth should I use in this field to minimize the overhead to its minimum ?
I've heard if you define the maximum to be like 60k then every unused space left in the row till 60k will be filled with space, you understand that this could cause unnecessary overhead, only few raws would make use of this lenghth while most of the rest rows won't, what do you suggest ??
In MySQL, VARCHAR stores compactly on disk, that is it stores only the string used on a given row, plus a byte or two to encode the length of that string. This is true for both data and indexes.
But once the VARCHAR is loaded out of the storage engine and into memory, it is padded out to its full length. This consumes a lot of memory needlessly if you declare VARCHAR(65535). Then that padded-out representation may end up on disk during sorting or temporary table operations.
So use TEXT. This data type doesn't get padded out in memory like VARCHAR does, and it also supports strings up to 64KB.
If you need longer strings, use MEDIUMTEXT which supports up to 16MB.
Use VARCHAR.
Values in VARCHAR columns are variable-length strings. The length can
be specified as a value from 0 to 255 before MySQL 5.0.3, and 0 to
65,535 in 5.0.3 and later versions.
one of the fields could be as long as 60kb or short as 100 bytes
Sounds like a BLOB/TEXT. As Andreas has pointed out, you could even use VARCHAR if you know for sure you won't need more than 64K.
I've heard if you define the maximum to be like 60k then every unused space left in the row till 60k will be filled with space
If you use CHAR, the yes: it stores padding.
only few rows would make use of this length while most of the rest rows won't
That's incorrect. Modern DBMSes store variable-length rows: only the space actually needed will be actually used. Just use BLOB/TEXT or VARCHAR and you'll be fine.

MYSQL VARCHAR or CHAR for fixed length string

If I know a value being stored in MySQL is always going to be exactly 32 characters, is it better performance to make the column type CHAR instead of VARCHAR? What exactly is the performance difference between using VARCHAR and CHAR?
Thanks.
I would use CHAR.
If you are likely to search on the column, CHAR presents a small performance upgrade over VARCHAR.
Since your data size is going to be fixed, there is no downside to using CHAR, as an VARCHAR equivalent will store anywhere from one to two bytes as a prefix.
Reference: MySQL CHAR vs VARCHAR
The answer to your first question, "is it better performance to make the column type CHAR instead of VARCHAR?"... is yes. Definitely.
If you ALWAYS know the length is going to be 32, then you should definitely use CHAR.
Several good answers were also given here:
Why would I ever pick CHAR over VARCHAR in SQL?
VARCHAR columns use a one or two byte prefix to store the length:
In contrast to CHAR, VARCHAR values are stored as a one-byte or two-byte length prefix plus data. The length prefix indicates the number of bytes in the value. A column uses one length byte if values require no more than 255 bytes, two length bytes if values may require more than 255 bytes.
So, a VARCHAR column will be very slightly larger. If you know it's always going to be exactly 32 characters, then CHAR seems reasonable. In pretty much any case where you're not completely sure of the length, I'd go with VARCHAR.
If the string will always be 32 chars (probably you're having an md5 hash) then go for Char(32)
You can always run this script against any table and see what mysql has to say
SELECT * FROM table PROCEDURE ANALYSE();
This will analyze your entire rows and give you some suggestions. Works well only if you have a decent number or rows in your table.

Is it wise to declare a VARCHAR with a value greater than 255 in MySQL?

I just noticed in the documentation that in versions greater than 5.0.3 of MySQL you can declare varchar's with larger values than 255. In the past I've switched datatypes for anything larger than 255 but I'm wondering if it's better practice now to define larger string values using varchar(1000) or whatever length is appropriate.
Is this common with other databases now as well, or is it best to stick with 255 as the max value and change datatypes above that?
As the answer #Eric pointed out suggests, VARCHARs are stored in table while TEXTs are stored in a separate file - the only truly important point that you have to keep in mind when designing a table structure is the row size limitation (MySQL limits each row / record to 65 KB).
I suggest you use VARCHARs for "one-liners" - anything that has a text input as its data source.
In my opinion, I would discourage to approach. When you need more than 255 characters, use TEXT are some more suitable.
Update: VARCHAR is now limited to 65535 bytes, but a row in MySQL cannot contain more than 65535 bytes.
You have to know that VARCHAR and fields like that are stored directly into your database when TEXT for example will be stored outisde the row why a pointer inside the row linking to it.
So if you want to use big VARCHAR, make sure they will not be too big and won't interfere with the rest of the data in the row.
For example, having mutltiple VARCHAR fields that can contains up to 65K char would be a bad idea.
The VARCHAR column is limited to 65,535 bytes, which doesn't always mean 65,535 characters depending on which character set you are using.
If your using the latin1 character set which is one byte per character you won't run into any issues because the length of the string in the same as the amount of storage needed.
If you use a character set that stores multi-byte characters you can only set the length to be what the character set will allow. For instance the utf8 character set can have a maximum length of 21,844 characters.