MySQL Performance Talk
May 29 2012
Eli Aschkenasy
@EliAschkenasy
Check list
 DB engine selection
Engines:
MyISAM vs. INNODB
Source: Oracle Jan, 2011
Source: Oracle Jan, 2011
Source: Oracle Jan, 2011
Source: Oracle Jan, 2011
Source: Google (google fix) 1Q, 2010
Check list
 DB engine selection
 Server update
Source: Giuseppe Maxia - MySQL Community Team Lead 1Q, 2010
Source: Giuseppe Maxia - MySQL Community Team Lead 1Q, 2010
Source: Giuseppe Maxia - MySQL Community Team Lead 1Q, 2010
Bottom line:
Newer ISbetter
Check list
 DB engine selection
 Server update
 Data types
Data Types Rules:
 Smaller is better (usually)
 Avoid NULL if possible (index/comparison)
 Don’t forget about TINYINT and (UN)SIGNED
 ALL INT calculations create 64bit BIGINT (storage is the only difference)
 Width identifier is meaningless – INT(1) occupies the same space as INT(20)
 VARCHAR vs. CHAR – fixed allocation of space (CHAR)
 Downside of VARCHAR is that the row might grow.
 BLOB and TEXT are identical other than collation and character set information
 ENUM
Data Types Rules:
Data Types Rules:
 Smaller is better (usually)
 Avoid NULL if possible (index/comparison)
 Don’t forget about TINYINT and (UN)SIGNED
 ALL INT calculations create 64bit BIGINT (storage is the only difference)
 Width identifier is meaningless – INT(1) occupies the same space as INT(20)
 VARCHAR vs. CHAR – fixed allocation of space (CHAR)
 Downside of VARCHAR is that the row might grow.
 BLOB and TEXT are identical other than collation and character set information
 ENUM (Don’t use)
 DATE vs. DATETIME
Check list
 DB engine selection
 Server update
 Data types
 Counter tables
mysql > SELECT COUNT(*) FROM asset_table;
mysql > CREATE TABLE asset_counter (
cnt INT UNSIGNED NOT NULL
) ENGINE=InnoDB;
On addition:
UPDATE asset_counter SET cnt = cnt+1;
mysql > CREATE TABLE asset_counter (
slot TINYINT UNSIGNED NOT NULL PRIMARY KEY,
cnt INT UNSIGNED NOT NULL
) ENGINE=InnoDB;
Pre-populate 100 rows (slot will be 1 – 100)
On addition:
UPDATE asset_counter SET cnt = cnt+1 WHERE slot = RAND() * 100;
Check list
 DB engine selection
 Server update
 Data types
 Counter tables
 Indexing (even for ORMs)
Indexing 1: hash – (for lookup tables)
InnoDB creates this automatically for frequently accessed index values.
Indexing 2: selectivity – (multi column indexes)
Check list
 DB engine selection
 Server update
 Data types
 Counter tables
 Indexing (even for ORMs)
 Query optimization
Query optimization:
Optimize data AMOUNT
Use Limit in your Query (instead of the application code)
Deadly * - Do you really need ALL data from a row ?!?
Double data – same resource in multiple queries
Reduce DB workload
Use Indexes!
Use a covering index (remember selectivity?)
Complex query vs. Many simple queries
Query optimization:
Query optimization:
Query optimization:
Bottom (bottom) line:
Always check real data!

MySQL Performance Optimization #NEOJUG12

  • 1.
    MySQL Performance Talk May29 2012 Eli Aschkenasy @EliAschkenasy
  • 2.
    Check list  DBengine selection
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
    Check list  DBengine selection  Server update
  • 10.
    Source: Giuseppe Maxia- MySQL Community Team Lead 1Q, 2010
  • 11.
    Source: Giuseppe Maxia- MySQL Community Team Lead 1Q, 2010
  • 12.
    Source: Giuseppe Maxia- MySQL Community Team Lead 1Q, 2010
  • 13.
  • 14.
    Check list  DBengine selection  Server update  Data types
  • 15.
    Data Types Rules: Smaller is better (usually)  Avoid NULL if possible (index/comparison)  Don’t forget about TINYINT and (UN)SIGNED  ALL INT calculations create 64bit BIGINT (storage is the only difference)  Width identifier is meaningless – INT(1) occupies the same space as INT(20)  VARCHAR vs. CHAR – fixed allocation of space (CHAR)  Downside of VARCHAR is that the row might grow.  BLOB and TEXT are identical other than collation and character set information  ENUM
  • 16.
  • 17.
    Data Types Rules: Smaller is better (usually)  Avoid NULL if possible (index/comparison)  Don’t forget about TINYINT and (UN)SIGNED  ALL INT calculations create 64bit BIGINT (storage is the only difference)  Width identifier is meaningless – INT(1) occupies the same space as INT(20)  VARCHAR vs. CHAR – fixed allocation of space (CHAR)  Downside of VARCHAR is that the row might grow.  BLOB and TEXT are identical other than collation and character set information  ENUM (Don’t use)  DATE vs. DATETIME
  • 18.
    Check list  DBengine selection  Server update  Data types  Counter tables
  • 19.
    mysql > SELECTCOUNT(*) FROM asset_table; mysql > CREATE TABLE asset_counter ( cnt INT UNSIGNED NOT NULL ) ENGINE=InnoDB; On addition: UPDATE asset_counter SET cnt = cnt+1; mysql > CREATE TABLE asset_counter ( slot TINYINT UNSIGNED NOT NULL PRIMARY KEY, cnt INT UNSIGNED NOT NULL ) ENGINE=InnoDB; Pre-populate 100 rows (slot will be 1 – 100) On addition: UPDATE asset_counter SET cnt = cnt+1 WHERE slot = RAND() * 100;
  • 20.
    Check list  DBengine selection  Server update  Data types  Counter tables  Indexing (even for ORMs)
  • 21.
    Indexing 1: hash– (for lookup tables) InnoDB creates this automatically for frequently accessed index values.
  • 22.
    Indexing 2: selectivity– (multi column indexes)
  • 23.
    Check list  DBengine selection  Server update  Data types  Counter tables  Indexing (even for ORMs)  Query optimization
  • 24.
    Query optimization: Optimize dataAMOUNT Use Limit in your Query (instead of the application code) Deadly * - Do you really need ALL data from a row ?!? Double data – same resource in multiple queries Reduce DB workload Use Indexes! Use a covering index (remember selectivity?) Complex query vs. Many simple queries
  • 25.
  • 26.
  • 27.
  • 28.