My sql storage engines

762 views

Published on

Published in: Technology, Business
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
762
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

My sql storage engines

  1. 1. INDEX:A. What is storage engine?B. Choosing the right engineC. Specifying and altering storage enginesD. How to tell which storage engine a MySQL table uses?E. Check MySQL engine type for a specific table?F. Check the Default Storage Engine of MySQLG. Setting the Storage EngineH. Check if InnoDB is enabled in MySQL serverI. Important INNODB storage related parameter innodb_file_per_table
  2. 2. (A)What is storage engine?In MySQL the data’s are stored as files in any one of the types in storage engines.MySQL supports pluggable storage engines that we can use any types of enginebelongs to your data. There are two types of storage engines in MySQL Transactional(The data can be modified in engines) and non-transactional (It can only fetch thedata from engines). The default storage engine for MySQL prior to version 5.5 wasMyISAM. For MySQL 5.5 and later, the default storage engine is InnoDB.Features of MySQL Storage EnginesChoosing the right storage engine is an important strategic decision, which will impactfuture development. If you are planning a production database, then things becomemore complicated.A storage engine is a software module that a database management system uses tocreate, read, and update data from a database. There are two types of storageengines in MySQL. Transactional and non-transactional.MySQL supported the following types of Storage Engine1. MyISAM2. InnoDB3. Merge4. Memory5. Blackhole6. Archive7. CSV8. Federated1. MyISAMMyISAM is the oldest and original storage engine. It is a fast storage engine.it doesnot support transactions. MyISAM provides table level locking. It is used most in web,data warehousing. If you create a table without representing the storage engine ittake MyISAM engine as default (before MySQL 5.5 version).MySQL databases aregenerally store in data directories and MYISAM tables are stored using 3 files..frm - Table structure.MYD - Data file
  3. 3. .MYI - Index fileMyISAM has most flexible auto increment. The tables can be used to set up mergetables. The table storage format is portable. Maximum no of indexes per table =64.Maximum no of columns per index = 16.Blob and TEXT columns can be indexed.Table size is 256TB. It does not provide for foreign key.2. InnoDBInnoDB is the most widely used storage engine with transaction supportive InnoDB isa Transactional safe storage engine. It is an ACID (Atomicity, Consistency, Isolation,Durability) compliant storage engine. It supports row-level locking, crash recovery andmulti-version concurrency control.i.e it has commits rollback and crash recoverycapability to recover the data. InnoDB provide row level locking to use multi userconcurrency and performance. It supports foreign key. InnoDB creates two log filesnamely "ib_logfile0" and "ib_logfile1" and a data file "ibdata1" in the MySQL datadirectory where it stores its tables. MySQL creates an auto-extending 10MB data filein ibdata1 and two 5MB log files in ib_logfile0 andib_logfile1 in the MySQL datadirectory. The table definitions are stored in database directory with a .frm extensionwhereas the data is stored in the "ibdata1" - tablespace.Minimum table space size is10MB. And maximum table space size is 64TB.It is the only engine which providesforeign key referential integrity constraint.3. MergeMerge operates on underlying MyISAM tables. Merge tables help manage largevolumes of data more easily. The MERGE engine type allows you to combine anumber of identical tables into a single table. You can then execute queries thatreturn the results from multiple tables as if they were just one table. Each tablemerged must have the same table definition. The MERGE table is particularly effectiveif you are logging data directly or indirectly into a MySQL database and create anindividual table per day, week or month and want to be able to produce aggregatequeries from multiple tables. Transaction No Locking level Table. It logically groups aseries of identical MyISAM tables, and references them as one object. Good for datawarehousing environments.
  4. 4. 4. MemoryMemory Storage engine creates tables in memory. It is the fastest engine. TheMEMORY storage engine (previously known as the HEAP storage engine) stores alldata in memory; once the MySQL server has been shut down any information storedin a MEMORY database will have been lost. However, the format of the individualtables is kept and this enables you to create temporary tables that can be used tostore information for quick access without having to recreate the tables each time thedatabase server is started. Cannot contain BLOB or TEXT columns. It has table levellocking. It does not support transactions. Memory storage engine is ideal for creatingtemporary tables or quick lookups. The data is lost when the database is restarted.5. BlackholeIt acts as a “black hole” that accepts data but throws it away and does not store it.BLACKHOLE engine does not actually store any data. Retrievals always return anempty set. Although you can create tables and indexes, all SQL statements that wouldadd or update information to the database are executed without actually writing anydata. The database structure is retained, however, and you can create any indexes onthe (non-existent) information that you want. The functionality can be used indistributed database design where data is automatically replicated, but not storedlocally. This storage engine can be used to perform performance tests or othertesting.6. ArchiveArchive storage engine is optimized for high speed inserting. It is used for storinglarge amounts of data without indexes in a very small footprint. It supports only theINSERT and SELECT statements, but does support most of the MySQL field types. Itcompresses data as it is inserted. It does not support truncations. Information storedin an ARCHIVE storage engine table is compressed and cannot be modified and soARCHIVE tables are perfect for storing log data when an archive table is created,following files are created in the database directory..frm-tabledefinition.ARZ-DATAfile.ARM - METADATA file
  5. 5. It does NOT support DELETE, REPLACE and UPDATE. It provides row level locking. It isideal for storing, retrieving large amounts of seldom referenced historical archiveddata.7. CSV (Comma Separated Value)CSV stores data in CSV files. It provides great flexibility, because data in this format iseasily integrated into other applications.. It stores data in text files using comma-separated values format. When a table is created 2 files are created in the databasedirectory.frm-tabledefinition.CSV - data fileIt is not an efficient method for storing large volumes of data, or larger data types likeBLOB, although such types are supported. There is also no indexing. However,because the data is stored in the CSV format it is exceedingly portable.8. FederatedFederated storage engine offers the ability to separate MySQL servers to create onelogical database from many physical servers. The FEDERATED storage engine (addedin MySQL 5.03) enables you to access data from remote MySQL database (otherdatabases may be supported in the future) as if it were a local database. In effect, theMySQL server acts as a proxy to the remote server, using the MySQL client accesslibrary to connect to the remote host, execute queries and then reformat the datainto the localized format. It does not support transactions. Queries on the local serverare automatically executed on the remote (federated) tables. No data is stored on thelocal tables. It is good for distributed environments.(B)Choosing the right engine :No storage engine is ideal for all circumstances. Some perform best under certainconditions and perform worse in other situations. There are trade-offs than must beconsidered. A more secure solution takes more resources. It might be slower, takemore CPU time and disk space. MySQL is very flexible in the fact that it providesseveral different storage engines. Some of them, like the Archive engine, are created
  6. 6. to be used in specific situations. Ironically this also brings a question, which storageengine to use? Which may not be easily answered?In some cases, the answer is clear. Whenever we are dealing with some paymentsystems, we are obliged to use the most secure solution. We cannot afford to losesuch sensitive data. InnoDB is the way to go. If we want full-text search, than we mustchoose MyISAM.Only InnoDB supports foreign key referential integrity constraint and if we plan to usethis constraint, then the choice is clear. In many situations we must have enoughexperience to choose the right engine. The question is further complicated by thefact, that we can choose different storage engines for different tables.(C) Specifying and altering storage enginesThe storage engine is specified at the time of the table creation.MySQL> CREATE TABLE Cars (Id INTEGER PRIMARY KEY, Name VARCHAR (50), CostINTEGER) ENGINE=MyISAM;The ENGINE keyword specifies the storage engine used for this particular table.If we do not specify the storage engine explicitly, then the default storage engine isused. Prior to MySQL 5.5 the default storage engine was MyISAM. For MySQL 5.5 andlater, the default storage engine is InnoDB.MySQL> SHOW VARIABLES LIKE storage_engine;The default storage engine can be found in the storage_engine variable.It is possible to migrate to a different storage engine. Note that migrating a large tablemight take a long time. Also we might run into some problems when migrating tables.Some features might not be supported in both tables.MySQL> SELECT ENGINE FROM information_schema.TABLES WHERETABLE_SCHEMA=mydbAND TABLE_NAME=Cars;
  7. 7. This SQL statement finds out the storage engine used for a Cars table in mydbdatabase. We could also use SELECT CREATE TABLE Cars SQL statement.The information_schema is a table which stores technical information about ourtables.MySQL> ALTER TABLE Cars ENGINE=MyISAM;This SQL statement changes the storage engine to MyISAM.MySQL> SELECT ENGINE FROM information_schema.TABLESWHERETABLE_SCHEMA=mydb AND TABLE_NAME=Cars;| ENGINE || MyISAM |Now the storage engine is MyISAM.(D)How to tell which storage engine a MySQL table uses :MySQL supports multiple storage engines (E.g. . . MyISAM, INNODB, etc…)eachWith its pros and cons, and each table in a MySQL database can use a differentstorage engine.How to work out which table storage engine is used by a MySQL table,using either a SQL query or using the web browser tool phpMyAdmin.SQL QueryAfter digging around in the phpMyAdmin code I worked out they determine theMySQL table storage engine by querying the INFORMATION_SCHEMA database. Thisis a special database which describes information relating to the various databases onthe server. The query the "products" table of the "test" database to see which storageengine it is using, you would run this SQL query:SELECT ENGINE FROM information_schema.TABLES WHERE TABLE_SCHEMA=’test’AND TABLE_NAME=’products’;The TABLE_SCHEMA is the name of the database, and TABLE_NAME is the table nameyou wish to query. The SQL query above will return the storage engine, assuming thedatabase and table specified exists, and you have sufficient permissions.
  8. 8. If you wanted to see the storage engine for all tables in your database, do thisinstead:SELECT TABLE_NAME, ENGINE FROM information_schema.TABLES WHERETABLE_SCHEMA=’test’;(E) Check MySQL engine type for a specific table?SHOW TABLE STATUS WHERE NAME=’table_name‘This will give you (among other things) an engine column, which is what you want.SELECT TABLE_NAME, ENGINE FROM information_schema.TABLES whereTABLE_SCHEMA = database AND ENGINE IS NOT NULL;This excludes MySQL views from the list, which dont have an engine
  9. 9. (F) Check the Default Storage Engine of MySQL1. Login as a rrot user and connect MySQL promt2. Start the servers3. Enter the following line into the MySQL prompt and hit Enter:USE information_schema;4. Enter the following line into the MySQL prompt and hit Enter:SELECT * FROM engines;5. A table with the Storage Engines of MySQL will show up. Inside the Supportcolumn the Default Storage Engine has the value "DEFAULT".Value MeaningYES The engine is supported and is activeDEFAULT Like YES, plus this is the default engineNO The engine is not supportedDISABLED The engine is supported but has been disabled
  10. 10. (http://dev.mysql.com/doc/refman/5.0/en/show-engines.html)(G) Setting the Storage Engine:When you create a new table, you can specify which storage engine to use by addingan ENGINE table option to the CREATE TABLE statement:CREATE TABLE t (i INT) ENGINE = INNODB;If you omit the ENGINE option, the default storage engine is used. Normally, thisis MyISAM, but you can change it by using the --default-storage-engine server startupoption, or by setting the default-storage-engine option in the my.cnf configurationfile.You can set the default storage engine to be used during the current session bysetting the storage_enginevariable:SET storage_engine=MYISAM;When MySQL is installed on Windows using the MySQL Configuration Wizard,the InnoDB or MyISAM storage engine can be selected as the default.(See Section 2.3.5.5, “MySQL Server Instance Config Wizard: The Database UsageDialog”.)To convert a table from one storage engine to another, use an ALTERTABLE statement that indicates the new engine:ALTER TABLE t ENGINE = MYISAM;(H) Check if innodb is enabled in Mysql serverMySQL database server has the capability to use different database storage engines.The database storage engine is what actually does the work of storing and retrievingdata from the underlying database tables.The storage engines work like a modular code which can be plugged into MySQL.Among the storageengineer’s MyISAM is the one, used most widely and also the
  11. 11. default storage engine for MySQL. However it does lack some high level functionalitywhich is requirement for any enterprise level database server e.g. it does not haveproper support for transaction or foreign key.This is where InnoDB storage engine comes into play, InnoDB is a transaction safestorage engine with support for foreign keys along with commit, rollback and crashrecovery capabilities.InnoDB is a product of InnobaseOn, which a Finnish company now owned by Oracle.MySQL has a licensing agreement with Innobase On which allows it to provide theInnoDB.How we can check if the MySQL server we are running has InnoDB support enabled ornot?There is a MySQL system variable have_innodb and its value can be checked to see ifthe support enabled or not. You can do this by using following commandMysqladmin variables | grephave_innodbIf this returns YES then it means that the InnoDB support is enabled. If you have ‘–skip-InnoDB’ in your /etc/my.cnf MySQL configuration file then InnoDB engine wouldbe disabled. And in that case you can comment out this option to enable InnoDB. It isenabled by default and unless there is some related disable configuration added tomy.cnf configuration, there should be no issues.Also if you would like to use InnoDB as the default storage engine, you can do that byusing below configuration in /etc/my.cnf :Default-stroage_engine=innodbBut make sure to do your homework before setting the storage engine, it do haveperformance benefits over MyISAM once its configured properly, but do have higheradministrative cost. InnoDB can be configured to have per table InnoDB file, which dohelp in isolating any individual database corruptions.(I)Important INNODB storage related parameter innodb_file_per_table:The data files that you define in the configuration file form the InnoDB systemtablespace. The files are logically concatenated to form the table space. There isno striping in use. Currently, you cannot define where within the table space yourTables are allocated. However, in a newly created table space, InnoDB allocates
  12. 12. Space starting from the first data file.To avoid the issues that come with storing all tables and indexes inside thesystem table space, you can turn on the innodb_file_per_table configurationoption, which stores each newly created table in a separate tablespace file (withextension .ibd). For tables stored this way, there is less fragmentation within thedisk file, and when the table is truncated, the space is returned to the operatingsystem rather than still being reserved by InnoDB within the system tablespace.-- [root@vmxdb01 test]#grep innodb_file_per_table /etc/my.cnfinnodb_file_per_table = 1MySQL> show variables like innodb_file_per_table;+-----------------------+-------+| Variable_name | Value |+-----------------------+-------+| innodb_file_per_table | ON |+-----------------------+-------+1 row in set (0.00 sec)[root@vmxdb01 test]# ls -l /var/lib/mysql/testtotal 216-rw-rw----. 1 mysqlmysql 9106 Jun 15 23:39 customers.frm-rw-rw----. 1 mysqlmysql 98304 Jun 15 23:42 customers.ibd-rw-rw----. 1 mysqlmysql 8556 Jun 16 00:02 user.frm-rw-rw----. 1 mysqlmysql 98304 Jun 16 00:02 user.ibdThis parameter can be dynamically changed:MySQL> set global innodb_file_per_table=on;

×