2010-04-06 16:25:43 +00:00
= Mysql2 - A modern, simple and very fast Mysql library for Ruby - binding to libmysql
2010-04-04 20:14:58 +00:00
2010-04-05 05:42:00 +00:00
The Mysql2 gem is meant to serve the extremely common use-case of connecting, querying and iterating on results.
Some database libraries out there serve as direct 1:1 mappings of the already complex C API's available.
This one is not.
It also forces the use of UTF-8 [or binary] for the connection [and all strings in 1.9] and uses encoding-aware MySQL API calls where it can.
The API consists of two clases:
Mysql2::Client - your connection to the database
Mysql2::Result - returned from issuing a #query on the connection. It includes Enumerable.
2010-04-05 21:15:30 +00:00
== Installing
2010-04-06 15:54:30 +00:00
gem install mysql2
2010-04-07 16:59:37 +00:00
You may have to specify --with-mysql-config=/some/random/path/bin/mysql_config
2010-04-05 21:15:30 +00:00
2010-04-05 05:42:00 +00:00
== Usage
Connect to a database:
# this takes a hash of options, almost all of which map directly
# to the familiar database.yml in rails
2010-04-06 05:46:56 +00:00
# See http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/MysqlAdapter.html
2010-04-05 05:42:00 +00:00
client = Mysql2::Client.new(:host => "localhost", :username => "root")
Then query it:
results = client.query("SELECT * FROM users WHERE group='githubbers'")
Need to escape something first?
escaped = client.escape("gi'thu\"bbe\0r's")
results = client.query("SELECT * FROM users WHERE group='#{escaped}'")
Finally, iterate over the results:
results.each do |row|
# conveniently, row is a hash
# the keys are the fields, as you'd expect
# the values are pre-built ruby primitives mapped from their corresponding field types in MySQL
# Here's an otter: http://farm1.static.flickr.com/130/398077070_b8795d0ef3_b.jpg
end
Or, you might just keep it simple:
client.query("SELECT * FROM users WHERE group='githubbers'").each do |row|
# do something with row, it's ready to rock
end
How about with symbolized keys?
# NOTE: the :symbolize_keys and future options will likely move to the #query method soon
client.query("SELECT * FROM users WHERE group='githubbers'").each(:symbolize_keys => true) do |row|
# do something with row, it's ready to rock
end
2010-04-07 17:08:04 +00:00
== Async
Mysql2::Client takes advantage of the MySQL C API's (undocumented) non-blocking function mysql_send_query for *all* queries.
But, in order to take full advantage of it in your Ruby code, you can do:
client.query("SELECT sleep(5)", :async => true)
Which will return nil immediately. At this point you'll probably want to use some socket monitoring mechanism
like EventMachine or even IO.select. Once the socket becomes readable, you can do:
# result will be a Mysql2::Result instance
result = client.async_result
2010-04-07 17:47:16 +00:00
NOTE: Because of the way MySQL's query API works, this method will block until the result is ready.
2010-04-07 17:08:04 +00:00
So if you really need things to stay async, it's best to just monitor the socket with something like EventMachine.
2010-04-07 17:47:16 +00:00
If you need multiple query concurrency take a look at using a connection pool.
2010-04-07 17:08:04 +00:00
2010-04-15 20:35:42 +00:00
== ActiveRecord
To use the ActiveRecord driver, all you should need to do is have this gem installed and set the adapter in your database.yml to "mysql2".
That was easy right? :)
== EventMachine
The mysql2 EventMachine deferrable api allows you to make async queries using EventMachine,
while specifying callbacks for success for failure. Here's a simple example:
require 'mysql2/em'
EM.run do
client1 = Mysql2::EM::Client.new
defer1 = client1.query "SELECT sleep(3) as first_query"
defer1.callback do |result|
puts "Result: #{result.to_a.inspect}"
end
client2 = Mysql2::EM::Client.new
defer2 = client2.query "SELECT sleep(1) second_query"
defer2.callback do |result|
puts "Result: #{result.to_a.inspect}"
end
end
2010-05-16 07:13:18 +00:00
== Lazy Everything
Well... almost ;)
Field name strings/symbols are shared across all the rows so only one object is ever created to represent the field name for an entire dataset.
Rows themselves are lazily created in ruby-land when an attempt to yield it is made via #each.
For example, if you were to yield 4 rows from a 100 row dataset, only 4 hashes will be created. The rest will sit and wait in C-land until you want them (or when the GC goes to cleanup your Mysql2::Result instance).
As for field values themselves, I'm workin on it - but expect that soon.
2010-04-06 02:17:32 +00:00
== Compatibility
The specs pass on my system (SL 10.6.3, x86_64) in these rubies:
* 1.8.7-p249
* ree-1.8.7-2010.01
* 1.9.1-p378
* ruby-trunk
2010-05-15 01:23:13 +00:00
* rbx-head - broken at the moment, working with the rbx team for a solution
2010-04-06 02:17:32 +00:00
2010-04-15 20:35:42 +00:00
The ActiveRecord driver should work on 2.3.5 and 3.0
2010-04-05 07:01:17 +00:00
== Yeah... but why?
2010-04-05 05:42:00 +00:00
2010-04-05 07:01:17 +00:00
Someone: Dude, the Mysql gem works fiiiiiine.
2010-04-05 05:42:00 +00:00
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.
2010-04-06 08:16:37 +00:00
Me: Yep, but it's API is considerably more complex *and* is 2-3x slower.
2010-04-05 05:42:00 +00:00
== Benchmarks
2010-04-06 08:16:37 +00:00
Performing a basic "SELECT * FROM" query on a table with 30k rows and fields of nearly every Ruby-representable data type,
2010-04-05 05:42:00 +00:00
then iterating over every row using an #each like method yielding a block:
2010-05-06 23:41:57 +00:00
# These results are from the query_with_mysql_casting.rb script in the benchmarks folder
2010-04-22 20:57:34 +00:00
user system total real
2010-04-05 05:42:00 +00:00
Mysql2
2010-05-06 23:41:57 +00:00
0.890000 0.190000 1.080000 ( 2.028887)
2010-04-06 08:16:37 +00:00
Mysql
2010-05-06 23:41:57 +00:00
7.330000 0.350000 7.680000 ( 8.013160)
2010-04-05 05:51:38 +00:00
do_mysql
2010-05-12 21:14:52 +00:00
1.740000 0.220000 1.960000 ( 2.909290)
== Special Thanks
* Eric Wong - for the contribution (and informative explanations of) of some thread-safety, non-blocking I/O and cleanup patches. You rock dude