Understanding MySql locking issues


Published on

When you start getting MySQL IOPS errors then you should look for some of the Major Locking issues which is described here

  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Understanding MySql locking issues

  1. 1. “Understanding MySQLLocking Issues”
  2. 2. Contents Server Variables Query Cache Locking Issues Internal Locking External Locking Concurrent Inserts Connection Pooling
  3. 3. Server VariablesThe MySQL server maintains many system variablesthat indicate how it is configured.• Each system variable has its default value.• We can dynamically change these settings usingSET command.• SHOW VARIABLES• Currently there are 291 variables defined inMySQL 5.1.6• SHOW VARIABLES LIKE have_query_cache;• SHOW STATUS LIKE Table%;
  4. 4. Query Cache• The have_query_cache server system variableindicates whether the query cache is available.• SHOW VARIABLES LIKE have_query_cache;• It totally depends upon query_cache_type andquery_cache_size• query_cache_type is of 0,1 and 2 (On DEMAND)• You can set max query_cache_size (1MB) aswell as query_cache_min_res_unit (4KB)• SHOW STATUS LIKE %qcache%;
  5. 5. Locking Issues• SHOW STATUS LIKE Table%;To achieve a very high lock speed, MySQL uses tablelocking (instead of page, row, or column locking)for all storage engines except InnoDB andNDBCLUSTER.• Internal Locking• External Locking• Concurrent Inserts
  6. 6. Internal LockingPerformed within the MySQL server itself to managecontention for table contents by multiple sessions.MySQL grants table write locks as follows:-If there are no locks on the table, put a write lockon it.Otherwise, put the lock request in the writelock queue.MySQL grants table read locks as follows:-If there are no write locks on the table, put a readlock on it.Otherwise, put the lock request in theread lock queue.
  7. 7. External Locking• External locking is used in situations where asingle process such as the MySQL server cannotbe assumed to be the only process that requiresaccess to tables.• If you run multiple servers that use the samedatabase directory, each server must haveexternal locking enabled.• With external locking in effect, each processthat requires access to a table acquires a filesystem lock for the table files before proceedingto access the table.
  8. 8. External Locking• External locking affects server performancebecause the server must sometimes wait forother processes before it can access tables.• The easiest way to satisfy these conditions is toalways use --external-locking together with• skip_external_locking = OFF• SET delay-key-write=OFF• SET query-cache-size=0.
  9. 9. Concurrent Inserts• The MySQL storage engine supportsconcurrent inserts to reduce contentionbetween readers and writers for a given table• An INSERT statement can be executed to addrows to the end of the table at the same timethat SELECT statements are reading rowsfrom the table.• If there are multiple INSERT statements,they are queued and performed in sequence,concurrently with the SELECT statements.
  10. 10. Concurrent Inserts- concurrent_insert = 0, 1, 20) If concurrent_insert is set to 0, concurrentinserts are disabled.1) By default, the variable is set to 1 andconcurrent inserts are handled.2) If the variable is set to 2, concurrent inserts atthe end of the table are permitted even fortables that have deleted rows(holes).
  11. 11. Connection Pooling• By default, connection pooling is enabled inADO.NET, Unless you explicitly disable it.• Only connections with the same configurationcan be pooled. ADO.NET keeps several pools atthe same time, one for each configuration.• Connections are separated into pools byconnection string, and by Windows identity whenintegrated security is used.• A connection pool is created for each uniqueconnection string.
  12. 12. Connection Pooling- Connections are added to the pool as needed, up tothe maximum pool size specified (100 is thedefault).- Connections are released back into the pool whenthey are closed or disposed.- If MinPoolSize is either not specified in theconnection string or is specified as 0, theconnections in the pool will be closed after aperiod of inactivity.- However, if the specified MinPoolSize is greaterthan zero, the connection pool is not destroyeduntil the AppDomain is unloaded and the process
  13. 13. Take Away