77

I used to work with the mysql gem, but often nowadays I hear about the mysql2 gem. Also, it is included in the Gemfile by default in Rails 3.0.x.

What is the difference and advantages of using the mysql2 gem?

Jai Chauhan
  • 4,035
  • 3
  • 36
  • 62
fl00r
  • 82,987
  • 33
  • 217
  • 237
  • "included into Gemfile by default"? I don't see that in 3.2.8 – KMC Sep 20 '12 at 02:58
  • @KMC, year ago it was included? :) – fl00r Sep 20 '12 at 08:28
  • I too am after a differences list, I hit one diff that was answered elsewhere re `each_hash` being removed. It would be nice to know what other changes I should search my code base for. – Ian Vaughan Apr 09 '13 at 11:23

2 Answers2

83

Here's a quote from the mysql2 gem page:

Yeah… but why?

Someone: Dude, the Mysql gem works fiiiiiine.

Me: It sure does, but it only hands you nil and strings for field values. Leaving you to convert them into proper Ruby types in Ruby-land - which is slow as balls.

Someone: OK fine, but do_mysql can already give me back values with Ruby objects mapped to MySQL types.

Me: Yep, but its API is considerably more complex and can be ~2x slower.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
ALoR
  • 4,904
  • 2
  • 23
  • 25
12

mysql2 is very much faster than the mysql gem. Also, apart from being effective, it also has more features and is more fun, just like Ruby on Rails.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Nikhil
  • 1,268
  • 2
  • 13
  • 29
  • 8
    More features? Last I checked it can't even do prepared statements! – wmarbut Nov 30 '12 at 16:33
  • I am also surprised there is no prepared statement support, although it looks like it's almost there as of [five days ago](https://github.com/brianmario/mysql2/pull/289). – twelve17 Jul 30 '13 at 00:11