Mybatis and SQL Server type conversion encountered pit

A. MyBatis SQL statement performance problems encountered

1. Restore scene

  Suppose we have a User table containing userId, userName, gender field, which userId data type char (20) , this time we want to get the person's name by userId,

  This SQL is very simple: the SELECT dbo.User the userName the FROM (NOLOCK) the WHERE the userId = '100'

2. Description of the problem

  Above this simple SQL statement conceals a very serious performance problem: When MyBatis to generate the statement, and execute SQL Server, a JDBC Type parameter userId is nvarchar (4000) , but the table userId data type char (20) , and therefore there must be cast.

  In the stress test scenario, or call the frequent cases, lead to SQL Server CPU seriously overweight, as well as service throughput serious decline.

Two. Char, varchar, nvarchar difference

  • char: the English alphabet is 1 byte, 2 bytes for the characters, char belonging to the fixed-length type data structure, all the remaining space filled with spaces, so efficiency index high.
  • varchar: do not use spaces to fill the extra space, the actual length of string length + 1 , 1 represents the length of the string.
  • nvarchar: all characters occupy two bytes , regardless letters or characters. To solve the multi-character set conversion issues between, N for Unicode .

III. Reference

Guess you like

Origin www.cnblogs.com/YaoFrankie/p/11441456.html