Mysql Bigint VS Varchar

bra*_*r19 12 mysql sql

索引字段和硬盘/ RAM上的空间令牌最好的是什么?Biginteger或Varchar(15)?我可以有这样的索引号:

from  10000001 to 45281229703 and higher...
Run Code Online (Sandbox Code Playgroud)

但更好的选择是什么?同样在非索引字段上哪种字段类型更好?

Mik*_*rov 31

BIGINT总是8个字节,VARCHAR(15)取决于值长度是1..16个字节,因此BIGINT在大型numnbers上需要更少的内存,但在小数字上需要更多的内存(短于7个数字).此外,BIGINT更快.


小智 11

我们在模拟环境中运行测试.

  1. 创建了1个BIGINT和1VARCHAR参数.
  2. 插入30Lac行.
  3. 在两个字段上创建索引.
  4. 结果是:BIGINT几乎比VARCHAR 响应快20倍.

以下是执行上述步骤的脚本:

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’;
Run Code Online (Sandbox Code Playgroud)