MySQL DBA Session 5 storage engines

Post on 12-Apr-2017

67 views 4 download

Transcript of MySQL DBA Session 5 storage engines

Session 5 Storage EnginesRAM N SANGWAN

WWW.HOMETUTOR.NET. IN

HT TP://YOUTUBE.COM/USER/THESKILLPEDIA

MySQL Storage Engines

• A storage engine can be though of as a File System in an Operating System.

• Storage engines differ in the way

◦ that they use locking to manage query contention, or

◦ whether the tables that they provide are transactional or non-transactional.

• These engine properties have implications for query processing performance,concurrency, and deadlock prevention.

• When you create a table, you can choose what storage engine to use.

R.N. SANGWAN (WWW.RNSANGWAN.COM) 2

Specify a storage engine..

• Explicitly in a CREATE TABLE statement, use an ENGINE option:

CREATE TABLE t (i INT) ENGINE = InnoDB;

• Without using an ENGINE option, the default engine given by storage_engine system variable willbe used.

• To determine which storage engine is used for a given table, you can use the SHOW CREATETABLE or the SHOW TABLE STATUS statement:

mysql> SHOW CREATE TABLE City\G

mysql> SHOW TABLE STATUS LIKE ‘CountryLanguage‘ \G

• The INFORMATION_SCHEMA TABLES table contains storage engine information as well:

mysql> SELECT TABLE_NAME, ENGINE FROM INFORMATION_SCHEMA.TABLES

-> WHERE TABLE_SCHEMA ='world';

• In most respects the way that you use the table after creating it, is engine independent.

R.N. SANGWAN (WWW.RNSANGWAN.COM) 3

Storage Engine Prerequisites

• It must be compiled into the server and enabled. Each storage engine is asoftware module that is compiled into the server.

• The use of this modular approach allows storage engines to be easily selectedfor inclusion in the server at configuration time.

• Some storage engines are always available, such as InnoDB, MyISAM, MERGE,and MEMORY.

• Support for optional engines typically can be selected when MySQL isconfigured and built.

• To reduce memory use, don't configure unneeded storage engines into theserver. To see what storage engines:

mysql> SHOW ENGINES\G

R.N. SANGWAN (WWW.RNSANGWAN.COM) 4

MyISAM Storage Engine

• MyISAM is the improved version of the original storage engine of MySQL, ISAM

• The MyISAM engine is fast and thus, preferred for web and other applicationenvironments.

• It is also used for data warehousing

• MyISAM is not transaction-safe and supports 64 keys per table with maximumkey length of 1024 bytes

• The size of MyISAM table depends on the host operating system

R.N. SANGWAN (WWW.RNSANGWAN.COM) 5

MyISAM Storage Engine Contd..

• MyISAM table allows table level locking only.

• There are no limitations on data file transfer and the data files can be portedfrom system to system

• The foreign key constraint cannot be defined

• MyISAM is the only storage engine that supports Full-text search

• It also supports one auto increment column per table

• A high-byte-first pattern for saving numeric key values ensures faster indexing

• It can be used where fulltext indexing is needed

R.N. SANGWAN (WWW.RNSANGWAN.COM) 6

MyISAM Storage Engine Contd..

• On disk, MySQL represents each MyISAM table using three files:

◦ a format file that stores the definition of the table structure,

◦ a data file that stores the contents of table rows, and

◦ an index file that stores any indexes on the table.

Files for a table named mytable are called mytable.frm, mytable.MYD, and mytable.MYI.

• MyISAM tables can be used to set up MERGE tables.

• MyISAM tables can be converted into fast, compressed, read-only tables to save space.

R.N. SANGWAN (WWW.RNSANGWAN.COM) 7

MyISAM Storage Engine Contd..

• Query performance is very fast for retrievals.

• Multiple queries can read the same table simultaneously.

• You can influence the scheduling mechanism for queries thatuse MyISAM tables by using LOW_PRIORITY/HIGH_PRIORITY Query Modifier.

• You can specify that a MyISAM table must be able to hold at least a certainnumber of rows, which allows MyISAM to adjust the table's internal rowpointer size accordingly.

R.N. SANGWAN (WWW.RNSANGWAN.COM) 8

MyISAM Storage Engine Contd..

• When loading data into an empty MyISAM table, you can disable updating of non-unique indexes and enable the indexes after loading.

• If you run out of disk space while adding rows to a MyISAM table, no error occurs. The server suspends the operation until space becomes available, and then completes the operation.

• MyISAM tables use the indexed sequential access method for indexing.

R.N. SANGWAN (WWW.RNSANGWAN.COM) 9

MyISAM Storage Engine Contd..

• MyISAM locking occurs at the table level. Deadlock cannot occur with tablelocking as it can row locking.

• When processing queries on MyISAM tables, the server manages contention forthe tables by simultaneous clients by implicitly acquiring any locks it needs.

• You can also lock tables explicitly with the LOCK TABLES and UNLOCKTABLES statements.

• MyISAM tables support concurrent inserts.

• Concurrent inserts can take place even for a table that has been read-lockedexplicitly if the locking client acquired a READ LOCAL lock rather than a regularREAD lock.

R.N. SANGWAN (WWW.RNSANGWAN.COM) 10

MyISAM Storage Engine Contd..

• If a table does have holes, concurrent inserts cannot be performed. However,you can remove the holes by using OPTIMIZE TABLE to defragment the table.

• For applications that use MyISAM tables, you can change the priority ofstatements that retrieve or modify data.

• By default, the server schedules queries for execution as follows :

◦ Write requests take priority over read requests.

◦ The server tries to perform write requests in the order that it receives them.

