You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -390,23 +400,50 @@ There are a few things that need to be kept in mind while using streaming:
390
400
391
401
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.
392
402
393
-
## Active Record
403
+
### Lazy Everything
404
+
405
+
Well... almost ;)
406
+
407
+
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.
408
+
409
+
Rows themselves are lazily created in ruby-land when an attempt to yield it is made via #each.
410
+
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).
411
+
Now say you were to iterate over that same collection again, this time yielding 15 rows - the 4 previous rows that had already been turned into ruby hashes would be pulled from an internal cache, then 11 more would be created and stored in that cache.
412
+
Once the entire dataset has been converted into ruby objects, Mysql2::Result will free the Mysql C result object as it's no longer needed.
413
+
414
+
This caching behavior can be disabled by setting the `:cache_rows` option to false.
415
+
416
+
As for field values themselves, I'm workin on it - but expect that soon.
417
+
418
+
## Compatibility
419
+
420
+
This gem is tested with the following Ruby versions on Linux and Mac OS X:
This gem is tested with the following MySQL and MariaDB versions:
427
+
428
+
* MySQL 5.0, 5.1, 5.5, 5.6
429
+
* MySQL Connector/C 6.0 and 6.1 (primarily on Windows)
430
+
* MariaDB 5.5, 10.0
394
431
395
-
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".
396
-
That was easy right? :)
432
+
### Active Record
397
433
398
-
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
399
-
Rails versions < 3.1 make sure and specify `gem "mysql2", "~> 0.2.7"` in your Gemfile
434
+
* mysql2 0.2.x includes an Active Record driver compatible with AR 2.3 and 3.0
435
+
* mysql2 0.3.x does not include an AR driver because it is included in AR 3.1 and above
400
436
401
-
## Asynchronous Active Record
437
+
### Asynchronous Active Record
402
438
403
439
Please see the [em-synchrony](https://github.com/igrigorik/em-synchrony) project for details about using EventMachine with mysql2 and Rails.
404
440
405
-
## Sequel
441
+
### Sequel
406
442
407
-
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.
443
+
Sequel includes a mysql2 adapter in all releases since 3.15 (2010-09-01).
444
+
Use the prefix "mysql2://" in your connection specification.
408
445
409
-
## EventMachine
446
+
### EventMachine
410
447
411
448
The mysql2 EventMachine deferrable api allows you to make async queries using EventMachine,
412
449
while specifying callbacks for success for failure. Here's a simple example:
@@ -429,64 +466,30 @@ EM.run do
429
466
end
430
467
```
431
468
432
-
## Lazy Everything
469
+
## Benchmarks and Comparison
433
470
434
-
Well... almost ;)
435
-
436
-
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.
437
-
438
-
Rows themselves are lazily created in ruby-land when an attempt to yield it is made via #each.
439
-
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).
440
-
Now say you were to iterate over that same collection again, this time yielding 15 rows - the 4 previous rows that had already been turned into ruby hashes would be pulled from an internal cache, then 11 more would be created and stored in that cache.
441
-
Once the entire dataset has been converted into ruby objects, Mysql2::Result will free the Mysql C result object as it's no longer needed.
442
-
443
-
This caching behavior can be disabled by setting the :cache_rows option to false.
444
-
445
-
As for field values themselves, I'm workin on it - but expect that soon.
471
+
The mysql2 gem converts MySQL field types to Ruby data types in C code, providing a serious speed benefit.
446
472
447
-
## Compatibility
448
-
449
-
This gem is regularly tested against the following Ruby versions on Linux and Mac OS X:
* Rubinius 2.0 in compatibility modes 1.8, 1.9, 2.0.
454
-
455
-
The mysql2 gem 0.2.x series includes an Active Record driver that works with AR
456
-
2.3.x and 3.0.x. Starting in Active Record 3.1, a mysql2 driver is included in
457
-
the Active Record codebase and no longer provided in mysql2 gem 0.3 and above.
458
-
459
-
## Yeah... but why?
460
-
461
-
Someone: Dude, the Mysql gem works fiiiiiine.
473
+
The do_mysql gem also converts MySQL fields types, but has a considerably more complex API and is still ~2x slower than mysql2.
462
474
463
-
Me: It sure does, but it only hands you nil and strings for field values. Leaving you to convert
464
-
them into proper Ruby types in Ruby-land - which is slow as balls.
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.
465
476
466
-
Someone: OK fine, but do_mysql can already give me back values with Ruby objects mapped to MySQL types.
467
-
468
-
Me: Yep, but it's API is considerably more complex *and* can be ~2x slower.
469
-
470
-
## Benchmarks
471
-
472
-
Performing a basic "SELECT * FROM" query on a table with 30k rows and fields of nearly every Ruby-representable data type,
473
-
then iterating over every row using an #each like method yielding a block:
474
-
475
-
These results are from the `query_with_mysql_casting.rb` script in the benchmarks folder
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:
476
481
477
482
``` sh
478
-
user system total real
479
-
Mysql2
480
-
0.750000 0.180000 0.930000 ( 1.821655)
481
-
do_mysql
482
-
1.650000 0.200000 1.850000 ( 2.811357)
483
-
Mysql
484
-
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)
485
487
```
486
488
489
+
These results are from the `query_with_mysql_casting.rb` script in the benchmarks folder.
490
+
487
491
## Development
488
492
489
-
To run the tests, you can use RVM and Bundler to create a pristine environment for mysql2 development/hacking.
490
493
Use 'bundle install' to install the necessary development and testing gems:
0 commit comments