Rails NULL Encoding of MySQL Results

If you are not careful, the encoding that you set in database.yml may bring some ill-effects. The encoding: utf8 causes ActiveRecord connection to execute set names = 'utf8' in the MySQL session. This tells MySQL server that the this particular session sends and want to receive everything in utf8 character set.

Even the column stored in ‘latin1’ is converted to ‘utf8’. This takes a considerable hit on the performance, I also have a suspicion that MySQL does not cache results when it is forced to do the conversion.

How to avoid

1. Store data in correct encoding as it needs.

2. By telling MySQL to not convert the results by initaliazing rails with…

ActiveRecord::Base.connection.execute('SET CHARACTER_SET_RESULTS = NULL')

Leave a Comment