1

I have a user table structured like this:

  • id MEDIUMINT(7),
  • username VARCHAR(15)

Would it technically be faster if I changed it to this instead:

  • id MEDIUMINT(5),
  • username VARCHAR(15)

I'm confused because even though the total row length in terms of characters and digits would be shorter, I assume the number of bytes used would be the same.

4 Answers 4

5

I'm confused because even though the total row length in terms of characters and digits would be shorter, I assume the number of bytes used would be the same.

You're correct - the number of digits specified does not change the number of bytes that a MEDIUMINT column will use to hold the value. Effectively, there's no speed performance.

Reference:

Sign up to request clarification or add additional context in comments.

Comments

3

The argument after MEDIUMINT makes no difference to its storage or the range of values it supports. It's only a hint for display width. Mostly this is relevant only when you use the ZEROFILL option.

CREATE TABLE foo (num MEDIUMINT(7) ZEROFILL);
INSERT INTO foo VALUES (1234);
SELECT num FROM foo;

0001234

MEDIUMINT is always three bytes.

Comments

1

No, it would be no faster. The number after MEDIUMINT is just the display width. It affects only the way in which query results are displayed in some contexts.

Comments

0

It's just a display width. If you use MEDIUMINT(5), it does not use small resource than just using MEDIUMINT. Thanks you for your question. I have a same question.

Reference: 12.2.5 Numeric Type Attributes

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.