Does anyone have considerable proof that CHAR is faster than VARCHAR?

后端 未结 4 1844
深忆病人
深忆病人 2020-12-16 18:48

Any benchmark, graph anything at all ? Its all academic and theoretical across the web.

Ok its not the first time that this question has been asked, they all say th

4条回答
  •  春和景丽
    2020-12-16 19:25

    CHAR will be faster as it is fixed length. For example CHAR(10) and VARCHAR(10) CHAR(10) is a fixed-length string of 10 while VARCHAR is a variable-length string with maximum length of 10.

    VARCHAR(10)

     {Index} (Length) [String]
     -------------------------
     {0} (8) [AAAAAAAA]
     {1} (5) [BBBBB]
     {2} (3) [CCC]
     {3} (7) [DDDDDDD]
     {4} (2) [EE]
     {5} (4) [FFFF]
    

    CHAR(10)

     {Index} (Length) [String]
     -------------------------
     {0} (10) [AAAAAAAA  ]
     {1} (10) [BBBBB     ]
     {2} (10) [CCC       ]
     {3} (10) [DDDDDDD   ]
     {4} (10) [EE        ]
     {5} (10) [FFFF      ]
    

    So imagine you have a table with 1,000,000 records and you need to get a record at offset 500,000.

    CHAR - database engine would have to do multiply 500,000 x 10 = offset is 5,000,000.

    VARCHAR - database engine would have to get each row length and sum them all 5 + 8 + 9 + 3 + 2 + 4... 500,000 times to get offset

    CHAR padding is x00 which basically is end of the string so CHAR(10) = [A,A,A,A,A,00,00,00,00,00] is "AAAAA".

提交回复
热议问题