简体   繁体   English

Mysql:字段大小/显示宽度是否影响索引性能?

[英]Mysql: Does field size/display width affect index performance?

CREATE TABLE student(
`id` int(11) auto_increment PRIMARY KEY
`grade` int(11)
)

Let's say I want to add an index on grade column.假设我想在grade列上添加索引。 Does it make a difference if it has a smaller display width, eg, int(4) ?如果它具有较小的显示宽度,例如int(4) ,它会有所不同吗?

EDIT:编辑:

  • By performance here I mean query time.这里的performance是指查询时间。

  • Also, not clear whether column display width affects index size.另外,不清楚列显示宽度是否影响索引大小。 We are concerning a very large table with at least millions of rows.我们正在考虑一个至少有数百万行的非常大的表。 It'd be great if the answer can shed lights on this.如果答案可以阐明这一点,那就太好了。

First, display makes no difference in any case--it's just for how the field will be represented in the query responses.首先,显示在任何情况下都没有区别——它只是关于字段在查询响应中的表示方式。 An int is still and int using 4 bytes, a bigint is a bigint using 8bytes etc...一个int仍然和int使用 4 个字节,一个bigint是一个使用 8bytes 的bigint等等......

From what aspect are you considering 'performance'?您从什么方面考虑“性能”? Overall request time, memory use needed to keep data and indexes loaded or cached?保持数据和索引加载或缓存所需的总请求时间、内存使用情况? Disk space?磁盘空间?

I'm guessing you're meaning, will it affect how fast the query responds back.我猜你的意思是,它会影响查询响应的速度。

This question however is pretty broad, the real answer is, it depends.然而,这个问题非常广泛,真正的答案是,这取决于。 Does is your system 64 or 32 bit?你的系统是64位还是32位? How many records are we talking?我们在谈论多少记录? Is the field part of a much larger composite index, but still a small portion of it?该字段是一个更大的复合索引的一部分,但仍然是它的一小部分吗?

(NOTE: need to be checked on this claim, like if CHARs are just hashed for indexes) Go from a or CHAR(4) to a CHAR(32) and sure you might find some non-negligible performance hit, but this is not due to complexity, but additional overhead of your OS and architecture dealing with these. (注意:需要对此声明进行检查,例如 CHAR 是否只是针对索引进行散列)从 a 或 CHAR(4) 转到 CHAR(32) 并确保您可能会发现一些不可忽略的性能影响,但这不是由于复杂性,但您的操作系统和架构处理这些的额外开销。

However, I'm going to go out on a limb and suggest, barring changing types (int to varchar) which may change the method for indexing or a massive change in storage size of your index, you'll probably not 'see' any difference.但是,我将大胆提出建议,除非更改类型(int 到 varchar),否则可能会更改索引方法或索引存储大小的大量更改,否则您可能不会“看到”任何区别。 I doubt between different integer types you'd be able to easily show consistent slowdown.我怀疑在不同的整数类型之间您是否能够轻松地显示一致的减速。

Short answer: (4) means nothing for INT .简短回答: (4)INT没有任何意义。

Loooong answer:龙哥 回复:

Column sizes impact the size of rows, which impacts the size of the table, which impacts the speed of queries.列大小影响行大小,从而影响表大小,进而影响查询速度。 But...但...

If the table is 'small', there will be very little difference in performance.如果表“小”,则性能差异很小。

If the table is bigger than can be cached in RAM, then the difference could be significant -- because you would probably be I/O-bound.如果该表大于 RAM 中可以缓存的大小,则差异可能很大——因为您可能会受到 I/O 限制。 In some situations, this is a ten-fold slowdown.在某些情况下,这是十倍的减速。

To shrink an INT , which is 4 bytes always , switch to TINYINT UNSIGNED (1 byte, range: 0..255), SMALLINT UNSIGNED (2 bytes, 0..65K), or MEDIUMINT UNSIGNED (3 bytes, 0..16M).缩小始终为 4 个字节的INT ,请切换到TINYINT UNSIGNED (1 个字节,范围:0..255)、 SMALLINT UNSIGNED (2 个字节,0..65K)或MEDIUMINT UNSIGNED (3 个字节,0..16M) )。

Assuming grade is 0..100, then TINYINT (signed or unsigned) is optimal.假设grade是 0..100,那么TINYINT (有符号或无符号)是最佳的。

Meanwhile, you could change your change your id .同时,您可以更改您的更改id

The only purpose of INT(4) is in conjunction with ZEROFILL , where you want to display 12 as 0012 . INT(4)唯一目的是与ZEROFILL结合使用,您希望将 12 显示为0012 That is extremely rare.这是极其罕见的。

Don't use CHAR unless the strings are really fixed length strings .除非字符串是真正固定长度的字符串,否则不要使用CHAR And then it probably should be explicitly declared CHARACTER SET ascii because it is hex, all digits, or a 2-letter country_code (etc).然后它可能应该明确声明为CHARACTER SET ascii因为它是十六进制、所有数字或 2 个字母的 country_code(等)。 Anyway, utf8 is overkill.无论如何,utf8 是矫枉过正。

Assuming you are using InnoDB, the 'secondary' INDEX(grade) will implicitly include the PRIMARY KEY(id) .假设您使用的是 InnoDB,“次要” INDEX(grade)将隐式包含PRIMARY KEY(id) So the size of each index entry is the size of grade plus the size of id plus a bunch of overhead.所以每个索引条目的大小是grade的大小加上id的大小加上一堆开销。 Assuming normal grades and never more than 65K students, you could use 3 bytes instead of your original 8. But the table is small, so you are unlikely to be I/O-bound.假设成绩正常且学生不超过 65K,您可以使用 3 个字节而不是原来的 8 个字节。但表很小,因此您不太可能受到 I/O 限制。 Hence small overhead for 8 instead of 3.因此 8 而不是 3 的开销很小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM