<< previous page   --   table of contents   --   next page >>
| | | | | | | |
  • Return to Table of Contents
  • Table of Contents

    1. General Information
    2. MySQL Installation
    3. Tutorial Introduction
    4. Database Administration
    5. MySQL Optimisation
    6. MySQL Language Reference
    7. MySQL Table Types
    8. MySQL APIs
    9. Extending MySQL

    Chapter 7:  MySQL Table Types 521 SELECT * FROM CHILD WHERE ID > 100 FOR UPDATE; Suppose there is an index on table  CHILD  on column  ID.  Our query will scan that index starting  from  the   rst  record  where  ID  is  bigger  than  100.   Now,  if  the  locks  set  on  the index records would not lock out inserts made in the gaps, a new child might meanwhile be inserted to the table.  If now I in my transaction execute SELECT * FROM CHILD WHERE ID > 100 FOR UPDATE; again, I will see a new child in the result set the query returns.  This is against the isolation principle of transactions:  a transaction should be able to run so that the data it has read does not change during the transaction.  If we regard a set of rows as a data item, then the new 'phantom' child would break this isolation principle. When InnoDB scans an index it can also lock the gap after the last record in the index. Just that happens in the previous example:  the locks set by InnoDB will prevent any insert to the table where ID would be bigger than 100. You can use next-key locking to implement a uniqueness check in your application:  if you read your data in share mode and do not see a duplicate for a row you are going to insert, then you can safely insert your row and know that the next-key lock set on the successor of your row during the read will prevent anyone meanwhile inserting a duplicate for your row.  Thus the next-key locking allows you to 'lock' the non-existence of something in your table. 7.5.8.4  Locks Set by Di erent SQL Statements in InnoDB    SELECT ... FROM ...  :  this is a consistent read,  reading a snapshot of the database and setting no locks.    SELECT ... FROM ... LOCK IN SHARE MODE  :   sets  shared  next-key  locks  on  all  index records the read encounters.    SELECT ... FROM ... FOR UPDATE  :  sets exclusive next-key locks on all index records the read encounters.    INSERT INTO ... VALUES (...) :  sets an exclusive lock on the inserted row; note that this lock is not a next-key lock and does not prevent other users from inserting to the gap before the inserted row.  If a duplicate key error occurs, sets a shared lock on the duplicate index record.    INSERT INTO T SELECT ... FROM S WHERE ... sets an exclusive (non-next-key) lock on each row inserted into  T.  Does the search on  S  as a consistent read, but sets shared next-key locks on S if the MySQL logging is on.  InnoDB has to set locks in the latter case because in roll-forward recovery from a backup every SQL statement has to be executed in exactly the same way as it was done originally.    CREATE TABLE ... SELECT ... performs the SELECT as a consistent read or with shared locks, like in the previous item.    REPLACE  is done like an insert if there is no collision on a unique key.  Otherwise, an exclusive next-key lock is placed on the row which has to be updated.    UPDATE ... SET ... WHERE ...  :  sets an exclusive next-key lock on every record the search encounters.
     

    Customer Support CentreMySQL Reference Manual

    Web Hosting Services
    UNIX WEB HOSTING
    MERCHANT ACCOUNTS
    DEDICATED SERVERS
    E-COMMERCE HOSTING
    SUPPORT & FAQ's
    TERMS OF USE
    Domain Services
    DOMAIN
    REGISTRATION
    MANAGE
    YOUR ACCOUNT
    SUPPORT & FAQ's
    TERMS OF USE
    Corporate Info
    ABOUT US
    OUR NETWORK
    CONTACT US
    SITE MAP
    Copyright © 2002 Dyntex Group, Inc. All Rights Reserved
  • Return to Table of Contents
  • Back to top

  • Web Hosting: Manuals & FAQ's

    1. Unix-Based Web Hosting
    2. Unix Dedicated Servers
    3. Windows Dedicated Servers
    4. CuteFTP User’s Guide
    5. CuteHTML User’s Guide
    6. WS_FTP Pro User's Guide
    7. Miva Order User's Guide
    8. Miva Merchant User's Guide