MySQL
  Home arrow MySQL arrow Page 5 - Storage Engine (Table Types)
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Actuate Whitepapers 
VeriSign Whitepapers 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
MYSQL

Storage Engine (Table Types)
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 46
    2004-08-02

    Table of Contents:
  • Storage Engine (Table Types)
  • Locking
  • Multi-Version Concurrency Control
  • Transactions
  • Bene
  • Deadlocks
  • Transactions in MySQL
  • Selecting the Right Engine
  • Practical Examples
  • Table Conversions
  • The Storage Engines
  • MyISAM Tables
  • Compressed MyISAM Tables
  • InnoDB Tables
  • Heap (In-Memory) Tables

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Storage Engine (Table Types) - Bene


    (Page 5 of 15 )

    ACID transactions ensure that banks don’t lose your money. By wrapping arbitrarily complex logic into single units of work, the database server takes some of the burden off application developers. The database server’s ACID properties offer guarantees that reduce the need for code guarding against race conditions and handling crash recovery.

    The downside of this extra security is that the database server has to do more work. It also means that a database server with ACID transactions will generally require more CPU power, memory, and disk space than one without them. As mentioned earlier, this is where MySQL’s modularity comes into play. Because you can decide on a per-table basis if you need ACID transactions or not, you don’t need to pay the performance penalty on a table that really won’t benefit from transactions.

    Isolation Levels

    The previous description of isolation was a bit simplistic. Isolation is more complex than it might first appear because of some peculiar cases that can occur. The SQL standard defines four isolation levels with specific rules for which changes are and aren’t visible inside and outside a transaction. Let’s look at each isolation level and the type of problems that can occur.

    Read uncommitted

    In the read uncommitted isolation level, transactions can view the results of uncommitted transactions. At this level, many problems can occur unless you really, really know what you are doing and have a good reason for doing it. Read uncommitted is rarely used in practice. Reading uncommitted data is also known as a dirty read.

    Read committed

    The default isolation level for most database systems is read committed. It satisfies the simple definition of isolation used earlier. A transaction will see the results only of transactions that were already committed when it began, and its changes won’t be visible to others until it’s committed.

    However, there are problems that can occur using that definition. To visualize the problems, refer to the sample data for the Stock and StockPrice tables as shown in Tables 2-2 and 2-3.

    id

    Ticker

    Name

    1

    MSFT

    Microsoft

    2

    EBAY

    eBay

    3

    YHOO

    Yahoo!

    4

    AMZN

    Amazon

    Table 2-2. The Stock table.

    stock_id

    date

    open

    high

    low

    close

    1

    2002-05-01

    21.25

    22.30

    20.18

    21.30

    2

    2002-05-01

    10.01

    10.20

    10.01

    10.18

    3

    2002-05-01

    18.23

    19.12

    18.10

    19.00

    4

    2002-05-01

    45.55

    46.99

    44.87

    45.71

    1

    2002-05-02

    21.30

    21.45

    20.02

    20.21

    2

    2002-05-02

    10.18

    10.55

    10.10

    10.35

    3

    2002-05-02

    19.01

    19.88

    19.01

    19.22

    4

    2002-05-02

    45.69

    45.69

    44.03

    44.30

    Table 2-3. The StockPrice table

    Imagine you have a Perl script that runs nightly to fetch price data about your favorite stocks. For each stock, it fetches the data and adds a record to the StockPrice

    table with the day’s numbers. So to update the information for Amazon.com, the transaction might look like this:

    BEGIN;
    SELECT @id := id FROM Stock WHERE ticker = 'AMZN';
    INSERT INTO StockPrice VALUES (@id, '2002-05-03', 20.50, 21.10, 20.08, 21.02);
    COMMIT;

    But what if, between the select and insert, Amazon’s id changes from 4 to 17 and a new stock is added with id 4? Or what if Amazon is removed entirely? You’ll end up inserting a record with the wrong id in the first case. And in the second case, you’ve inserted a record for which there is no longer a corresponding row in the Stock table. Neither of these is what you intended.

    The problem is that you have a nonrepeatable read in the query. That is, the data you read in the SELECT becomes invalid by the time you execute the INSERT. The repeatable read isolation level exists to solve this problem.

    Repeatable read

    At the repeatable read isolation level, any rows that are read during a transaction are locked so that they can’t be changed until the transaction finishes. This provides the perfect solution to the problem mentioned in the previous section, in which Ama-zon’s id can change or vanish entirely. However, this isolation level still leaves the door open to another tricky problem: phantom reads.

    Using the same data, imagine that you have a script that performs some analysis based on the data in the StockPrice table. And let’s assume it does this while the nightly update is also running.

    The analysis script does something like this:

    BEGIN;
    SELECT * FROM StockPrice WHERE close BETWEEN 10 and 20;
    // think for a bit
    SELECT * FROM StockPrice WHERE close BETWEEN 10 and 20; COMMIT;

    But the nightly update script inserts between those two queries new rows that happen to match the close BETWEEN 10 and 20 condition. The second query will find more rows that the first one! These additional rows are known as phantom rows (or simply phantoms). They weren’t locked the first time because they didn’t exist when the query ran.

    Having said all that, we need to point out that this is a bit more academic than you might think. Phantom rows are such a common problem that InnoDB’s locking (known as next-key locking) prevents this from happening. Rather than locking only the rows you’ve touched in a query, InnoDB actually locks the slot following them in the index structure as well.

    Serializable

    The highest level of isolation, serializable, solves the phantom read problem by ordering transactions so that they can’t conflict. At this level, a lot of timeouts and lock contention may occur, but the needs of your application may bring you to accept the decreased performance in favor of the data stability that results.

    Table 2-2 summarizes the various isolation levels and the drawbacks associated with each one. Keep in mind that as you move down the list, you’re sacrificing concurrency and performance for increased safety.

    Isolation level

    Dirty reads possible

    Non-repeatable reads possible

    Phantom reads possible

    Read uncommitted

    Yes

    Yes

    Yes

    Read committed

    No

    Yes

    Yes

    Repeatable read

    No

    No

    Yes

    Serializable

    No

    No

    No

    Table 2-4. ANSI SQL isolation levels

    Buy the book!If you've enjoyed what you've seen here, or to get more information, click on the "Buy the book!" graphic. Pick up a copy today!

    Visit the O'Reilly Network http://www.oreillynet.com for more online content.

    More MySQL Articles
    More By O'Reilly Media


       · Where is figure 2-1?
       · We will look into this ASAP.
       · Fixed
       · I don't think, that this sentence is quite acurate"MVCC can be thought of as a...
     

       

    MYSQL ARTICLES

    - MySQL Table Prefix Changer Tool in PHP
    - Using the SIGNAL Statement for Error Handling
    - Error Handling Examples
    - Error Handling
    - Completing a Search Engine with MySQL and PH...
    - Paginating Result Sets for a Search Engine B...
    - Building a Search Engine with MySQL and PHP 5
    - Using Boolean Operators for Full Text and Bo...
    - PHP, MySQL and the PEAR Database
    - Working with PHP and MySQL
    - Getting PHP to Talk to MySQL
    - Creating an RSS Reader: the Reader
    - MySQL Security Overview
    - Creating the Admin Script for a PHP/MySQL Bl...
    - Creating the Blog Script for a PHP/MySQL Blo...

    BlackBerry VTS




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway