Skip to content

Commit fba67a9

Browse files
committed
Readme updates to the Compatibility and Benchmarks section
1 parent 3c2af97 commit fba67a9

File tree

1 file changed

+46
-60
lines changed

1 file changed

+46
-60
lines changed

README.md

Lines changed: 46 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ the `:default_file` and `:default_group` paramters. For example:
253253
Mysql2::Client.new(:default_file => '/user/.my.cnf', :default_group => 'client')
254254
```
255255

256-
### init_command
256+
### Initial command on connect and reconnect
257257

258258
If you specify the init_command option, the SQL string you provide will be executed after the connection is established.
259259
If `:reconnect` is set to `true`, init_command will also be executed after a successful reconnect.
@@ -400,46 +400,7 @@ There are a few things that need to be kept in mind while using streaming:
400400

401401
Read more about the consequences of using `mysql_use_result` (what streaming is implemented with) here: http://dev.mysql.com/doc/refman/5.0/en/mysql-use-result.html.
402402

403-
## Active Record
404-
405-
To use the Active Record driver (with or without rails), all you should need to do is have this gem installed and set the adapter in your database.yml to "mysql2".
406-
That was easy right? :)
407-
408-
NOTE: as of 0.3.0, and Active Record 3.1 - the Active Record adapter has been pulled out of this gem and into Active Record itself. If you need to use mysql2 with
409-
Rails versions < 3.1 make sure and specify `gem "mysql2", "~> 0.2.7"` in your Gemfile
410-
411-
## Asynchronous Active Record
412-
413-
Please see the [em-synchrony](https://github.com/igrigorik/em-synchrony) project for details about using EventMachine with mysql2 and Rails.
414-
415-
## Sequel
416-
417-
The Sequel adapter was pulled out into Sequel core (will be part of the next release) and can be used by specifying the "mysql2://" prefix to your connection specification.
418-
419-
## EventMachine
420-
421-
The mysql2 EventMachine deferrable api allows you to make async queries using EventMachine,
422-
while specifying callbacks for success for failure. Here's a simple example:
423-
424-
``` ruby
425-
require 'mysql2/em'
426-
427-
EM.run do
428-
client1 = Mysql2::EM::Client.new
429-
defer1 = client1.query "SELECT sleep(3) as first_query"
430-
defer1.callback do |result|
431-
puts "Result: #{result.to_a.inspect}"
432-
end
433-
434-
client2 = Mysql2::EM::Client.new
435-
defer2 = client2.query "SELECT sleep(1) second_query"
436-
defer2.callback do |result|
437-
puts "Result: #{result.to_a.inspect}"
438-
end
439-
end
440-
```
441-
442-
## Lazy Everything
403+
### Lazy Everything
443404

444405
Well... almost ;)
445406

@@ -468,42 +429,67 @@ This gem is tested with the following MySQL and MariaDB versions:
468429
* MySQL Connector/C 6.0 and 6.1 (primarily on Windows)
469430
* MariaDB 5.5, 10.0
470431

471-
This gem has two version families:
432+
### Active Record
472433

473434
* mysql2 0.2.x includes an Active Record driver compatible with AR 2.3 and 3.0
474435
* mysql2 0.3.x does not include an AR driver because it is included in AR 3.1 and above
475436

476-
## Yeah... but why?
437+
### Asynchronous Active Record
477438

478-
Someone: Dude, the Mysql gem works fiiiiiine.
439+
Please see the [em-synchrony](https://github.com/igrigorik/em-synchrony) project for details about using EventMachine with mysql2 and Rails.
479440

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

483-
Someone: OK fine, but do_mysql can already give me back values with Ruby objects mapped to MySQL types.
443+
Sequel includes a mysql2 adapter in all releases since 3.15 (2010-09-01).
444+
Use the prefix "mysql2://" in your connection specification.
484445

485-
Me: Yep, but it's API is considerably more complex *and* can be ~2x slower.
446+
### EventMachine
486447

487-
## Benchmarks
448+
The mysql2 EventMachine deferrable api allows you to make async queries using EventMachine,
449+
while specifying callbacks for success for failure. Here's a simple example:
488450

489-
Performing a basic "SELECT * FROM" query on a table with 30k rows and fields of nearly every Ruby-representable data type,
490-
then iterating over every row using an #each like method yielding a block:
451+
``` ruby
452+
require 'mysql2/em'
491453
492-
These results are from the `query_with_mysql_casting.rb` script in the benchmarks folder
454+
EM.run do
455+
client1 = Mysql2::EM::Client.new
456+
defer1 = client1.query "SELECT sleep(3) as first_query"
457+
defer1.callback do |result|
458+
puts "Result: #{result.to_a.inspect}"
459+
end
460+
461+
client2 = Mysql2::EM::Client.new
462+
defer2 = client2.query "SELECT sleep(1) second_query"
463+
defer2.callback do |result|
464+
puts "Result: #{result.to_a.inspect}"
465+
end
466+
end
467+
```
468+
469+
## Benchmarks and Comparison
470+
471+
The mysql2 gem converts MySQL field types to Ruby data types in C code, providing a serious speed benefit.
472+
473+
The do_mysql gem also converts MySQL fields types, but has a considerably more complex API and is still ~2x slower than mysql2.
474+
475+
The mysql gem returns only nil or string data types, leaving you to convert field values to Ruby types in Ruby-land, which is much slower than mysql2's C code.
476+
477+
For a comparative benchmark, the script below performs a basic "SELECT * FROM"
478+
query on a table with 30k rows and fields of nearly every Ruby-representable
479+
data type, then iterating over every row using an #each like method yielding a
480+
block:
493481

494482
``` sh
495-
user system total real
496-
Mysql2
497-
0.750000 0.180000 0.930000 ( 1.821655)
498-
do_mysql
499-
1.650000 0.200000 1.850000 ( 2.811357)
500-
Mysql
501-
7.500000 0.210000 7.710000 ( 8.065871)
483+
user system total real
484+
Mysql2 0.750000 0.180000 0.930000 (1.821655)
485+
do_mysql 1.650000 0.200000 1.850000 (2.811357)
486+
Mysql 7.500000 0.210000 7.710000 (8.065871)
502487
```
503488

489+
These results are from the `query_with_mysql_casting.rb` script in the benchmarks folder.
490+
504491
## Development
505492

506-
To run the tests, you can use RVM and Bundler to create a pristine environment for mysql2 development/hacking.
507493
Use 'bundle install' to install the necessary development and testing gems:
508494

509495
``` sh

0 commit comments

Comments
 (0)