• However, if a table is being read from when a write request arrives, the writerequest cannot be processed until all current readers have finished.

R.N. SANGWAN (WWW.RNSANGWAN.COM) 11

When working with MyISAM tables..

Certain scheduling modifiers are available to change the priority of requests:

◦ The LOW_PRIORITY modifier may be applied to statements that update tables(INSERT, DELETE, REPLACE, or UPDATE).

A low-priority write request waits not only until all current readers have finished, but for any pendingread requests that arrive while the write request itself is waiting.

◦ HIGH_PRIORITY may be used with a SELECT statement to move it ahead of updates and ahead ofother SELECT statements that do not use the HIGH_PRIORITY modifier.

◦ DELAYED may be used with INSERT (and REPLACE). The server buffers the rows in memory and insertsthem when the table is not being used.

◦ Delayed inserts increase efficiency because they're done in batches rather than individually.

◦ Using DELAYED allows the client to proceed immediately after issuing the INSERT statement ratherthan waiting until it completes.

R.N. SANGWAN (WWW.RNSANGWAN.COM) 12

MyISAM Row-Storage Formats

Three storage formats: fixed-row, dynamic-row, and compressed.

Characteristics:

• Fixed-row format:◦ All rows have the same size.

◦ Rows are stored within the table at positions that are multiples of the row size.

◦ Fixed-size rows take more space.

• Dynamic-row format:◦ Rows take varying amounts of space.

◦ Rows cannot be looked up as efficiently.

◦ Dynamic-rows tables usually take less space because rows are not padded to a fixed size.

◦ Fragmentation can occur more easily than for fixed-row tables.

• Compressed format:◦ Tables are packed to save space.

◦ Storage is optimized for quick retrieval.

◦ Tables are read-only.

R.N. SANGWAN (WWW.RNSANGWAN.COM) 13

The MERGE Engine

The MERGE storage engine manages tables that have the following characteristics:

• A MERGE table is a collection of identically structured MyISAM tables. EachMERGE table is represented on disk by an .frm format file and an .MRG file thatlists the names of the constituent MyISAM files.

• Logically, a query on a MERGE table acts as a query on all the MyISAM tables ofwhich it consists.

• A MERGE table creates a logical entity that can exceed the maximumMyISAM table size.

• MySQL manages contention between queries for MERGE table access usingtable-level locking.

R.N. SANGWAN (WWW.RNSANGWAN.COM) 14

The MERGE Engine

• A MERGE table is portable because the .MRG file is a text file andthe MyISAM tables that it names are portable.

• The MERGE engine supports SELECT, DELETE, UPDATE, and INSERT statements.For INSERT, the CREATE TABLE statement can specify whether records should beinserted into the first or last table, or disallowed.

• Disadvantage:

◦ They increase the number of file descriptors required because each of theunderlying tables must be opened along with the MERGE table.

R.N. SANGWAN (WWW.RNSANGWAN.COM) 15

Example of Merge Engine

mysql> CREATE TABLE NACountry SELECT Code, Name

-> FROM Country WHERE Continent ='North America';

mysql> CREATE TABLE SACountry SELECT Code, Name

-> FROM Country WHERE Continent ='South America';

mysql> DESCRIBE NACountry;

mysql> CREATE TABLE NorthAndSouth

-> (Code CHAR(3) NOT NULL, Name CHAR(52) NOT NULL)

-> ENGINE = MERGE UNION = (NACountry, SACountry);

mysql> SELECT COUNT(*) FROM NACountry;

mysql> SELECT COUNT(*) FROM SACountry;

mysql> SELECT COUNT(*) FROM NorthAndSouth;

R.N. SANGWAN (WWW.RNSANGWAN.COM) 16

Other Storage Engines - BDB

• The BDB storage engine provides transactional tables.

◦ Each BDB table is represented on disk by an .frm format file and a .db file that stores data and index information.

◦ BDB supports transactions with full ACID compliance.

◦ The BDB engine provides auto-recovery after a crash of the MySQL server or the host where the server runs. BDB uses page-level locking.

◦ It's possible for deadlock to occur.

R.N. SANGWAN (WWW.RNSANGWAN.COM) 17

Other Storage Engines Contd..

• The ARCHIVE storage engine provides an efficient way to store large amounts of data when you don't need indexes and need to minimize the amount of disk space used.

◦ This engine supports only SELECT and INSERT operations. SELECT scans the entire table, and INSERT performs compressed inserts.

◦ Each ARCHIVE table is represented on disk by an .frm format file, an .ARZ data file, and an .ARM metadata file.

R.N. SANGWAN (WWW.RNSANGWAN.COM) 18

Other Storage Engines Contd..

• The CSV storage engine stores records as text in the well-known comma-separated valuesformat.

◦ It does not support indexing. Each CSV table is represented on disk by an .frm format file and a .CSV plain text file that contains data rows.

• The BLACKHOLE storage engine creates tables that act as "black holes."

◦ That is, what goes in does not come out.

◦ Data stored in a BLACKHOLE table disappears because the engine simply discards it.

◦ The only disk file associated with a BLACKHOLE table is its .frm format file.

R.N. SANGWAN (WWW.RNSANGWAN.COM) 19

Other Storage Engines Contd..

• The EXAMPLE storage engine does nothing except create tables.

◦ You can't even store any rows in an EXAMPLE table, although this is by design.

◦ The purpose of this engine is to provide simple example code in MySQL source todemonstrates how to get started writing a new storage engine.

R.N. SANGWAN (WWW.RNSANGWAN.COM) 20

Thank You

R.N. SANGWAN (WWW.RNSANGWAN.COM) 21