Showing posts with label ROW. Show all posts
Showing posts with label ROW. Show all posts

Wednesday, July 10, 2013

Know about Locking Mechanism in MySQL Storage Engines



We all  know MySQL has different types of locking for different storage engines.It will vary depends upon the storage engines which you are using.Here I have explained how the locking mechanism will work.


There are three types of locking in MySQL,

1.Table level locking,
2.Row level locking and 
3.Page level locking.

 Table level locking:

Example1:Assume that there is a house with 5 rooms(1,2,3,4,5) and it is having 3 floors(x,y,z) and each houses are having only one  main door and each floor is having separate door to go inside the floor.And all the doors are automated when one person goes inside it will lock until the person is leaving from the house,floor and room.Here house is a table and rooms are rows and floors are pages.when a person is going inside the house name A and it will locked until the person open the door.

Likewise In MyISAM engine when one query is running on a particular table it will lock the entire table(likewise house A locked by a person) the remaining queries on the particular table it should be wait until the lock release it.This is called table level locking.

 If a query accesses the table it will lock the entire table and not allow access to the table from other queries.  The benefit of this is that it entirely eliminates deadlocking issues. The disadvantage is that, as mentioned, no other queries have access to the table while it is locked.  If you had a table with 20,000,000 rows and needed to modify one row, the entire table is inaccessible by other queries. The MyISAM and memory storage engine use table-level locking.

Row level locking:

Example2:In a single house we are having 5 rooms(Rows) when any one want to go inside the room 2 that person can access it if some other person want to use the remaining room they can also use the remaining rooms 1,3,4,5.here when one person is trying to access the same room which it was used by some person this new person have to wait. likewise in InnoDB engine when any query is doing a process in a table it will not lock the entire table.It will lock only the particular row in the table.It is called as Row level locking.when another query want to use the same row we will see the status as "table metadata lock" in process list.

Row level acquires a lock on as small an amount as a single row from a table. This will block the minimal amount of table content and allows for the most concurrency on a table without problems. InnoDB and Falcon both use row-level locking.

Page level locking:

Example3: In the house we have 3 floors(Pages) in each floor it is having five rooms.When a person want to go inside the 3rd floor 5th room that person can go inside the room but the 3rd floor will lock until the person leaving from the room.when any person is trying to go 3rd floor it was locked by the 1st person when the person comes out only other person can access it.like wise in BDB storage engine when any process happening in a particular row it locks the full Page until finish the process no any other query can access it until the lock release.Page is depends upon the size what you have given the defaults size is 16K.

Page-level locking is locking of a group of rows instead of a the entire table. The number of rows actually locked will vary based on a number of factors. Going back to our example of a 20,000,000-row table, lets assume a page-level lock is used.  If a page consists of 1,000 rows (this would vary depending on the size of the rows and the actual amount of memory allocated to a page), a lock would lock only a thousand rows.  Any of the other 19,999,000 rows could be used by other queries without interference. The BDB storage engine uses page-level locking.



Wednesday, February 13, 2013

KNOW ABOUT MYSQL BINLOG FORMATS

What is binlog?

           In MySQL the binary log record the events that, when ever the changes happens in the databases. (eg.create,delete,update,insert)

What is binlog formats?

          The format that has been used to record the changes of data in binlog.

What are the their types?

          There are three types of binlog formats.

                                         1) Statement

                                         2) Row

                                         3) Mixed

1) Statement

     It is the default binlog format for MySQL5.6.

     It records the events in SQL statement in binlog to read easily with
     mysqlbinlog.

    The binlog does not grow so fast than row format.

    Faster to recover from a backup.

