A fast and furious guide to my sql database engines


Published on

MYSQL database engines

  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

A fast and furious guide to my sql database engines

  1. 1. A fast and furious guide to MySQLdatabase enginesBy Sanders "Kaufman, Jr."April 7, 2003, 7:00am PDTWhat if you were a racecar driver and could swap engines with the flip of a switch instead ofhaving to make a trip to the garage? The MySQL database does something like this fordevelopers; it gives you a choice of database engines and an easy way to switch them.The MySQL stock engine is certainly adequate, but there are circumstances where the otheravailable engines may be better suited to the task at hand. If you want, you can even bore outthe cylinders and slap on a four-barrel carburetor by building your own database engine usingthe MySQL++ API. Lets look at how you choose the engine and how to change betweenengines that are available to you.Choose your engineThe number of database engines available to you depends on how your installation of MySQLwas compiled. To add a new engine, MySQL must be recompiled. The concept of compilingan application just to add a feature may seem odd to Windows developers, but in the UNIXworld, its the norm. By default, MySQL supports three database engines: ISAM, MyISAM,and HEAP. Two other types, InnoDB and Berkley (BDB), are often available as well.ISAMISAM is a well-defined, time-tested method of managing data tables, designed with the ideathat a database will be queried far more often than it will be updated. As a result, ISAMperforms very fast read operations and is very easy on memory and storage resources. Thetwo main downsides of ISAM are that it doesnt support transactions and isnt fault-tolerant: Ifyour hard drive crashes, the data files will not be recoverable. If youre using ISAM in amission-critical application, you’ll want to have a provision for constantly backing up all yourlive data, something MySQL supports through its capable replication features.MyISAMMyISAM is MySQLs extended ISAM format and default database engine. In addition toproviding a number of indexing and field management functions not available in ISAM,MyISAM uses a table-locking mechanism to optimize multiple simultaneous reads and writes.The trade-off is that you need to run the OPTIMIZE TABLE command from time to time torecover space wasted by the update algorithms. MyISAM also has a few useful extensionssuch as the MyISAMChk utility to repair database files and the MyISAMPack utility forrecovering wasted space.MyISAM, with its emphasis on speedy read operations, is probably the major reason MySQLis so popular for Web development, where the vast majority of the data operations you’ll becarrying out are read operations. As a result, most hosting and Internet Presence Provider(IPP) companies will allow the use of only the MyISAM format.HEAPHEAP allows for temporary tables that reside only in memory. Residing in memory makes
  2. 2. HEAP faster than ISAM or MyISAM, but the data it manages is volatile and will be lost if itsnot saved prior to shutdown. HEAP also doesn’t waste as much space when rows are deleted.HEAP tables are very useful in situations where you might use a nested SELECT statement toselect and manipulate data. Just remember to destroy the table after you’re done with it. Letme repeat that: Don’t forget to destroy the table after you’re done with it.InnoDB and Berkley DBThe InnoDB and Berkley DB (BDB) database engines are direct products of the technologythat makes MySQL so flexible, the MySQL++ API. Almost every challenge youre likely toface when using MySQL stems directly from the fact that the ISAM and MyISAM databaseengines arent transactional and lack foreign-key support. Although much slower than theISAM and MyISAM engines, InnoDB and BDB include the transactional and foreign-keysupport missing from the former two choices. As such, if your design requires either or bothof these features, you’re actually compelled to use one of these two choices.If you’re feeling particularly capable, you can create your own database engine usingMySQL++. The API provides all the functions you need for working with fields, records,tables, databases, connections, security accounts, and all of the other myriad functions thatmake up a DBMS such as MySQL. Going too heavily into the API is beyond the scope of thisarticle, but it’s important to know that MySQL++ exists and that it’s the technology behindMySQL’s swappable engines. Presumably, this model of plug-in database engines could evenbe used to build a native XML provider for MySQL. (Any MySQL++ developers reading thismay consider that a request.)Flipping the switchThe switch that makes all this flexibility possible is an extension MySQL provides to ANSISQL, the TYPE parameter. MySQL allows you to specify database engines at the table level,so they are sometimes referred to as table formats. The following sample code shows how tocreate tables that use the MyISAM, ISAM, and HEAP engines, respectively. Notice that thecode to create each table is the same, with the exception of the trailing TYPE parameter,which specifies the database engine.CREATE TABLE tblMyISAM ( id INT NOT NULL AUTO_INCREMENT, PRIMARY KEY (id), value_a TINYINT) TYPE=MyISAMCREATE TABLE tblISAM ( id INT NOT NULL AUTO_INCREMENT, PRIMARY KEY (id), value_a TINYINT) TYPE=ISAMCREATE TABLE tblHeap ( id INT NOT NULL AUTO_INCREMENT, PRIMARY KEY (id), value_a TINYINT) TYPE=HeapYou can also use the ALTER TABLE command to move an existing table from one engine to
  3. 3. another. The following code shows the use of ALTER TABLE to move a MyISAM table toInnoDBs engine:ALTER TABLE tblMyISAM CHANGE TYPE=InnoDBMySQL makes this happen with three steps. First, an exact copy of the table is created. Next,any incoming data changes are queued, while the copy is moved to the other engine. Finally,any queued data changes are committed to the new table, and the original one is deleted.An ALTER TABLE shortcutIf you’re simply upgrading your tables from ISAM to MyISAM, you can use the commandmysql_convert_table_format instead of writing ALTER TABLE statements.You can use the SHOW TABLE command (which is another MySQL extension to the ANSIstandard) to determine which engine is managing a particular table. SHOW TABLE returns aresult set with multiple columns that you can query to get all kinds of information: The nameof the database engine is in the Type field. The following sample code illustrates the use ofSHOW TABLE:SHOW TABLE STATUS FROM tblInnoDBA SHOW TABLE alternativeYou can use SHOW CREATE TABLE [TableName] to retrieve the same information thatSHOW TABLE retrieves.Finally, its worth noting that if you try to use an engine that isnt compiled into MySQL andactivated, MySQL won’t complain about it. It will instead quietly give you a table in thedefault format (MyISAM) instead. There are plans to have MySQL throw an error in additionto using the default table format, but for now, you’ll want to check the table format usingSHOW TABLE if you’re not certain that a particular database engine is available.More choices means better performanceConsidering the additional complications of recompiling and tracking which engines are usedfor particular tables, why would you want to use any of the nondefault database engines? Theanswer is simple: to tune the database to meet your needs.Sure, MyISAM is fast, but if your logical design requires transactions, you’re free to use oneof the transaction-enabled engines. Further, since MySQL allows you to apply databaseengines on the table level, you can take the performance hit on only the tables that requiretransactions and leave the nontransactional tables to be managed by the more lightweightMyISAM engine. With MySQL, flexibility is the key.13CommentsAdd Your OpinionJoin the conversation!
  4. 4. Follow via: RSS Email AlertSee All Comments Staff Picks Top Rated Most Recent My Contacts0 Votes+-RE: A fast and furious guide to MySQL database enginesjwendel@... 11th Jun 2009While your article led me to the solution, my version of MySQL required a different syntax:alter table tableName type=InnoDB;show table status from databaseName;View in thread0 Votes+-Great! Exactly what I was looking for.deviloper@... Updated - 11th Mar 2009Thanks a lot.View in thread0 Votes+-RE: A fast and furious guide to MySQL database enginesharishkous@... Updated - 13th Oct 2008This is a good article which helps one who are facing with common MYSQL error of "Cantcreate table (errno: 150)", when trying to add a foreign key constraint to existing table.Rootcause for the... Read Whole Comment +