简体   繁体   English

使用order by时,数字的长度是否会影响mysql的性能?

[英]Does the length of a number affect performance in mysql when using order by?

Does the length of a number stored in an INT(10) column affect performance when using order by. 使用order by时,存储在INT(10)列中的数字的长度是否会影响性能。

I currently have some 7 digit long numbers in an INT(10) column which are ordered using order by. 我目前在INT(10)列中有一些7位长数字,这些数字使用order by进行排序。

If ordering large numbers brings down performance, I could try something else. 如果订购大量产品会降低性能,我可以尝试其他方法。 Do they slow things down? 他们会减慢速度吗?

No, it doesn't affect performance since INT type has fixed length of 4B . 不,因为INT类型的固定长度为4B ,所以它不会影响性能。
So either if you have 1 digit numbers or 10 digit numbers the performance should be the same. 因此,无论您是1位数字还是10位数字,性能都应该相同。

Also it is a general misinformation regarding INT(N) N represent the number of digits to display , in other words INT(1) is the same size as INT(10) 也是关于INT(N)的一般错误信息N表示要显示的位数,换句话说INT(1)的大小与INT(10)大小相同

UPDATE if you what to speed up the ORDER BY part you can add a INDEX on that column UPDATE,如果你有什么要加快ORDER BY一部分,你可以添加一个INDEX该列

In some cases, MySQL can use an index to satisfy an ORDER BY clause without doing any extra sorting. 在某些情况下,MySQL可以使用索引来满足ORDER BY子句,而无需进行任何额外的排序。
The index can also be used even if the ORDER BY does not match the index exactly, as long as all of the unused portions of the index and all the extra ORDER BY columns are constants in the WHERE clause 即使ORDER BY与索引不完全匹配,也可以使用索引,只要索引的所有未使用部分和所有额外的ORDER BY列在WHERE子句中都是常量

DOCS DOCS

The number in brackets does not affect on the the range of possible values that can be stored in the field. 括号中的数字不会影响可存储在该字段中的可能值的范围。

MySQL supports an extension for optionally specifying the display width of integer data types in parentheses following the base keyword for the type. MySQL支持扩展,可以选择在整数类型的基本关键字之后的括号中指定整数数据类型的显示宽度。 For example, INT(4) specifies an INT with a display width of four digits. 例如,INT(4)指定显示宽度为四位数的INT。 This optional display width may be used by applications to display integer values having a width less than the width specified for the column by left-padding them with spaces. 应用程序可以使用此可选的显示宽度来显示整数值,该整数值的宽度小于为列指定的宽度,方法是用空格左键填充它们。 (That is, this width is present in the metadata returned with result sets. Whether it is used or not is up to the application.) (也就是说,此宽度存在于结果集返回的元数据中。是否使用它取决于应用程序。)

The display width does not constrain the range of values that can be stored in the column. 显示宽度不限制可以存储在列中的值的范围。 Nor does it prevent values wider than the column display width from being displayed correctly. 也不会阻止宽于列显示宽度的值正确显示。 For example, a column specified as SMALLINT(3) has the usual SMALLINT range of -32768 to 32767, and values outside the range permitted by three digits are displayed in full using more than three digits. 例如,指定为SMALLINT(3)的列通常具有-32768到32767的SMALLINT范围,并且使用三位以上的数字完整显示三位数所允许的范围之外的值。

I don't think that INT(10) affects performance because that type is built to use padding for the values when are returned. 我认为INT(10)不会影响性能,因为该类型是为在返回值时使用填充来构建的。 The point is that if you want to tune that column you should add an index. 关键是,如果要调整该列,则应添加一个索引。 That would affects performance. 会影响性能。 Int will always took the same footprint. Int始终会占用相同的空间。

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

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