how it works?
  • set the binlog format to statement if you use older version of MySQL 5.6 in my.cnf file.
  • restart MySQL 
  • create a database example india.
  • mysql> create database india;
    Query OK, 1 row affected (0.00 sec)

    mysql> use india;
    Database changed
    mysql> create table states(id int not null auto_increment,statename varchar(25),country varchar(25),primary key(id));
    Query OK, 0 rows affected (0.18 sec)
  •  insert the details and use update query it must be affect all the rows in the column.
  • Then flush the logs.
  • use the old binlog before the new binlog.
  • Now use mysqlbinlog to view the events recorded in binlog.
  • Ex:
    shell> mysqlbinlog /path/mysql-bin.000007|less
     
  • The insert and update query that recorded in mysqlbinlog is. 
  • # at 1457
    #130213 18:05:44 server id 1  end_log_pos 1589  Query   thread_id=1     exec_time=0     error_code=0
    SET TIMESTAMP=1360758944/*!*/;
    insert into states (statename,country)values('Chattisgarh','India')
    # at 1934
    #130213 18:05:55 server id 1  end_log_pos 1838  Query   thread_id=1     exec_time=0    error_code=0
    SET TIMESTAMP=1360758955/*!*/;
    insert into states (statename,country)values('Delhi','India')
    # at 1934
    #130213 18:11:03 server id 1 end_log_pos 2037 Query thread_id=1 exec_time=0 error_code=0
    SET TIMESTAMP=1360759263/*!*/;
    update states set country='Great India'
    /*!*/;
    # at 2037
    #130213 18:11:03 server id 1 end_log_pos 2064 Xid = 24
    COMMIT/*!*/;
    # at 2064
    #130213 18:11:56 server id 1 end_log_pos 2107 Rotate to mysql-bin.000011 pos: 4
    DELIMITER ;
    # End of log file
    ROLLBACK /* added by mysqlbinlog */;
    /*!50003 COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/; 
In this the events are recorded in SQL statement.So it is very easily to read when point time recovery.

2) Row

     It records each row of modification in binlog.

     It will record each and every thing that create,delete,update in the datas.

     The binlog grow faster than statement format.

  • set the binlog format to row format in my.cnf file.
  • restart MySQL 
  • create a database example india.
  • mysql> create database india;
    Query OK, 1 row affected (0.00 sec)

    mysql> use india;
    Database changed
    mysql> create table states(id int not null auto_increment,statename varchar(25),country varchar(25),primary key(id));
    Query OK, 0 rows affected (0.18 sec)
  • Insert the details and use update query it must be affect all the rows in the column.
  • Then flush the logs.
  • use the old binlog before the new binlog.
  • Now use mysqlbinlog to view the events recorded in binlog.
  • Ex:
    shell>mysqlbinlog /path/mysql-bin.000007|less
     
  • The insert and update query that recorded in mysqlbinlog is. 
  • # at 1514
    # at 1566
    #130213 17:20:26 server id 1 end_log_pos 1566 Table_map: `india`.`states` mapped to number 34
    #130213 17:20:26 server id 1 end_log_pos 1850 Update_rows: table id 34 flags: STMT_END_F
    BINLOG '
    An4bURMBAAAANAAAAB4GAAAAACIAAAAAAAEABWluZGlhAAZzdG
    0ZXMAAwMPDwQZABkABg==
    An4bURgBAAAAHAEAADoHAAAAACIAAAAAAAEAA///+AEAAAANQW
    5kcmgUHJhZGVzaAVJbmRpYfgB
    AAAADUFuZHJhIFByYWRlc2gLR3JlYXQgSW5kaWH4AgAAABFBcn
    VuYWNoYWwgUHJhZGVzaAVJbmRp
    YfgCAAAAEUFydW5hY2hhbCBQcmFkZXNoC0dyZWF0IEluZGlh+AM
    AAAAFQXNzYW0FSW5kaWH4AwAA
    AAVBc3NhbQtHcmVhdCBJbmRpYfgEAAAABUJpaGFyBUluZGlh+AQ
    AAAAFQmloYXILR3JlYXQgSW5k
    aWH4BQAAAAxDaGhhdHRpc2dhcmgFSW5kaWH4BQAAAAxDaGhh
    dHRpc2dhcmgLR3JlYXQgSW5kaWE=
    '/*!*/;
    # at 1850
    #130213 17:20:26 server id 1 end_log_pos 1877 Xid = 19
    COMMIT/*!*/;
  • we cant understand when we use  mysqlbinlog /path/mysql-bin.000007 for row format. so to view and identify the row format in binlog.There are two ways to read row format in binlog.
  • --verbose,-v: It Reconstruct row events and display them as commented SQL statements. If this option is given twice, the output includes comments to indicate column data types and some metadata. 
  •  --base64-output=decode-rows: This option determines when events should be displayed encoded as base-64 strings using  BINLOG statements.
  •  Eg
    shell>mysqlbinlog -v /path/mysql-bin.000007|less
     or
    shell>mysqlbinlog -v --base64-output=decode-rows mysql-bin.000007|less
     
  • The insert query that recorded in mysqlbinlog is. 
  • # at 517
    # at 569
    #130213 17:15:18 server id 1 end_log_pos 569 Table_map: `india`.`states` mapped to number 34
    #130213 17:15:18 server id 1 end_log_pos 623 Write_rows: table id 34 flags: STMT_END_F
    BINLOG '
    znwbURMBAAAANAAAADkCAAAAACIAAAAAAAEABWluZGlhAAZzdGF0Z
    XMAAwMPDwQZABkABg==
    znwbURcBAAAANgAAAG8CAAAAACIAAAAAAAEAA//4AQAAAA1BbmRyY
    SBQcmFkZXNoBUluZGlh
    '/*!*/;
    ### INSERT INTO india.states
    ### SET
    ### @1=1
    ### @2='Andra Pradesh'
    ### @3='India'
    # at 623
    #130213 17:15:18 server id 1 end_log_pos 650 Xid = 12
    COMMIT/*!*/; 
    -->
    # at 719
    # at 771
    #130213 17:15:52 server id 1 end_log_pos 771 Table_map: `india`.`states` mapped to number 34
    #130213 17:15:52 server id 1 end_log_pos 829 Write_rows: table id 34 flags: STMT_END_F
    BINLOG '
    8HwbURMBAAAANAAAAAMDAAAAACIAAAAAAAEABWluZGlhAAZzdGF
    0ZXMAAwMPDwQZABkABg==
    8HwbURcBAAAAOgAAAD0DAAAAACIAAAAAAAEAA//4AgAAABFBcnVu
    YWNoYWwgUHJhZGVzaAVJbmRp
    YQ==
    '/*!*/;
    ### INSERT INTO india.states
    ### SET
    ### @1=2
    ### @2='Arunachal Pradesh'
    ### @3='India'
    # at 829
    #130213 17:15:52 server id 1 end_log_pos 856 Xid = 13
    COMMIT/*!*/;
      Here the insert statement are recorded in row format.


      The update query that recorded in mysqlbinlog is. 
  • BINLOG '
    An4bURMBAAAANAAAAB4GAAAAACIAAAAAAAEABWluZGlhAAZzdGF0
    ZXMAAwMPDwQZABkABg==
    An4bURgBAAAAHAEAADoHAAAAACIAAAAAAAEAA///+AEAAAANQW5k
    cmEgUHJhZGVzaAVJbmRpYfgB 
    AAAADUFuZHJhIFByYWRlc2gLR3JlYXQgSW5kaWH4AgAAABFBcnVuYW
    NoYWwgUHJhZGVzaAVJbmRp
    YfgCAAAAEUFydW5hY2hhbCBQcmFkZXNoC0dyZWF0IEluZGlh+AMAAA
    AFQXNzYW0FSW5kaWH4AwAA
    AAVBc3NhbQtHcmVhdCBJbmRpYfgEAAAABUJpaGFyBUluZGlh+AQAAA
    AFQmloYXILR3JlYXQgSW5k
    aWH4BQAAAAxDaGhhdHRpc2dhcmgFSW5kaWH4BQAAAAxDaGhhdHR
    pc2dhcmgLR3JlYXQgSW5kaWE=
    '/*!*/;
    ### UPDATE india.states
    ### WHERE
    ### @1=1
    ### @2='Andra Pradesh'
    ### @3='India'
    ### SET
    ### @1=1
    ### @2='Andra Pradesh'
    ### @3='Great India'
    ### UPDATE india.states
    ### WHERE
    ### @1=2
    ### @2='Arunachal Pradesh'
    ### @3='India'
    ### SET
    ### @1=2
    ### @2='Arunachal Pradesh'
    ### @3='Great India'
    ### UPDATE india.states
    ### WHERE
    ### @1=3
    ### @2='Assam'
    ### @3='India'
    ### SET
    ### SET
    ### @1=3
    ### @2='Assam'
    ### @3='Great India'
    ### UPDATE india.states
    ### WHERE
    ### @1=4
    ### @2='Bihar'
    ### @3='India'
    ### SET
    ### @1=4
    ### @2='Bihar'
    ### @3='Great India'
    ### UPDATE india.states
    ### WHERE
    ### @1=5
    ### @2='Chhattisgarh'
    ### @3='India'
    ### SET
    ### @1=5
    ### @2='Chhattisgarh'
    ### @3='Great India'
    # at 1850
    #130213 17:20:26 server id 1 end_log_pos 1877 Xid = 19
    COMMIT/*!*/;
    # at 1877
    #130213 17:25:01 server id 1 end_log_pos 1896 Stop
    DELIMITER ;
    # End of log file
    ROLLBACK /* added by mysqlbinlog */;
    /*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
    (END)

     Here the update query was recorded by each and every row affected in the
     table.

     So that binlog will grow faster than statement format.

3) Mixed

     It combines of both statement and row format.

     In this statement format is the default one for mixed format.

     The row format will change automatically when it needs.
     More details : http://dev.mysql.com/doc/refman/5.1/en/binary-log-mixed.html