SQL SELECT Speed Int与varchar [英] SQL SELECT speed int vs 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
字段在2至8个字节之间占据 ,通常4个绰绰有余(-2147483648至+2147483647) - 字符类型占用 4个字节加实际字符串.
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屋!