Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mysql Bigint VS Varchar

Tags:

sql

mysql

What is the best for index field, and space token on hard disk/RAM? Biginteger or Varchar(15) ? I can have for example such index number:

from  10000001 to 45281229703 and higher...

But what is better to choose? Also on non-indexing field what field type is better?

like image 639
brabertaser19 Avatar asked Mar 06 '13 07:03

brabertaser19


People also ask

When should I use BIGINT in MySQL?

The number is used to display width. BIGINT takes 8 bytes i.e. 64 bits. The signed range is -9223372036854775808 to 9223372036854775807 and unsigned range takes positive value. The range of unsigned is 0 to 18446744073709551615.

Does BIGINT take up more space?

Storage Size (INT VS BIGINT) If we talk about the storage size of INT and BI GINT, the storage size of INT is 4 bytes while the storage size of BIGINT is double of that i.e.: 8 bytes.

Can BIGINT store characters?

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.

What does BIGINT mean in MySQL?

BIGINT is the MySQL data type that can be assigned to the columns of the table in which we want to store the whole numbers and we are aware that the range of the numbers that we will store in that column will be huge and not exceed the range of the BIGINT data type.


2 Answers

BIGINT is always 8 bytes, VARCHAR(15) is 1..16 bytes depending on value length, so BIGINT needs less memory on large numbers, but more memory on small numbers (shorter than 7 digits). Also, BIGINT is faster.

like image 112
Mikhail Vladimirov Avatar answered Oct 13 '22 03:10

Mikhail Vladimirov


We have run test in simulation environment.

  1. Created able with 1 BIGINT and 1VARCHAR parameter.
  2. Inserted 30Lac rows.
  3. created index on both fields.
  4. Result is : BIGINT respond almost 20time's faster than VARCHAR.

Here is script to perform above steps:

Create table r5(mob bigint,m_mob varchar(30));

Create index i_d on r5(mob,m_mob);


do $$
begin
for i in 1..3000000 loop
insert into r5(mob,m_mob) values(i,i||’abc’);
end loop;
end; $$

select * from r5
where mob=2900000;

select * from r5
where m_mob=’2900000abc’;
like image 31
Mohit M Avatar answered Oct 13 '22 02:10

Mohit M