1) How do I backup my tables using UNIX file system?
.frm holds the definition of a table. With MyISAM, the .MYD and MYI files hold the data and index information for the table.
If you are using InnoDB or another non-MyISAM engine for a table, you will not have .MYD and .MYI files. Therefore backing up the MyISAM tables is easy and fast if you have access to these files. You can create table of the same structure without keys, load data into it to get correct .MYD (data file). Create table with all keys defined and copy over .frm and .MYI files from it, followed by FLUSH TABLES. Now you can use REPAIR TABLE to rebuild all keys by sort, including UNIQUE keys.
2) What is the difference in MyISAM and InnoDB tables?
Backup files Can't backup files
Doesn't support crash recovery Does support crash recovery
Table level locking Row level locking
fast full table scan Slow full tablescan
Full text index No full text index possible
No Foreign Key support Foreign Key support
No Transaction support Supports transactions
InnoDB is a storage engine, and so is MyISAM. When you create a table, you specify one of the types. When you add data and indexes to that table, the type of table determines the storage engine used. InnoDB does row level locking. This means that when you are updating a row, only that one row gets locked (which means that another connection to the database cannot modify that row). MyISAM locks the entire table. Only one connection / session at a time can update / insert / delete.
InnoDB uses the concept of a tablespace; MyISAM doesn't. A tablespace is where you store your data, and is made up of datafiles. You don't know where your data is stored in those data files. When you create a table in MyISAM, it crates a file of the same name as your table. Some queries are really bad on InnoDB compared to MyISAM, the most notorious is probably SELECT COUNT(*) A full table scan is also much faster on MyISAM than on InnoDB.
InnoDB doesn't support full text indexes on text columns so if you need to search text fields you may want to use MyISAM.
In addition to foreign keys, InnoDB offers transaction support, which is absolutely critical when dealing with larger applications. Sped does suffer though because all this Foreign Key / Transaction stuff takes lots of overhead. With InnoDB it becomes particularly important that you use good keys.
Inserting 50,000 records is something MyISAM is very comfortable with. In some cases it is faster to drop the indexes, insert the records and recreate the indexes (especially with full text indexes).
Use InnoDB in a situation where there are lots of inserts, updates and selects. Tests show that MyISAM is a lot faster when there are very few records (<50k), but the average execution time for a query increases almost linearly with the number of records while InnoDB shows almost constant query execution times for very small and large tables.
MyISAM does not support crash recovery while InnoDB does.
3) What should be the correct column definition?
VARCHAR(25) and VARCHAR(200) are practically the same for the purpose of storing data on the disk. If the user types only 5 characters in either columns, the bytes used will be the same. Select the value liberally but remember that VARCHAR(250) for
all text columns is not good idea either because it will not allow you to create composite indexes.
4) How does Primary and other keys affect the programming logic?
Composite primary indexes are great to avoid duplicate entries. But sometimes it can be a problem. For e.g. take a look at the following tables PRIMARY KEY definition.
CREATE TABLE `student` (
`enroll` varchar(13) NOT NULL default '',
`other` int(2) NOT NULL default '0',
`year` int(4) NOT NULL default '0',
`month` tinyint(2) unsigned default NULL,
`mode_read` int(2) NOT NULL default '0',
`mode_direct` int(2) NOT NULL default '0',
`sent_flag` int(11) NOT NULL default '0',
`system_date` datetime NOT NULL default '0000-00-00 00:00:00',
PRIMARY KEY (`enroll_no`,`other`,`year`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
Adding year as a part of primary key but not adding month column in it means there can be only 1 prospectus entry per year for each student. Now we can not use replace command that will insert a record if it does not exist in the primary key definition or update if it already does.
Labels: mysql FAQ