- General
- Articles
- Legacy
- Misc
- Third Party
- Who uses RedBeanPHP
- Personal
Optimizer
The optimizer is a plugin that is shipped with RedBeanPHP by default. This plugin can adjust columns for you on-the-fly for specific types. For instance, DATETIME columns, ENUMs or other columns that are (database) specific. Here is how to use the optimizer:
$o = new RedBean_Plugin_Optimizer(R::$toolbox);
$odt = new RedBean_Plugin_Optimizer_Datetime(R::$toolbox);
$o->addOptimizer($odt);
$os = new RedBean_Plugin_Optimizer_Shrink(R::$toolbox);
$o->addOptimizer($os);
R::$redbean->addEventListener('update',$o);
$b = R::dispense('event');
$b->adate = '2010-10-10 10:00:00';
R::store($b);
//after a couple of times column gets converted to DATETIME.
The optimizer works posthoc. In the case of DATETIME for instance a datetime value is first saved as a VARCHAR. While the column has the wrong format (or, not the optimal format) in MySQL you can still treat the value as a DATETIME value. On every update query the optimizer selects a random column and tries to optimize it. When the optimizer selects the VARCHAR column containing a DATETIME value it will try to convert the column to DATETIME. The optimizer first checks whether the values in the column would be preserved in case the column got converted. If it turns out no information gets lost by the conversion, the actual conversion will take place. Otherwise the column remains untouched. Two optimizers are shipped with RedBeanPHP by default; both are for use with MySQL only: Datetime and Shrink.
Datetime (MySQL)
The DATETIME optimizers tries to convert columns that only contain datetime values to DATETIME columns. Once the column has been changed there is no way back (except changing it with your DBA tool). Usage:
$odt = new RedBean_Plugin_Optimizer_Datetime(R::$toolbox);
$o->addOptimizer($odt);
Shrink (MySQL)
The shrink optimizer tries to make columns smaller if possible. For instance if all values in a VARCHAR column are numeric and they would fit perfectly in a TINYINT, the Shrink optimizer will narrow the column to a TINYINT. Usage:
$os = new RedBean_Plugin_Optimizer_Shrink(R::$toolbox);
$o->addOptimizer($os);
Unlike the Datetime optimizer, columns that have been shrunken can be restored again by inserting a value that does not fit. RedBeanPHP's widening mechanism will then stretch the column one again to make the data type fit the value. Of course, this happens only in fluid mode.
Using Optimizers
Optimizers come in handy in fluid mode for development, they might reduce some of the post-dev DBA adjustments. Do not rely on the too much though.
Tweet
User contributed notes. Please use the comment section to provide tips, notes and examples. To ask for support or to provide feedback use the forum. For bug reports use Github Issue Tracker.
Site comments powered by Disqus