Book Image

Instant InnoDB

By : Matt Reid
Book Image

Instant InnoDB

By: Matt Reid

Overview of this book

<p>InnoDB, the default storage engine for MySQL, is one of the most robust and commonly used in the world today. It can, however, be initially daunting to learn how to start implementing its features, and it can pose unique problems to the beginner user. This guide is written to address these problems.</p> <p>"Instant InnoDB" addresses everything you need to know for the installation, configuration, load testing, analysis, and long term support of an InnoDB based database environment. If you've ever wanted to learn more about InnoDB but didn't want to sit down with a formidably large or dry read, then the Instant InnoDB&nbsp; is your ideal companion.</p> <p>"Instant InnoDB" guides you expertly through the initial setup and system requirements for getting up and running with a simple environment that can be used for the development and testing of the InnoDB engine.<br /><br />After an in-depth look at basic and advanced configuration settings we delve into the internals of InnoDB that allow it to handle Multi-Version Concurrency Control and ACID transaction compliance. We will then dig deeper into advanced topics, such as memory buffer tuning methods and transaction log file handling. We'll then cover typical approaches and solutions to monitoring the database and reporting on operational states.</p> <p>By the end of the Instant InnoDB , we will have covered the common approaches to troubleshooting and error handling, as well as providing you with solutions to the most common architectural, administrative, and engineering related situations that arise when working with the InnoDB database engine.</p>
Table of Contents (14 chapters)

Creating your first InnoDB table


Once InnoDB support has been verified, you can start using the engine in your table definitions. A simple test to interact with InnoDB can be run as follows, where we will create a table with the engine specified explicitly along with a primary key, which InnoDB uses to index data.

If you leave off the engine definition at the end of the table create statement then MySQL will create the table with the system default engine, which is defined by the startup variables: default-storage-engine or, alternately, storage_engine, both accomplish the same purpose.

If you plan to use InnoDB exclusively for your table engine definitions, it generally makes sense to ensure that tables are created, explicitly and implicitly, by changing one or both of those startup variables to InnoDB. As of MySQL Version 5.5, the default has been changed to InnoDB so depending on your version you may not need to explicitly define the variable.

mysql> create database test; use test;
Query OK, 1 row affected (0.00 sec)
Database changed
mysql> CREATE TABLE `test` (
    ->   `id` int(8) NOT NULL auto_increment,
    ->   `data` varchar(255),
    ->   `date` datetime,
    ->  PRIMARY KEY  (`id`),
    ->  INDEX `date_ix` (`date`)
    -> ) ENGINE=InnoDB;
Query OK, 0 rows affected (0.06 sec)

In the preceding table create statement, we created one table with three columns. These columns are described as follows:

  • A primary key based id field that will automatically increase in value for every inserted row

  • A variable character based data field to store our values

  • Datetime based date field to record the insert date

Besides edge cases, these columns can be considered the bare minimum columns for relational data. The automatically incremented id field will allow InnoDB to efficiently index our data or rapid lookups, and the date field will allow us to easily sort and search our data based on the time it was created in the table.

These two columns are specifically important for InnoDB based tables, as other table engines can operate relatively quick and efficient full table scans without a primary key column if running a typical query such as SELECT COUNT(*) FROM test;. However, InnoDB requires a PRIMARY KEY column or other INDEX to read in order for the query results to remain expedient; thus our id column provides that performance gain. Instead of writing the inefficient latter query, we can now write SELECT COUNT(id) FROM test; and InnoDB will access the id column PRIMARY KEY index which results in a very fast result versus a full table scan. Similarly, we can sort our table data via the date column and get quickly-returned table data.

If you'd like to experiment with InnoDB features more easily or see how different versions of MySQL work with InnoDB, you can install a sandboxing tool such as MySQL Sandbox.