简体   繁体   中英

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.

I currently have some 7 digit long numbers in an INT(10) column which are ordered using 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 .
So either if you have 1 digit numbers or 10 digit numbers the performance should be the same.

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)

UPDATE if you what to speed up the ORDER BY part you can add a INDEX on that column

In some cases, MySQL can use an index to satisfy an ORDER BY clause without doing any extra sorting.
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

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. For example, INT(4) specifies an INT with a display width of four digits. 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.

I don't think that INT(10) affects performance because that type is built to use padding for the values when are returned. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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