SQL SELECT Speed Int与varchar [英] SQL SELECT speed int vs varchar

查看:129
本文介绍了SQL SELECT Speed Int与varchar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建表,这让我感到惊讶.

I'm in the process of creating a table and it made me wonder.

如果我存储有品牌的汽车(例如宝马,奥迪等),那么如果我将品牌存储为int或varchar会对查询速度产生任何影响.

If I store, say cars that has a make (fx BMW, Audi ect.), will it make any difference on the query speed if I store the make as an int or varchar.

也是

SELECT * FROM table WHERE make = 5 AND ...;

更快/更慢

SELECT * FROM table WHERE make = 'audi' AND ...;

还是速度会大致相同?

推荐答案

Int比较比varchar比较快,原因很简单,即int比varchars占用更少的空间.

Int comparisons are faster than varchar comparisons, for the simple fact that ints take up much less space than varchars.

这适用于未索引访问和索引访问.最快的方法是建立索引的int列.

This holds true both for unindexed and indexed access. The fastest way to go is an indexed int column.

正如我看到的那样,您已经标记了问题postgreql,您可能会对不同日期类型的空间使用感兴趣:

As I see you've tagged the question postgreql, you might be interested in the space usage of different date types:

  • int fields occupy between 2 and 8 bytes, with 4 being usually more than enough ( -2147483648 to +2147483647 )
  • character types occupy 4 bytes plus the actual strings.

这篇关于SQL SELECT Speed Int与varchar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