MySQL Idiosyncrasies That Bite SF

Ronald Bradford
Ronald BradfordPrincipal - Data architecture, performance and scalability for MySQL solutions.
Title




        MySQL Idiosyncrasies
            that BITE
                                Oracle Open World 2010
                                    MySQL Sunday

                                       2010.09




Ronald Bradford
http://ronaldbradford.com - 2010.09
   MySQL Idiosyncrasies That BITE        #oow10 #mysql @RonaldBradford
Definitions




     idiosyncrasy
     [id-ee-uh-sing-kruh-see, -sin-] –noun,plural-sies.



     A characteristic, habit, mannerism, or the
     like, that is peculiar to ...

                                           http://dictionary.com

MySQL Idiosyncrasies That BITE - 2010.09
About RDBMS - Oracle != MySQL




     Product Age
     Development philosophies
     Target audience
     Developer practices
     Installed versions



MySQL Idiosyncrasies That BITE - 2010.09
Expected RDBMS Characteristics




     Data Integrity
     Transactions
     ACID
     Data Security
     ANSI SQL



MySQL Idiosyncrasies That BITE - 2010.09
These RDBMS characteristics
 are NOT enabled by default
                  (*)
        in MySQL


MySQL Idiosyncrasies That BITE - 2010.09
WARNING !!!



These RDBMS characteristics
 are NOT enabled by default
                  (*)
        in MySQL


MySQL Idiosyncrasies That BITE - 2010.09
MySQL Terminology




     Storage Engines
          MyISAM - Default
          InnoDB - Default from MySQL 5.5
     Transactions
          Non Transactional Engine(s)
          Transactional Engine(s)


MySQL Idiosyncrasies That BITE - 2010.09
1. Data Integrity


MySQL Idiosyncrasies That BITE - 2010.09
Data Integrity - Expected




CREATE TABLE sample_data (
  i TINYINT UNSIGNED NOT NULL,
                                                       ✔
  c CHAR(2) NULL,
  t TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET latin1;

INSERT INTO sample_data(i) VALUES (0), (100), (255);

SELECT * FROM sample_data;
+-----+------+---------------------+
| i   | c    | t                   |
+-----+------+---------------------+
|   0 | NULL | 2010-06-06 13:28:44 |
| 100 | NULL | 2010-06-06 13:28:44 |
| 255 | NULL | 2010-06-06 13:28:44 |
+-----+------+---------------------+



MySQL Idiosyncrasies That BITE - 2010.09
Data Integrity - Unexpected




                                                      ✘
mysql> INSERT INTO sample_data (i) VALUES (-1), (9000);

mysql> SELECT * FROM sample_data;
+-----+------+---------------------+
| i   | c    | t                   |
+-----+------+---------------------+
|   0 | NULL | 2010-06-06 13:28:44 |
| 100 | NULL | 2010-06-06 13:28:44 |
| 255 | NULL | 2010-06-06 13:28:44 |
|   0 | NULL | 2010-06-06 13:32:52 |
| 255 | NULL | 2010-06-06 13:32:52 |
+-----+------+---------------------+
5 rows in set (0.01 sec)




MySQL Idiosyncrasies That BITE - 2010.09
Data Integrity - Unexpected




mysql> INSERT INTO sample_data (i) VALUES (-1), (9000);  ✘
mysql> SELECT * FROM sample_data;
+-----+------+---------------------+
| i   | c    | t                   |
+-----+------+---------------------+
|   0 | NULL | 2010-06-06 13:28:44 |
| 100 | NULL | 2010-06-06 13:28:44 |
| 255 | NULL | 2010-06-06 13:28:44 |
|   0 | NULL | 2010-06-06 13:32:52 |
| 255 | NULL | 2010-06-06 13:32:52 |
+-----+------+---------------------+
5 rows in set (0.01 sec)

                                               TINYINT is 1 byte.
                                              UNSIGNED supports
                                                 values 0-255


MySQL Idiosyncrasies That BITE - 2010.09
Data Integrity - Warnings




mysql> INSERT INTO sample_data (i) VALUES (-1), (9000);
Query OK, 2 rows affected, 2 warnings (0.00 sec)
mysql> SHOW WARNINGS;
+---------+------+--------------------------------------------+
| Level   | Code | Message                                    |
+---------+------+--------------------------------------------+
| Warning | 1264 | Out of range value for column 'i' at row 1 |
| Warning | 1264 | Out of range value for column 'i' at row 2 |
+---------+------+--------------------------------------------+




MySQL Idiosyncrasies That BITE - 2010.09
Data Integrity - Unexpected (2)




mysql> INSERT INTO sample_data (c)
    -> VALUES ('A'),('BB'),('CCC');
                                                        ✘
mysql> SELECT * FROM sample_data WHERE c IS NOT NULL;
+---+------+---------------------+
| i | c    | t                   |
+---+------+---------------------+
| 0 | A    | 2010-06-06 13:34:59 |
| 0 | BB   | 2010-06-06 13:34:59 |
| 0 | CC   | 2010-06-06 13:34:59 |
+---+------+---------------------+
3 rows in set (0.09 sec)




MySQL Idiosyncrasies That BITE - 2010.09
Data Integrity - Warnings (2)




mysql> INSERT INTO sample_data (c) VALUES ('A'),('BB'),('CCC');
Query OK, 3 rows affected, 2 warnings (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 1

mysql> SHOW WARNINGS;
+---------+------+----------------------------------------+
| Level   | Code | Message                                |
+---------+------+----------------------------------------+   Only listed
| Warning | 1364 | Field 'i' doesn't have a default value |
| Warning | 1265 | Data truncated for column 'c' at row 3 |   for 1 row
+---------+------+----------------------------------------+
2 rows in set (0.01 sec)




MySQL Idiosyncrasies That BITE - 2010.09
Data Integrity with Dates - Warnings




mysql> INSERT INTO sample_date (d) VALUES ('2010-02-31');
Query OK, 1 row affected, 1 warning (0.00 sec)
mysql> SHOW WARNINGS;
                                                              ✘
+---------+------+----------------------------------------+
| Level   | Code | Message                                |
+---------+------+----------------------------------------+
| Warning | 1265 | Data truncated for column 'd' at row 1 |
+---------+------+----------------------------------------+
mysql> SELECT * FROM sample_date;
+------------+
| d          |
+------------+
| 0000-00-00 |
+------------+




MySQL Idiosyncrasies That BITE - 2010.09
SHOW WARNINGS

                                    http://dev.mysql.com/doc/refman/5.1/en/show-warnings.html



MySQL Idiosyncrasies That BITE - 2010.09
Using SQL_MODE for Data Integrity




                                                                              Recommended
                                                                              Configuration
SQL_MODE =
      STRICT_ALL_TABLES;




                                  http://dev.mysql.com/doc/refman/5.1/en/server-sql-mode.html



MySQL Idiosyncrasies That BITE - 2010.09
Data Integrity - Expected




mysql> SET SESSION SQL_MODE=STRICT_ALL_TABLES;
mysql> TRUNCATE TABLE sample_data;
                                                                 ✔
mysql> INSERT INTO sample_data(i) VALUES (0), (100), (255);

mysql> INSERT INTO sample_data (i) VALUES (-1), (9000);
ERROR 1264 (22003): Out of range value for column 'i' at row 1

mysql> SELECT * FROM sample_data;
+-----+------+---------------------+
| i   | c    | t                   |
+-----+------+---------------------+
|   0 | NULL | 2010-06-06 14:39:48 |
| 100 | NULL | 2010-06-06 14:39:48 |
| 255 | NULL | 2010-06-06 14:39:48 |
+-----+------+---------------------+
3 rows in set (0.00 sec)




MySQL Idiosyncrasies That BITE - 2010.09
Data Integrity with Dates - Expected




mysql> SET SESSION SQL_MODE=STRICT_ALL_TABLES;
mysql> INSERT INTO sample_date (d) VALUES ('2010-02-31');
ERROR 1292 (22007): Incorrect date value: '2010-02-31' for
                                                             ✔
column 'd' at row 1




MySQL Idiosyncrasies That BITE - 2010.09
Data Integrity with Dates - Unexpected




mysql> INSERT INTO sample_date (d) VALUES ('2010-00-00');
mysql> INSERT INTO sample_date (d) VALUES ('2010-00-05');
                                                            ✘
mysql> INSERT INTO sample_date (d) VALUES ('0000-00-00');

mysql> SELECT * FROM sample_date;
+------------+
| d          |
+------------+
| 2010-00-00 |
| 2010-00-05 |
| 0000-00-00 |
+------------+




MySQL Idiosyncrasies That BITE - 2010.09
Using SQL_MODE for Date Integrity




                                                                              Recommended
                                                                              Configuration
SQL_MODE =
      STRICT_ALL_TABLES,
      NO_ZERO_DATE,
      NO_ZERO_IN_DATE;



                                  http://dev.mysql.com/doc/refman/5.1/en/server-sql-mode.html



MySQL Idiosyncrasies That BITE - 2010.09
Data Integrity with Dates - Expected




mysql> SET SESSION
SQL_MODE='STRICT_ALL_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE';
                                                                            ✔
mysql> TRUNCATE TABLE sample_date;

mysql> INSERT INTO sample_date (d)         VALUES ('2010-00-00');
ERROR 1292 (22007): Incorrect date         value: '2010-00-00' for column
'd' at row 1
mysql> INSERT INTO sample_date (d)         VALUES ('2010-00-05');
ERROR 1292 (22007): Incorrect date         value: '2010-00-05' for column
'd' at row 1
mysql> INSERT INTO sample_date (d)         VALUES ('0000-00-00');
ERROR 1292 (22007): Incorrect date         value: '0000-00-00' for column
'd' at row 1
mysql> SELECT * FROM sample_date;
Empty set (0.00 sec)




MySQL Idiosyncrasies That BITE - 2010.09
2. Transactions


MySQL Idiosyncrasies That BITE - 2010.09
Transactions Example - Tables




DROP TABLE IF EXISTS parent;
CREATE TABLE parent (
  id   INT UNSIGNED NOT NULL AUTO_INCREMENT,
  val VARCHAR(10) NOT NULL,
PRIMARY KEY (id),
UNIQUE KEY (val)
) ENGINE=InnoDB DEFAULT CHARSET latin1;

DROP TABLE IF EXISTS child;
CREATE TABLE child (
  id        INT UNSIGNED NOT NULL AUTO_INCREMENT,
  parent_id INT UNSIGNED NOT NULL,
  created   TIMESTAMP NOT NULL,
PRIMARY KEY (id),
INDEX (parent_id)
) ENGINE=InnoDB DEFAULT CHARSET latin1;



MySQL Idiosyncrasies That BITE - 2010.09
Transactions Example - SQL




START TRANSACTION;
INSERT INTO parent(val) VALUES("a");
INSERT INTO child(parent_id,created)
           VALUES(LAST_INSERT_ID(),NOW());

# Expecting an error with next SQL
INSERT INTO parent(val) VALUES("a");
ROLLBACK;

SELECT * FROM parent;
SELECT * FROM child;




MySQL Idiosyncrasies That BITE - 2010.09
Transactions Example - Expected




...
mysql> INSERT INTO parent (val) VALUES("a");
                                                        ✔
ERROR 1062 (23000): Duplicate entry 'a' for key 'val'

mysql> ROLLBACK;

mysql> SELECT * FROM parent;
Empty set (0.00 sec)

mysql> SELECT * FROM child;
Empty set (0.00 sec)




MySQL Idiosyncrasies That BITE - 2010.09
Transactions Example - Tables




                                               MyISAM is the current
DROP TABLE IF EXISTS parent;                   default if not specified
CREATE TABLE parent (
  id   INT UNSIGNED NOT NULL AUTO_INCREMENT,
  val VARCHAR(10) NOT NULL,
PRIMARY KEY (id),
UNIQUE KEY (val)
) ENGINE=MyISAM DEFAULT CHARSET latin1;

DROP TABLE IF EXISTS child;
CREATE TABLE child (
  id        INT UNSIGNED NOT NULL AUTO_INCREMENT,
  parent_id INT UNSIGNED NOT NULL,
  created   TIMESTAMP NOT NULL,
PRIMARY KEY (id),
INDEX (parent_id)
) ENGINE=MyISAM DEFAULT CHARSET latin1;



MySQL Idiosyncrasies That BITE - 2010.09
Transactions Example - Unexpected




mysql> INSERT INTO parent (val) VALUES("a");
ERROR 1062 (23000): Duplicate entry 'a' for key 'val'
                                                        ✘
mysql> ROLLBACK;
mysql> SELECT * FROM parent;
+----+-----+
| id | val |
+----+-----+
| 1 | a    |
+----+-----+
1 row in set (0.00 sec)
mysql> SELECT * FROM child;
+----+-----------+---------------------+
| id | parent_id | created             |
+----+-----------+---------------------+
| 1 |          1 | 2010-06-03 13:53:38 |
+----+-----------+---------------------+
1 row in set (0.00 sec)




MySQL Idiosyncrasies That BITE - 2010.09
Transactions Example - Unexpected (2)




mysql> ROLLBACK;
Query OK, 0 rows affected, 1 warning (0.00 sec)
                                                      ✘
mysql> SHOW WARNINGS;
+---------+-------+-------------------------------------
| Level   | Code | Message
|
+---------+------+--------------------------------------
| Warning | 1196 | Some non-transactional changed tables
couldn't be rolled back |
+---------+------+--------------------------------------




MySQL Idiosyncrasies That BITE - 2010.09
Transactions Solution




                                                                                Recommended
       mysql> SET GLOBAL storage_engine=InnoDB;                                 Configuration


       # my.cnf
       [mysqld]
       default-storage-engine=InnoDB

       # NOTE: Requires server restart

       $ /etc/init.d/mysqld stop
       $ /etc/init.d/mysqld start




http://dev.mysql.com/doc/refman/5.1/en/server-options.html#option_mysqld_default-storage-engine



       MySQL Idiosyncrasies That BITE - 2010.09
Why use Non Transactional Tables?




     No transaction overhead
          Much faster
          Less disk writes
          Lower disk space requirements
          Less memory requirements

          http://dev.mysql.com/doc/refman/5.1/en/storage-engine-compare-transactions.html




MySQL Idiosyncrasies That BITE - 2010.09
3. Variable Scope


MySQL Idiosyncrasies That BITE - 2010.09
Variable Scope Example




mysql> CREATE TABLE test1(id INT UNSIGNED NOT NULL);

mysql> SHOW CREATE TABLE test1G
CREATE TABLE `test1` (
  `id` int(10) unsigned NOT NULL             MyISAM is the current
) ENGINE=MyISAM DEFAULT CHARSET=latin1       default if not specified




MySQL Idiosyncrasies That BITE - 2010.09
Variable Scope Example (2)




mysql> SHOW GLOBAL VARIABLES LIKE 'storage_engine';
+----------------+--------+
| Variable_name | Value |                             Changing in
+----------------+--------+                           MySQL 5.5
| storage_engine | MyISAM |
+----------------+--------+

mysql> SET GLOBAL storage_engine='InnoDB';
mysql> SHOW GLOBAL VARIABLES LIKE 'storage_engine';
+----------------+--------+
| Variable_name | Value |
+----------------+--------+
| storage_engine | InnoDB |
+----------------+--------+




MySQL Idiosyncrasies That BITE - 2010.09
Variable Scope Example (3)




mysql> SHOW GLOBAL VARIABLES LIKE 'storage_engine';
+----------------+--------+
                                                       ✘
| Variable_name | Value |
+----------------+--------+
| storage_engine | InnoDB |
+----------------+--------+

mysql> DROP TABLE test1;
mysql> CREATE TABLE test1(id INT UNSIGNED NOT NULL);

mysql> SHOW CREATE TABLE test1G
CREATE TABLE `test1` (
  `id` int(10) unsigned NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1




MySQL Idiosyncrasies That BITE - 2010.09
Variable Scope Example (4)




mysql> SHOW GLOBAL VARIABLES LIKE 'storage_engine';
+----------------+--------+
| Variable_name | Value |
+----------------+--------+
| storage_engine | InnoDB |
+----------------+--------+




MySQL Idiosyncrasies That BITE - 2010.09
Variable Scope Example (4)




mysql> SHOW GLOBAL VARIABLES LIKE 'storage_engine';
+----------------+--------+
| Variable_name | Value |
+----------------+--------+
| storage_engine | InnoDB |
+----------------+--------+

mysql> SHOW SESSION VARIABLES LIKE 'storage_engine';
+----------------+--------+
| Variable_name | Value |
+----------------+--------+
| storage_engine | MyISAM |
+----------------+--------+




MySQL Idiosyncrasies That BITE - 2010.09
Variable Scope Example (4)




mysql> SHOW GLOBAL VARIABLES LIKE 'storage_engine';
+----------------+--------+
| Variable_name | Value |
+----------------+--------+
| storage_engine | InnoDB |
+----------------+--------+

mysql> SHOW SESSION VARIABLES LIKE 'storage_engine';
+----------------+--------+
| Variable_name | Value |
+----------------+--------+
| storage_engine | MyISAM |
+----------------+--------+



                                                  MySQL Variables have
                                                      two scopes.
                                                  SESSION & GLOBAL


MySQL Idiosyncrasies That BITE - 2010.09
Variable Scope Syntax




     SHOW [GLOBAL | SESSION] VARIABLES;
     SET [GLOBAL | SESSION] variable = value;
     Default is GLOBAL since 5.0.2




                                         http://dev.mysql.com/doc/refman/5.1/en/set-option.html
                                      http://dev.mysql.com/doc/refman/5.1/en/user-variables.html



MySQL Idiosyncrasies That BITE - 2010.09
Variable Scope Precautions




     Persistent connections (e.g. Java)
     Replication SQL_THREAD




                                           Be careful of existing
                                               connections!


MySQL Idiosyncrasies That BITE - 2010.09
4. Storage Engines


MySQL Idiosyncrasies That BITE - 2010.09
Storage Engines Example - Creation




CREATE TABLE test1 (
  id INT UNSIGNED NOT NULL
                                                   ✘
) ENGINE=InnoDB DEFAULT CHARSET latin1;

SHOW CREATE TABLE test1G
*************************** 1. row *************
Table: test1
Create Table: CREATE TABLE `test1` (
  `id` int(10) unsigned NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1




MySQL Idiosyncrasies That BITE - 2010.09
Storage Engines Example - Creation




CREATE TABLE test1 (
  id INT UNSIGNED NOT NULL
                                                          ✘
) ENGINE=InnoDB DEFAULT CHARSET latin1;

SHOW CREATE TABLE test1G
*************************** 1. row *************
Table: test1
Create Table: CREATE TABLE `test1` (
  `id` int(10) unsigned NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1




                                                Even specified the
                                             storage engine reverted
                                                  to the default


MySQL Idiosyncrasies That BITE - 2010.09
Storage Engines Example - Unexpected




CREATE TABLE test1 (
  id INT UNSIGNED NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET latin1;
Query OK, 0 rows affected, 2 warnings (0.13 sec)
SHOW WARNINGS;
+---------+------+-----------------------------------------------+
| Level   | Code | Message                                       |
+---------+------+-----------------------------------------------+
| Warning | 1286 | Unknown table engine 'InnoDB'                 |
| Warning | 1266 | Using storage engine MyISAM for table 'test1' |
+---------+------+-----------------------------------------------+
2 rows in set (0.00 sec)




MySQL Idiosyncrasies That BITE - 2010.09
Storage Engines Example - Cause




   mysql> SHOW ENGINES;
+------------+---------+-------------------------+--------------+
| Engine     | Support | Comment                  | Transactions |
+------------+---------+-------------------------+--------------+
| InnoDB     | NO       | Supports transaction... | NULL         |
| MEMORY     | YES      | Hash based, stored i... | NO           |
| MyISAM     | DEFAULT | Default engine as ... | NO              |




MySQL Idiosyncrasies That BITE - 2010.09
Storage Engines Example - Unexpected (2)




mysql> CREATE TABLE test1
id INT UNSIGNED NOT NULL
) ENGINE=InnDB DEFAULT CHARSET latin1;
                                                                    ✘
Query OK, 0 rows affected, 2 warnings (0.11 sec)

mysql> SHOW WARNINGS;
+---------+------+-----------------------------------------------+
| Level   | Code | Message                                       |
+---------+------+-----------------------------------------------+
| Warning | 1286 | Unknown table engine 'InnDB'                  |
| Warning | 1266 | Using storage engine MyISAM for table 'test1' |
+---------+------+-----------------------------------------------+




                                                       Be careful of spelling
                                                         mistakes as well.


MySQL Idiosyncrasies That BITE - 2010.09
Using SQL_MODE for Storage Engines




                                                                              Recommended
                                                                              Configuration
SQL_MODE =
      STRICT_ALL_TABLES,
      NO_ZERO_DATE,
      NO_ZERO_IN_DATE,
      NO_ENGINE_SUBSTITUTION;

                                  http://dev.mysql.com/doc/refman/5.1/en/server-sql-mode.html



MySQL Idiosyncrasies That BITE - 2010.09
Storage Engines Example - Solution




SET SESSION SQL_MODE=NO_ENGINE_SUBSTITUTION;        ✔
DROP TABLE IF EXISTS test3;
CREATE TABLE test3 (
  id INT UNSIGNED NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET latin1;

ERROR 1286 (42000): Unknown table engine 'InnoDB'




MySQL Idiosyncrasies That BITE - 2010.09
5. ACID


MySQL Idiosyncrasies That BITE - 2010.09
Atomicity -
                      All or nothing


MySQL Idiosyncrasies That BITE - 2010.09
Atomicity - Table




DROP TABLE IF EXISTS test1;
CREATE TABLE test1(
  id INT UNSIGNED NOT NULL AUTO_INCREMENT,
  val CHAR(1) NOT NULL,
  PRIMARY KEY (id)
) ENGINE=MyISAM DEFAULT CHARSET latin1;




MySQL Idiosyncrasies That BITE - 2010.09
Atomicity - Data




INSERT INTO test1(val)
            VALUES ('a'),('b'),('c'),('d'),('e'),('f'),('g');

mysql> SELECT * FROM test1;
+----+-----+
| id | val |
+----+-----+
| 1 | a    |
| 2 | b    |
| 3 | c    |
| 4 | d    |
| 5 | e    |
| 6 | f    |
| 7 | g    |
+----+-----+
7 rows in set (0.00 sec)

mysql> UPDATE test1 SET id = 10 - id;



MySQL Idiosyncrasies That BITE - 2010.09
Atomicity - Unexpected




mysql> UPDATE test1 SET id = 10 - id;
ERROR 1062 (23000): Duplicate entry '7' for key
                                                  ✘
'PRIMARY'
mysql> SELECT * FROM test1;
+----+-----+
| id | val |
+----+-----+
| 9 | a    |
| 8 | b    |
| 3 | c    |
| 4 | d    |
| 5 | e    |
| 6 | f    |
| 7 | g    |
+----+-----+




MySQL Idiosyncrasies That BITE - 2010.09
Atomicity - Unexpected (2)




mysql> ROLLBACK;
Query OK, 0 rows affected (0.00 sec)
                                           ✘
mysql> SELECT * FROM test1;
+----+-----+
| id | val |
+----+-----+
| 9 | a    |
| 8 | b    |
...




MySQL Idiosyncrasies That BITE - 2010.09
Atomicity - Unexpected (2)




mysql> ROLLBACK;
Query OK, 0 rows affected (0.00 sec)
                                                        ✘
mysql> SELECT * FROM test1;
+----+-----+
| id | val |
+----+-----+
| 9 | a    |
| 8 | b    |
...




                                           Rollback has no effect
                                           on non transactional
                                                   tables


MySQL Idiosyncrasies That BITE - 2010.09
Atomicity - Transactional Table




ALTER TABLE test1 ENGINE=InnoDB;
DELETE FROM test1 WHERE id > 5;
SELECT * FROM test1;
                                           ✘
+----+-----+
| id | val |
+----+-----+
| 3 | c    |
| 4 | d    |
| 5 | e    |
+----+-----+
ROLLBACK;
SELECT * FROM test1;
+----+-----+
| id | val |
+----+-----+
| 3 | c    |
| 4 | d    |
| 5 | e    |
+----+-----+




MySQL Idiosyncrasies That BITE - 2010.09
Atomicity - The culprit




mysql> SHOW GLOBAL VARIABLES LIKE 'autocommit';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| autocommit    | ON    |
+---------------+-------+




MySQL Idiosyncrasies That BITE - 2010.09
Atomicity - The culprit




mysql> SHOW GLOBAL VARIABLES LIKE 'autocommit';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| autocommit    | ON    |
+---------------+-------+




                                                 The default for
                                              autocommit is ON.
                                             Great care is needed to
                                               change the default.
MySQL Idiosyncrasies That BITE - 2010.09
Atomicity - Recommendations




                                           Recommended
                                              Practice
     Always wrap SQL in transactions
          START TRANSACTION | BEGIN [WORK]
          COMMIT




MySQL Idiosyncrasies That BITE - 2010.09
Isolation -
     Transactions do not affect
        other transactions


MySQL Idiosyncrasies That BITE - 2010.09
Transaction Isolation - Levels




                                                                           MySQL supports 4
                                                                            isolation levels
       READ-UNCOMMITTED
       READ-COMMITTED
       REPEATABLE-READ
       SERIALIZABLE


                                 http://dev.mysql.com/doc/refman/5.1/en/set-transaction.html
http://ronaldbradford.com/blog/understanding-mysql-innodb-transaction-isolation-2009-09-24/



  MySQL Idiosyncrasies That BITE - 2010.09
Transaction Isolation - Default




     The default is
          REPEATABLE-READ

     SET GLOBAL tx_isolation = 'READ-COMMITTED';


     This is a mistake


               http://ronaldbradford.com/blog/dont-assume-common-terminology-2010-03-03/

MySQL Idiosyncrasies That BITE - 2010.09
Transaction Isolation - Default




     The default is
          REPEATABLE-READ

     SET GLOBAL tx_isolation = 'READ-COMMITTED';


     This is a mistake
                                                                        Common mistake by
                                                                           Oracle DBA's
                                                                         supporting MySQL

               http://ronaldbradford.com/blog/dont-assume-common-terminology-2010-03-03/

MySQL Idiosyncrasies That BITE - 2010.09
Oracle READ COMMITTED
           !=
 MySQL READ-COMMITTED

MySQL Idiosyncrasies That BITE - 2010.09
Transaction Isolation - Binary Logging Errors




SET GLOBAL tx_isolation='READ-COMMITTED';
Query OK, 0 rows affected (0.00 sec)
                                                             ✘
mysql> insert into test1 values (1,'x');
ERROR 1598 (HY000): Binary logging not possible.
Message: Transaction level 'READ-COMMITTED' in InnoDB is
not safe for binlog mode 'STATEMENT'




                                                As of 5.1 this requires
                                                further configuration
                                                   considerations



MySQL Idiosyncrasies That BITE - 2010.09
Durability -
                  No committed
               transactions are lost


MySQL Idiosyncrasies That BITE - 2010.09
Data Safety




     MyISAM writes/flushes data every statement
          Does not flush indexes (*)
     InnoDB can relax durability
          flush all transactions per second

     innodb_flush_log_at_trx_commit
     sync_binlog
     innodb_support_xa
MySQL Idiosyncrasies That BITE - 2010.09
Auto Recovery




     InnoDB has it
     MyISAM does not - (*) Indexes


     MYISAM-RECOVER=FORCE,BACKUP
     REPAIR TABLE
     myisamchk


MySQL Idiosyncrasies That BITE - 2010.09
Recovery Time




     InnoDB is consistent
          Redo Log file size
          Slow performance in Undo (*)
     MyISAM grows with data volume




MySQL Idiosyncrasies That BITE - 2010.09
InnoDB Auto Recovery Example




InnoDB: Log scan progressed past the checkpoint lsn 0 188755039
100624 16:37:44 InnoDB: Database was not shut down normally!
InnoDB: Starting crash recovery.
                                                                           ✔
InnoDB: Reading tablespace information from the .ibd files...
InnoDB: Restoring possible half-written data pages from the doublewrite
InnoDB: buffer...
InnoDB: Doing recovery: scanned up to log sequence number 0 193997824
InnoDB: Doing recovery: scanned up to log sequence number 0 195151897
InnoDB: 1 transaction(s) which must be rolled back or cleaned up
InnoDB: in total 105565 row operations to undo
InnoDB: Trx id counter is 0 18688
100624 16:37:45 InnoDB: Starting an apply batch of log records to the
database...
InnoDB: Progress in percents: 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
19 ... 99
InnoDB: Apply batch completed
InnoDB: Last MySQL binlog file position 0 12051, file name ./binary-log.
000003
InnoDB: Starting in background the rollback of uncommitted transactions
...




MySQL Idiosyncrasies That BITE - 2010.09
MyISAM Repair needed Example




100126 22:44:35 [ERROR] /var/lib/mysql5/bin/mysqld: Table './XXX/
descriptions' is marked as crashed and should be repaired
100126 22:44:35 [Warning] Checking table:   './XXX/descriptions'
                                                                        ✘
100126 22:44:35 [ERROR] /var/lib/mysql5/bin/mysqld: Table './XXX/taggings'
is marked as crashed and should be repaired
100126 22:44:35 [Warning] Checking table:   './XXX/taggings'
100126 22:44:35 [ERROR] /var/lib/mysql5/bin/mysqld: Table './XXX/vehicle'
is marked as crashed and should be repaired




MySQL Idiosyncrasies That BITE - 2010.09
6. Data Security


MySQL Idiosyncrasies That BITE - 2010.09
User Privileges - Practices




Best Practice
CREATE USER goodguy@localhost IDENTIFIED BY 'sakila';
GRANT CREATE,SELECT,INSERT,UPDATE,DELETE ON odtug.* TO
                                                                                                ✔
goodguy@localhost;



Normal Practice
CREATE USER superman@'%';
GRANT ALL ON *.* TO superman@'%';
                                                                                                ✘
                                           http://dev.mysql.com/doc/refman/5.1/en/create-user.html
                                                  http://dev.mysql.com/doc/refman/5.1/en/grant.html


MySQL Idiosyncrasies That BITE - 2010.09
User Privileges - Normal Bad Practice




     GRANT ALL ON *.* TO user@’%’
          *.* gives you access to all tables in all schemas
          @’%’ give you access from any external location
          ALL gives you
          ALTER, ALTER ROUTINE, CREATE, CREATE ROUTINE, CREATE TEMPORARY
          TABLES, CREATE USER, CREATE VIEW, DELETE, DROP, EVENT, EXECUTE, FILE,
          INDEX, INSERT, LOCK TABLES, PROCESS, REFERENCES, RELOAD, REPLICATION
          CLIENT, REPLICATION SLAVE, SELECT, SHOW DATABASES, SHOW VIEW,
          SHUTDOWN, SUPER, TRIGGER, UPDATE, USAGE



MySQL Idiosyncrasies That BITE - 2010.09
User Privileges - Why SUPER is bad




     SUPER
          Bypasses read_only
          Bypasses init_connect
          Can Disable binary logging
          Change configuration dynamically
          No reserved connection


MySQL Idiosyncrasies That BITE - 2010.09
SUPER and Read Only




$ mysql -ugoodguy -psakila odtug
mysql> insert into test1(id) values(1);
                                                      ✔
ERROR 1290 (HY000): The MySQL server is running with the
--read-only option so it cannot execute this statement




$ mysql -usuperman odtug
mysql> insert into test1(id) values(1);
Query OK, 1 row affected (0.01 sec)
                                                      ✘

MySQL Idiosyncrasies That BITE - 2010.09
SUPER and Read Only




$ mysql -ugoodguy -psakila odtug
mysql> insert into test1(id) values(1);
ERROR 1290 (HY000): The MySQL server is running with the
--read-only option so it cannot execute this statement   ✔

$ mysql -usuperman odtug
mysql> insert into test1(id) values(1);
Query OK, 1 row affected (0.01 sec)
                                                         ✘
                                               Data inconsistency
                                                  now exists


MySQL Idiosyncrasies That BITE - 2010.09
SUPER and Init Connect




                                           Common configuration
#my.cnf                                     practice to support
[client]                                           UTF8
init_connect=SET NAMES utf8


This specifies to use UTF8 for communication with client
and data




MySQL Idiosyncrasies That BITE - 2010.09
SUPER and Init Connect (2)




MySQL Idiosyncrasies That BITE - 2010.09
SUPER and Init Connect (2)




✔
$ mysql -ugoodguy -psakila odtug

mysql> SHOW SESSION VARIABLES LIKE 'ch%';
+--------------------------+----------+
| Variable_name            | Value    |
+--------------------------+----------+
| character_set_client     | utf8     |
| character_set_connection | utf8     |
| character_set_database   | latin1   |
| character_set_filesystem | binary   |
| character_set_results    | utf8     |
| character_set_server     | latin1   |
| character_set_system     | utf8     |
+--------------------------+----------+




 MySQL Idiosyncrasies That BITE - 2010.09
SUPER and Init Connect (2)




✔                       $ mysql -usuperman odtug
$ mysql -ugoodguy -psakila odtug
                                                              ✘
                        mysql> SHOW SESSION VARIABLES LIKE
mysql> SHOW SESSION VARIABLES LIKE 'ch%';
                        'character%';
+--------------------------+----------+
                        +--------------------------+----------+
| Variable_name             | Value
                        | Variable_name|           | Value    |
+--------------------------+----------+
                        +--------------------------+----------+
| character_set_client | character_set_client
                            | utf8     |           | latin1   |
| character_set_connection | utf8      |
                        | character_set_connection | latin1   |
| character_set_database character_set_database
                        |   | latin1   |           | latin1   |
| character_set_filesystem | binary    |
                        | character_set_filesystem | binary   |
| character_set_results | character_set_results
                            | utf8     |           | latin1   |
| character_set_server | character_set_server
                            | latin1   |           | latin1   |
| character_set_system | character_set_system
                            | utf8     |           | utf8     |
+--------------------------+----------+
                        +--------------------------+----------+




 MySQL Idiosyncrasies That BITE - 2010.09
SUPER and Init Connect (2)




✔                       $ mysql -usuperman odtug
$ mysql -ugoodguy -psakila odtug
                                                                    ✘
                        mysql> SHOW SESSION VARIABLES LIKE
mysql> SHOW SESSION VARIABLES LIKE 'ch%';
                        'character%';
+--------------------------+----------+
                        +--------------------------+----------+
| Variable_name             | Value
                        | Variable_name|           | Value           |
+--------------------------+----------+
                        +--------------------------+----------+
| character_set_client | character_set_client
                            | utf8     |           | latin1          |
| character_set_connection | utf8      |
                        | character_set_connection | latin1          |
| character_set_database character_set_database
                        |   | latin1   |           | latin1          |
| character_set_filesystem | binary    |
                        | character_set_filesystem | binary          |
| character_set_results | character_set_results
                            | utf8     |           | latin1          |
| character_set_server | character_set_server
                            | latin1   |           | latin1          |
| character_set_system | character_set_system
                            | utf8     |           | utf8            |
+--------------------------+----------+
                        +--------------------------+----------+
                                                 Data integrity can be
                                                    compromised


 MySQL Idiosyncrasies That BITE - 2010.09
SUPER and Binary Log




mysql> SHOW MASTER STATUS;
+-------------------+----------+--------------+------------------+
| File              | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------+----------+--------------+------------------+
| binary-log.000001 |      354 |              |                  |
+-------------------+----------+--------------+------------------+

mysql> DROP TABLE time_zone_leap_second;
mysql> SET SQL_LOG_BIN=0;
mysql> DROP TABLE time_zone_name;
mysql> SET SQL_LOG_BIN=1;
mysql> DROP TABLE time_zone_transition;
mysql> SHOW MASTER STATUS;
+-------------------+----------+--------------+------------------+
| File              | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------+----------+--------------+------------------+
| binary-log.000001 |      674 |              |                  |
+-------------------+----------+--------------+------------------+




MySQL Idiosyncrasies That BITE - 2010.09
SUPER and Binary Log (2)




$ mysqlbinlog binary-log.000001 --start-position=354 --stop-position=674

# at 354
#100604 18:00:08 server id 1 end_log_pos 450 Query thread_id=1 exec_time=0
error_code=0
use mysql/*!*/;
SET TIMESTAMP=1275688808/*!*/;



                                                                           ✘
DROP TABLE time_zone_leap_second
/*!*/;
# at 579
#100604 18:04:31 server id 1 end_log_pos 674 Query thread_id=2 exec_time=0
error_code=0
use mysql/*!*/;
SET TIMESTAMP=1275689071/*!*/;
DROP TABLE time_zone_transition
/*!*/;
DELIMITER ;
# End of log file
ROLLBACK /* added by mysqlbinlog */;


                                                            Data auditability is now
                                                                compromised


MySQL Idiosyncrasies That BITE - 2010.09
SUPER and reserved connection




$ mysql -uroot                                              ✔
mysql> show global variables like 'max_connections';
+-----------------+-------+
| Variable_name   | Value |
+-----------------+-------+
| max_connections | 3     |
+-----------------+-------+
1 row in set (0.07 sec)

mysql> show global status like 'threads_connected';
+-------------------+-------+
| Variable_name     | Value |
+-------------------+-------+
| Threads_connected | 4     |
+-------------------+-------+                    When connected
                                             correctly, 1 connection
                                              is reserved for SUPER

MySQL Idiosyncrasies That BITE - 2010.09
SUPER and reserved connection - Unexpected




$ mysql -uroot
ERROR 1040 (HY000): Too many connections
                                                                                  ✘
mysql> SHOW PROCESSLIST;
+----+------+-----------+-------+---------+------+------------+---------------
| Id | User | Host      | db    | Command | Time | State      | Info
+----+------+-----------+-------+---------+------+------------+---------------
| 13 | root | localhost | odtug | Query   | 144 | User sleep | UPDATE test1 ...
| 14 | root | localhost | odtug | Query   | 116 | Locked      | select * from test1
| 15 | root | localhost | odtug | Query   |   89 | Locked     | select * from test1




                                                                       When application
                                                                      users all use SUPER,
                                                                      administrator can't
                                                                       diagnose problem


MySQL Idiosyncrasies That BITE - 2010.09
7. ANSI SQL


MySQL Idiosyncrasies That BITE - 2010.09
Group By Example




SELECT country, COUNT(*) AS color_count
FROM   flags
GROUP BY country;


                                           ✔
+-----------+-------------+
| country    | color_count |
+-----------+-------------+
| Australia |            3 |
| Canada     |           2 |
| Japan      |           2 |
| Sweden     |           2 |
| USA        |           3 |
+-----------+-------------+


SELECT country, COUNT(*)



                                           ✘
FROM   flags;
+-----------+----------+
| country   | COUNT(*) |
+-----------+----------+
| Australia |       12 |
+-----------+----------+




MySQL Idiosyncrasies That BITE - 2010.09
Group By Example (2)




SET SESSION sql_mode=ONLY_FULL_GROUP_BY;
                                                                                       ✔
SELECT country, COUNT(*)
FROM   flags;

ERROR 1140 (42000): Mixing of GROUP columns (MIN(),MAX(),COUNT(),...)
   with no GROUP columns is illegal if there is no GROUP BY clause




                                           http://ExpertPHPandMySQL.com   Chapter 1 - Pg 31



MySQL Idiosyncrasies That BITE - 2010.09
Using SQL_MODE for ANSI SQL




                                           Recommended
                                           Configuration
SQL_MODE =
      STRICT_ALL_TABLES,
      NO_ZERO_DATE,
      NO_ZERO_IN_DATE,
      NO_ENGINE_SUBSTITUTION,
      ONLY_FULL_GROUP_BY;


MySQL Idiosyncrasies That BITE - 2010.09
8. Case Sensitivity


MySQL Idiosyncrasies That BITE - 2010.09
Case Sensitivity String Comparison




  SELECT 'USA' = UPPER('usa');
+----------------------+
| 'USA' = UPPER('usa') |
+----------------------+
|                    1 |
+----------------------+

SELECT 'USA' = 'USA', 'USA' = 'Usa', 'USA' = 'usa',
       'USA' = 'usa' COLLATE latin1_general_cs AS different;
+---------------+---------------+---------------+-----------+
| 'USA' = 'USA' | 'USA' = 'Usa' | 'USA' = 'usa' | different |
+---------------+---------------+---------------+-----------+
|             1 |             1 |             1 |         0 |
+---------------+---------------+---------------+-----------+




SELECT name, address, email
FROM   customer
                                                                         ✘
                                                                 This practice is
WHERE name = UPPER('bradford')
                                                            unnecessary as does not
                                                              utilize index if exists

MySQL Idiosyncrasies That BITE - 2010.09
Case Sensitivity Tables




# Server 1

mysql> CREATE TABLE test1(id INT UNSIGNED NOT NULL);
mysql> INSERT INTO test1 VALUES(1);
mysql> INSERT INTO TEST1 VALUES(2);

# Server 2

mysql> CREATE TABLE test1(id INT UNSIGNED NOT NULL);
mysql> INSERT INTO test1 VALUES(1);
mysql> INSERT INTO TEST1 VALUES(2);
ERROR 1146 (42S02): Table 'test.TEST1' doesn't exist




MySQL Idiosyncrasies That BITE - 2010.09
Case Sensitivity Tables (2) - Operating Specific Settings




# Server 1 - Mac OS X / Windows Default
mysql> SHOW GLOBAL VARIABLES LIKE 'lower%';
+------------------------+-------+
| Variable_name          | Value |
+------------------------+-------+
| lower_case_file_system | ON    |
| lower_case_table_names | 2     |
+------------------------+-------+

# Server 2 - Linux Default
mysql> SHOW GLOBAL VARIABLES LIKE 'lower%';
+------------------------+-------+
| Variable_name          | Value |
+------------------------+-------+
| lower_case_file_system | OFF   |
| lower_case_table_names | 0     |
+------------------------+-------+
                                                       Be careful. This default
                                                        varies depending on
                                                         Operating System

MySQL Idiosyncrasies That BITE - 2010.09
Other Gotchas


MySQL Idiosyncrasies That BITE - 2010.09
Other features the BITE




     Character Sets
       Data/Client/Application/Web
     Sub Queries
       Rewrite as JOIN when possible
     Views
       No always efficient
     Cascading Configuration Files
     SQL_MODE's not to use
MySQL Idiosyncrasies That BITE - 2010.09
Conclusion


MySQL Idiosyncrasies That BITE - 2010.09
DON'T
                   ASSUME
MySQL Idiosyncrasies That BITE - 2010.09
Conclusion - The story so far




MySQL Idiosyncrasies that BITE
Definitions, Relational Database [Structure/Requirements], Oracle != MySQL, RDBMS Characteristics,
Data Integrity [Expected | Unexpected | Warnings | Unexpected (2) | Warnings (2) ],
Using SQL_MODE for Data Integrity,
Data Integrity [ Expected ], Data Integrity with Dates [Expected | Unexpected ],
Using SQL_MODE for Date Integrity, Data Integrity with Dates [Expected ],
Transactions Example [ Tables | SQL | Expected | Using MyISAM | Unexpected [ (2)],
Transactions Solution, Why use Non Transactional Tables?
Variable Scope Example [ | (2)| (3)| [4])| Syntax
Storage Engines Example [Creation | Unexpected | Cause | Unexpected (2) ]
Using SQL_MODE for Storage Engines, Storage Engines Example [Solution]
Atomicity [ Table | Data | Unexpected | Unexpected (2) | Transactional Table | The culprit | Recommendations ]
Transaction Isolation [ Levels | Default | Binary Logging ]
Durablilty [ Auto Recovery]
User Privileges [Practices | Normal Bad Practice | Why SUPER is bad ]
SUPER and [ Read Only | Init Connect [(2)] | Binary Log [2] | Reserved Connection [2] ]
Group by Example [2], Using SQL_MODE for ANSI SQL
Case Sensitivity [ String Comparison | Tables [2] ]
More Gotchas, Character Sets, Sub Queries, Views
Conclusion [ DON'T ASSUME | The Story so far | SQL_MODE ] RonaldBradford.com




MySQL Idiosyncrasies That BITE - 2010.09
Conclusion - SQL_MODE




     SQL_MODE =

                                                                                                ✔
     ALLOW_INVALID_DATES, ANSI_QUOTES, ERROR_FOR_DIVISION_ZERO,
     HIGH_NOT_PRECEDENCE,IGNORE_SPACE,NO_AUTO_CREATE_USER,
     NO_AUTO_VALUE_ON_ZERO,NO_BACKSLASH_ESCAPES,
     NO_DIR_IN_CREATE,NO_ENGINE_SUBSTITUTION,
     NO_FIELD_OPTIONS,NO_KEY_OPTIONS,NO_TABLE_OPTIONS,
     NO_UNSIGNED_SUBTRACTION,NO_ZERO_DATE,
     NO_ZERO_IN_DATE,ONLY_FULL_GROUP_BY (5.1.11),
     PAD_CHAR_TO_FULL_LENGTH (5.1.20), PIPES_AS_CONCAT,
     REAL_AS_FLOAT,STRICT_ALL_TABLES, STRICT_TRANS_TABLES
     ANSI, DB2, MAXDB, MSSQL, MYSQL323, MYSQL40, ORACLE,
     POSTGRESQL,TRADITIONAL
                                  http://dev.mysql.com/doc/refman/5.1/en/server-sql-mode.html



MySQL Idiosyncrasies That BITE - 2010.09
Conclusion - SQL_MODE




     SQL_MODE =

                                                                                                ✔
     ALLOW_INVALID_DATES, ANSI_QUOTES, ERROR_FOR_DIVISION_ZERO,
     HIGH_NOT_PRECEDENCE,IGNORE_SPACE,NO_AUTO_CREATE_USER,
     NO_AUTO_VALUE_ON_ZERO,NO_BACKSLASH_ESCAPES,
     NO_DIR_IN_CREATE,NO_ENGINE_SUBSTITUTION,
     NO_FIELD_OPTIONS,NO_KEY_OPTIONS,NO_TABLE_OPTIONS,
     NO_UNSIGNED_SUBTRACTION,NO_ZERO_DATE,
     NO_ZERO_IN_DATE,ONLY_FULL_GROUP_BY (5.1.11),
     PAD_CHAR_TO_FULL_LENGTH (5.1.20), PIPES_AS_CONCAT,
     REAL_AS_FLOAT,STRICT_ALL_TABLES, STRICT_TRANS_TABLES
                                                                                                ✘
     ANSI, DB2, MAXDB, MSSQL, MYSQL323, MYSQL40, ORACLE,
     POSTGRESQL,TRADITIONAL
                                  http://dev.mysql.com/doc/refman/5.1/en/server-sql-mode.html
                                                                                                ?
MySQL Idiosyncrasies That BITE - 2010.09
RonaldBradford.com

  MySQL4OracleDBA.com


MySQL Idiosyncrasies That BITE - 2010.09
1 of 102

More Related Content

What's hot(20)

MySQL 8.0.21 - New Features SummaryMySQL 8.0.21 - New Features Summary
MySQL 8.0.21 - New Features Summary
Olivier DASINI3.1K views
MySQL For Oracle DBA's and DevelopersMySQL For Oracle DBA's and Developers
MySQL For Oracle DBA's and Developers
Ronald Bradford9.2K views
MySQL Performance Best PracticesMySQL Performance Best Practices
MySQL Performance Best Practices
Olivier DASINI905 views
Mysql allMysql all
Mysql all
Prof. Wim Van Criekinge1.6K views
My sql 5.7-upcoming-changes-v2My sql 5.7-upcoming-changes-v2
My sql 5.7-upcoming-changes-v2
Morgan Tocker1.1K views
Highload Perf TuningHighload Perf Tuning
Highload Perf Tuning
HighLoad20091.3K views
database-querry-student-notedatabase-querry-student-note
database-querry-student-note
Leerpiny Makouach216 views
MySQL8.0 in COSCUP2017MySQL8.0 in COSCUP2017
MySQL8.0 in COSCUP2017
Shinya Sugiyama6.1K views
MySQL sys schema deep diveMySQL sys schema deep dive
MySQL sys schema deep dive
Mark Leith9.4K views
Introduction Mysql Introduction Mysql
Introduction Mysql
Gerben Menschaert319 views
Introduction To Lamp P2Introduction To Lamp P2
Introduction To Lamp P2
Amzad Hossain1K views
MySQL Document StoreMySQL Document Store
MySQL Document Store
I Goo Lee494 views
10x Performance Improvements - A Case Study10x Performance Improvements - A Case Study
10x Performance Improvements - A Case Study
Ronald Bradford13.3K views
Multi thread slave_performance_on_opcMulti thread slave_performance_on_opc
Multi thread slave_performance_on_opc
Shinya Sugiyama3.5K views
Troubleshooting MySQL PerformanceTroubleshooting MySQL Performance
Troubleshooting MySQL Performance
Sveta Smirnova2.1K views

Similar to MySQL Idiosyncrasies That Bite SF

Mysql basics1Mysql basics1
Mysql basics1Steffy Robert
1.1K views48 slides

Similar to MySQL Idiosyncrasies That Bite SF(20)

MySQL Idiosyncrasies That Bite 2010.07MySQL Idiosyncrasies That Bite 2010.07
MySQL Idiosyncrasies That Bite 2010.07
Ronald Bradford2.1K views
MySQL Idiosyncrasies That BiteMySQL Idiosyncrasies That Bite
MySQL Idiosyncrasies That Bite
Ronald Bradford7.6K views
My SQL Idiosyncrasies That Bite OTNMy SQL Idiosyncrasies That Bite OTN
My SQL Idiosyncrasies That Bite OTN
Ronald Bradford2.9K views
MySQL SQL TutorialMySQL SQL Tutorial
MySQL SQL Tutorial
Chien Chung Shen481 views
Mysql basics1Mysql basics1
Mysql basics1
Steffy Robert1.1K views
Hanya contoh saja dari xamppHanya contoh saja dari xampp
Hanya contoh saja dari xampp
Bina Sarana Informatika508 views
MySQLinsanityMySQLinsanity
MySQLinsanity
Stanley Huang470 views
Fluentd 20150918 no_demo_publicFluentd 20150918 no_demo_public
Fluentd 20150918 no_demo_public
Saewoong Lee203 views
MySQL5.7で遊んでみようMySQL5.7で遊んでみよう
MySQL5.7で遊んでみよう
yoku082518.4K views
4. Data Manipulation.ppt4. Data Manipulation.ppt
4. Data Manipulation.ppt
KISHOYIANKISH36 views
Tugas praktikum smbdTugas praktikum smbd
Tugas praktikum smbd
Amar Senjaku Ofdetraisar423 views
Cat databaseCat database
Cat database
tubbeles559 views
Short Intro to PHP and MySQLShort Intro to PHP and MySQL
Short Intro to PHP and MySQL
Jussi Pohjolainen3.4K views
My SQLMy SQL
My SQL
Karan Kashyap1.5K views
Instalar MySQL CentOSInstalar MySQL CentOS
Instalar MySQL CentOS
Moisés Elías Araya1.5K views

More from Ronald Bradford(19)

Successful Scalability Principles - Part 1Successful Scalability Principles - Part 1
Successful Scalability Principles - Part 1
Ronald Bradford9.8K views
MySQL Backup and Recovery EssentialsMySQL Backup and Recovery Essentials
MySQL Backup and Recovery Essentials
Ronald Bradford11.7K views
MySQL Scalability Mistakes - OTNMySQL Scalability Mistakes - OTN
MySQL Scalability Mistakes - OTN
Ronald Bradford9.2K views
Successful MySQL ScalabilitySuccessful MySQL Scalability
Successful MySQL Scalability
Ronald Bradford3.2K views
LIFTOFF - MySQLCamp for the Oracle DBALIFTOFF - MySQLCamp for the Oracle DBA
LIFTOFF - MySQLCamp for the Oracle DBA
Ronald Bradford1.5K views
IGNITION - MySQLCamp for the Oracle DBAIGNITION - MySQLCamp for the Oracle DBA
IGNITION - MySQLCamp for the Oracle DBA
Ronald Bradford1.7K views
Dolphins Now And Beyond - FOSDEM 2010Dolphins Now And Beyond - FOSDEM 2010
Dolphins Now And Beyond - FOSDEM 2010
Ronald Bradford2.2K views
SQL v No SQLSQL v No SQL
SQL v No SQL
Ronald Bradford1.2K views
MySQL For Oracle DevelopersMySQL For Oracle Developers
MySQL For Oracle Developers
Ronald Bradford9.2K views
The Ideal Performance ArchitectureThe Ideal Performance Architecture
The Ideal Performance Architecture
Ronald Bradford2.7K views
Extending The My Sql Data LandscapeExtending The My Sql Data Landscape
Extending The My Sql Data Landscape
Ronald Bradford734 views
Best Design Practices ABest Design Practices A
Best Design Practices A
Ronald Bradford772 views

MySQL Idiosyncrasies That Bite SF

  • 1. Title MySQL Idiosyncrasies that BITE Oracle Open World 2010 MySQL Sunday 2010.09 Ronald Bradford http://ronaldbradford.com - 2010.09 MySQL Idiosyncrasies That BITE #oow10 #mysql @RonaldBradford
  • 2. Definitions idiosyncrasy [id-ee-uh-sing-kruh-see, -sin-] –noun,plural-sies. A characteristic, habit, mannerism, or the like, that is peculiar to ... http://dictionary.com MySQL Idiosyncrasies That BITE - 2010.09
  • 3. About RDBMS - Oracle != MySQL Product Age Development philosophies Target audience Developer practices Installed versions MySQL Idiosyncrasies That BITE - 2010.09
  • 4. Expected RDBMS Characteristics Data Integrity Transactions ACID Data Security ANSI SQL MySQL Idiosyncrasies That BITE - 2010.09
  • 5. These RDBMS characteristics are NOT enabled by default (*) in MySQL MySQL Idiosyncrasies That BITE - 2010.09
  • 6. WARNING !!! These RDBMS characteristics are NOT enabled by default (*) in MySQL MySQL Idiosyncrasies That BITE - 2010.09
  • 7. MySQL Terminology Storage Engines MyISAM - Default InnoDB - Default from MySQL 5.5 Transactions Non Transactional Engine(s) Transactional Engine(s) MySQL Idiosyncrasies That BITE - 2010.09
  • 8. 1. Data Integrity MySQL Idiosyncrasies That BITE - 2010.09
  • 9. Data Integrity - Expected CREATE TABLE sample_data ( i TINYINT UNSIGNED NOT NULL, ✔ c CHAR(2) NULL, t TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ) ENGINE=InnoDB DEFAULT CHARSET latin1; INSERT INTO sample_data(i) VALUES (0), (100), (255); SELECT * FROM sample_data; +-----+------+---------------------+ | i | c | t | +-----+------+---------------------+ | 0 | NULL | 2010-06-06 13:28:44 | | 100 | NULL | 2010-06-06 13:28:44 | | 255 | NULL | 2010-06-06 13:28:44 | +-----+------+---------------------+ MySQL Idiosyncrasies That BITE - 2010.09
  • 10. Data Integrity - Unexpected ✘ mysql> INSERT INTO sample_data (i) VALUES (-1), (9000); mysql> SELECT * FROM sample_data; +-----+------+---------------------+ | i | c | t | +-----+------+---------------------+ | 0 | NULL | 2010-06-06 13:28:44 | | 100 | NULL | 2010-06-06 13:28:44 | | 255 | NULL | 2010-06-06 13:28:44 | | 0 | NULL | 2010-06-06 13:32:52 | | 255 | NULL | 2010-06-06 13:32:52 | +-----+------+---------------------+ 5 rows in set (0.01 sec) MySQL Idiosyncrasies That BITE - 2010.09
  • 11. Data Integrity - Unexpected mysql> INSERT INTO sample_data (i) VALUES (-1), (9000); ✘ mysql> SELECT * FROM sample_data; +-----+------+---------------------+ | i | c | t | +-----+------+---------------------+ | 0 | NULL | 2010-06-06 13:28:44 | | 100 | NULL | 2010-06-06 13:28:44 | | 255 | NULL | 2010-06-06 13:28:44 | | 0 | NULL | 2010-06-06 13:32:52 | | 255 | NULL | 2010-06-06 13:32:52 | +-----+------+---------------------+ 5 rows in set (0.01 sec) TINYINT is 1 byte. UNSIGNED supports values 0-255 MySQL Idiosyncrasies That BITE - 2010.09
  • 12. Data Integrity - Warnings mysql> INSERT INTO sample_data (i) VALUES (-1), (9000); Query OK, 2 rows affected, 2 warnings (0.00 sec) mysql> SHOW WARNINGS; +---------+------+--------------------------------------------+ | Level | Code | Message | +---------+------+--------------------------------------------+ | Warning | 1264 | Out of range value for column 'i' at row 1 | | Warning | 1264 | Out of range value for column 'i' at row 2 | +---------+------+--------------------------------------------+ MySQL Idiosyncrasies That BITE - 2010.09
  • 13. Data Integrity - Unexpected (2) mysql> INSERT INTO sample_data (c) -> VALUES ('A'),('BB'),('CCC'); ✘ mysql> SELECT * FROM sample_data WHERE c IS NOT NULL; +---+------+---------------------+ | i | c | t | +---+------+---------------------+ | 0 | A | 2010-06-06 13:34:59 | | 0 | BB | 2010-06-06 13:34:59 | | 0 | CC | 2010-06-06 13:34:59 | +---+------+---------------------+ 3 rows in set (0.09 sec) MySQL Idiosyncrasies That BITE - 2010.09
  • 14. Data Integrity - Warnings (2) mysql> INSERT INTO sample_data (c) VALUES ('A'),('BB'),('CCC'); Query OK, 3 rows affected, 2 warnings (0.00 sec) Records: 3 Duplicates: 0 Warnings: 1 mysql> SHOW WARNINGS; +---------+------+----------------------------------------+ | Level | Code | Message | +---------+------+----------------------------------------+ Only listed | Warning | 1364 | Field 'i' doesn't have a default value | | Warning | 1265 | Data truncated for column 'c' at row 3 | for 1 row +---------+------+----------------------------------------+ 2 rows in set (0.01 sec) MySQL Idiosyncrasies That BITE - 2010.09
  • 15. Data Integrity with Dates - Warnings mysql> INSERT INTO sample_date (d) VALUES ('2010-02-31'); Query OK, 1 row affected, 1 warning (0.00 sec) mysql> SHOW WARNINGS; ✘ +---------+------+----------------------------------------+ | Level | Code | Message | +---------+------+----------------------------------------+ | Warning | 1265 | Data truncated for column 'd' at row 1 | +---------+------+----------------------------------------+ mysql> SELECT * FROM sample_date; +------------+ | d | +------------+ | 0000-00-00 | +------------+ MySQL Idiosyncrasies That BITE - 2010.09
  • 16. SHOW WARNINGS http://dev.mysql.com/doc/refman/5.1/en/show-warnings.html MySQL Idiosyncrasies That BITE - 2010.09
  • 17. Using SQL_MODE for Data Integrity Recommended Configuration SQL_MODE = STRICT_ALL_TABLES; http://dev.mysql.com/doc/refman/5.1/en/server-sql-mode.html MySQL Idiosyncrasies That BITE - 2010.09
  • 18. Data Integrity - Expected mysql> SET SESSION SQL_MODE=STRICT_ALL_TABLES; mysql> TRUNCATE TABLE sample_data; ✔ mysql> INSERT INTO sample_data(i) VALUES (0), (100), (255); mysql> INSERT INTO sample_data (i) VALUES (-1), (9000); ERROR 1264 (22003): Out of range value for column 'i' at row 1 mysql> SELECT * FROM sample_data; +-----+------+---------------------+ | i | c | t | +-----+------+---------------------+ | 0 | NULL | 2010-06-06 14:39:48 | | 100 | NULL | 2010-06-06 14:39:48 | | 255 | NULL | 2010-06-06 14:39:48 | +-----+------+---------------------+ 3 rows in set (0.00 sec) MySQL Idiosyncrasies That BITE - 2010.09
  • 19. Data Integrity with Dates - Expected mysql> SET SESSION SQL_MODE=STRICT_ALL_TABLES; mysql> INSERT INTO sample_date (d) VALUES ('2010-02-31'); ERROR 1292 (22007): Incorrect date value: '2010-02-31' for ✔ column 'd' at row 1 MySQL Idiosyncrasies That BITE - 2010.09
  • 20. Data Integrity with Dates - Unexpected mysql> INSERT INTO sample_date (d) VALUES ('2010-00-00'); mysql> INSERT INTO sample_date (d) VALUES ('2010-00-05'); ✘ mysql> INSERT INTO sample_date (d) VALUES ('0000-00-00'); mysql> SELECT * FROM sample_date; +------------+ | d | +------------+ | 2010-00-00 | | 2010-00-05 | | 0000-00-00 | +------------+ MySQL Idiosyncrasies That BITE - 2010.09
  • 21. Using SQL_MODE for Date Integrity Recommended Configuration SQL_MODE = STRICT_ALL_TABLES, NO_ZERO_DATE, NO_ZERO_IN_DATE; http://dev.mysql.com/doc/refman/5.1/en/server-sql-mode.html MySQL Idiosyncrasies That BITE - 2010.09
  • 22. Data Integrity with Dates - Expected mysql> SET SESSION SQL_MODE='STRICT_ALL_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE'; ✔ mysql> TRUNCATE TABLE sample_date; mysql> INSERT INTO sample_date (d) VALUES ('2010-00-00'); ERROR 1292 (22007): Incorrect date value: '2010-00-00' for column 'd' at row 1 mysql> INSERT INTO sample_date (d) VALUES ('2010-00-05'); ERROR 1292 (22007): Incorrect date value: '2010-00-05' for column 'd' at row 1 mysql> INSERT INTO sample_date (d) VALUES ('0000-00-00'); ERROR 1292 (22007): Incorrect date value: '0000-00-00' for column 'd' at row 1 mysql> SELECT * FROM sample_date; Empty set (0.00 sec) MySQL Idiosyncrasies That BITE - 2010.09
  • 24. Transactions Example - Tables DROP TABLE IF EXISTS parent; CREATE TABLE parent ( id INT UNSIGNED NOT NULL AUTO_INCREMENT, val VARCHAR(10) NOT NULL, PRIMARY KEY (id), UNIQUE KEY (val) ) ENGINE=InnoDB DEFAULT CHARSET latin1; DROP TABLE IF EXISTS child; CREATE TABLE child ( id INT UNSIGNED NOT NULL AUTO_INCREMENT, parent_id INT UNSIGNED NOT NULL, created TIMESTAMP NOT NULL, PRIMARY KEY (id), INDEX (parent_id) ) ENGINE=InnoDB DEFAULT CHARSET latin1; MySQL Idiosyncrasies That BITE - 2010.09
  • 25. Transactions Example - SQL START TRANSACTION; INSERT INTO parent(val) VALUES("a"); INSERT INTO child(parent_id,created) VALUES(LAST_INSERT_ID(),NOW()); # Expecting an error with next SQL INSERT INTO parent(val) VALUES("a"); ROLLBACK; SELECT * FROM parent; SELECT * FROM child; MySQL Idiosyncrasies That BITE - 2010.09
  • 26. Transactions Example - Expected ... mysql> INSERT INTO parent (val) VALUES("a"); ✔ ERROR 1062 (23000): Duplicate entry 'a' for key 'val' mysql> ROLLBACK; mysql> SELECT * FROM parent; Empty set (0.00 sec) mysql> SELECT * FROM child; Empty set (0.00 sec) MySQL Idiosyncrasies That BITE - 2010.09
  • 27. Transactions Example - Tables MyISAM is the current DROP TABLE IF EXISTS parent; default if not specified CREATE TABLE parent ( id INT UNSIGNED NOT NULL AUTO_INCREMENT, val VARCHAR(10) NOT NULL, PRIMARY KEY (id), UNIQUE KEY (val) ) ENGINE=MyISAM DEFAULT CHARSET latin1; DROP TABLE IF EXISTS child; CREATE TABLE child ( id INT UNSIGNED NOT NULL AUTO_INCREMENT, parent_id INT UNSIGNED NOT NULL, created TIMESTAMP NOT NULL, PRIMARY KEY (id), INDEX (parent_id) ) ENGINE=MyISAM DEFAULT CHARSET latin1; MySQL Idiosyncrasies That BITE - 2010.09
  • 28. Transactions Example - Unexpected mysql> INSERT INTO parent (val) VALUES("a"); ERROR 1062 (23000): Duplicate entry 'a' for key 'val' ✘ mysql> ROLLBACK; mysql> SELECT * FROM parent; +----+-----+ | id | val | +----+-----+ | 1 | a | +----+-----+ 1 row in set (0.00 sec) mysql> SELECT * FROM child; +----+-----------+---------------------+ | id | parent_id | created | +----+-----------+---------------------+ | 1 | 1 | 2010-06-03 13:53:38 | +----+-----------+---------------------+ 1 row in set (0.00 sec) MySQL Idiosyncrasies That BITE - 2010.09
  • 29. Transactions Example - Unexpected (2) mysql> ROLLBACK; Query OK, 0 rows affected, 1 warning (0.00 sec) ✘ mysql> SHOW WARNINGS; +---------+-------+------------------------------------- | Level | Code | Message | +---------+------+-------------------------------------- | Warning | 1196 | Some non-transactional changed tables couldn't be rolled back | +---------+------+-------------------------------------- MySQL Idiosyncrasies That BITE - 2010.09
  • 30. Transactions Solution Recommended mysql> SET GLOBAL storage_engine=InnoDB; Configuration # my.cnf [mysqld] default-storage-engine=InnoDB # NOTE: Requires server restart $ /etc/init.d/mysqld stop $ /etc/init.d/mysqld start http://dev.mysql.com/doc/refman/5.1/en/server-options.html#option_mysqld_default-storage-engine MySQL Idiosyncrasies That BITE - 2010.09
  • 31. Why use Non Transactional Tables? No transaction overhead Much faster Less disk writes Lower disk space requirements Less memory requirements http://dev.mysql.com/doc/refman/5.1/en/storage-engine-compare-transactions.html MySQL Idiosyncrasies That BITE - 2010.09
  • 32. 3. Variable Scope MySQL Idiosyncrasies That BITE - 2010.09
  • 33. Variable Scope Example mysql> CREATE TABLE test1(id INT UNSIGNED NOT NULL); mysql> SHOW CREATE TABLE test1G CREATE TABLE `test1` ( `id` int(10) unsigned NOT NULL MyISAM is the current ) ENGINE=MyISAM DEFAULT CHARSET=latin1 default if not specified MySQL Idiosyncrasies That BITE - 2010.09
  • 34. Variable Scope Example (2) mysql> SHOW GLOBAL VARIABLES LIKE 'storage_engine'; +----------------+--------+ | Variable_name | Value | Changing in +----------------+--------+ MySQL 5.5 | storage_engine | MyISAM | +----------------+--------+ mysql> SET GLOBAL storage_engine='InnoDB'; mysql> SHOW GLOBAL VARIABLES LIKE 'storage_engine'; +----------------+--------+ | Variable_name | Value | +----------------+--------+ | storage_engine | InnoDB | +----------------+--------+ MySQL Idiosyncrasies That BITE - 2010.09
  • 35. Variable Scope Example (3) mysql> SHOW GLOBAL VARIABLES LIKE 'storage_engine'; +----------------+--------+ ✘ | Variable_name | Value | +----------------+--------+ | storage_engine | InnoDB | +----------------+--------+ mysql> DROP TABLE test1; mysql> CREATE TABLE test1(id INT UNSIGNED NOT NULL); mysql> SHOW CREATE TABLE test1G CREATE TABLE `test1` ( `id` int(10) unsigned NOT NULL ) ENGINE=MyISAM DEFAULT CHARSET=latin1 MySQL Idiosyncrasies That BITE - 2010.09
  • 36. Variable Scope Example (4) mysql> SHOW GLOBAL VARIABLES LIKE 'storage_engine'; +----------------+--------+ | Variable_name | Value | +----------------+--------+ | storage_engine | InnoDB | +----------------+--------+ MySQL Idiosyncrasies That BITE - 2010.09
  • 37. Variable Scope Example (4) mysql> SHOW GLOBAL VARIABLES LIKE 'storage_engine'; +----------------+--------+ | Variable_name | Value | +----------------+--------+ | storage_engine | InnoDB | +----------------+--------+ mysql> SHOW SESSION VARIABLES LIKE 'storage_engine'; +----------------+--------+ | Variable_name | Value | +----------------+--------+ | storage_engine | MyISAM | +----------------+--------+ MySQL Idiosyncrasies That BITE - 2010.09
  • 38. Variable Scope Example (4) mysql> SHOW GLOBAL VARIABLES LIKE 'storage_engine'; +----------------+--------+ | Variable_name | Value | +----------------+--------+ | storage_engine | InnoDB | +----------------+--------+ mysql> SHOW SESSION VARIABLES LIKE 'storage_engine'; +----------------+--------+ | Variable_name | Value | +----------------+--------+ | storage_engine | MyISAM | +----------------+--------+ MySQL Variables have two scopes. SESSION & GLOBAL MySQL Idiosyncrasies That BITE - 2010.09
  • 39. Variable Scope Syntax SHOW [GLOBAL | SESSION] VARIABLES; SET [GLOBAL | SESSION] variable = value; Default is GLOBAL since 5.0.2 http://dev.mysql.com/doc/refman/5.1/en/set-option.html http://dev.mysql.com/doc/refman/5.1/en/user-variables.html MySQL Idiosyncrasies That BITE - 2010.09
  • 40. Variable Scope Precautions Persistent connections (e.g. Java) Replication SQL_THREAD Be careful of existing connections! MySQL Idiosyncrasies That BITE - 2010.09
  • 41. 4. Storage Engines MySQL Idiosyncrasies That BITE - 2010.09
  • 42. Storage Engines Example - Creation CREATE TABLE test1 ( id INT UNSIGNED NOT NULL ✘ ) ENGINE=InnoDB DEFAULT CHARSET latin1; SHOW CREATE TABLE test1G *************************** 1. row ************* Table: test1 Create Table: CREATE TABLE `test1` ( `id` int(10) unsigned NOT NULL ) ENGINE=MyISAM DEFAULT CHARSET=latin1 MySQL Idiosyncrasies That BITE - 2010.09
  • 43. Storage Engines Example - Creation CREATE TABLE test1 ( id INT UNSIGNED NOT NULL ✘ ) ENGINE=InnoDB DEFAULT CHARSET latin1; SHOW CREATE TABLE test1G *************************** 1. row ************* Table: test1 Create Table: CREATE TABLE `test1` ( `id` int(10) unsigned NOT NULL ) ENGINE=MyISAM DEFAULT CHARSET=latin1 Even specified the storage engine reverted to the default MySQL Idiosyncrasies That BITE - 2010.09
  • 44. Storage Engines Example - Unexpected CREATE TABLE test1 ( id INT UNSIGNED NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET latin1; Query OK, 0 rows affected, 2 warnings (0.13 sec) SHOW WARNINGS; +---------+------+-----------------------------------------------+ | Level | Code | Message | +---------+------+-----------------------------------------------+ | Warning | 1286 | Unknown table engine 'InnoDB' | | Warning | 1266 | Using storage engine MyISAM for table 'test1' | +---------+------+-----------------------------------------------+ 2 rows in set (0.00 sec) MySQL Idiosyncrasies That BITE - 2010.09
  • 45. Storage Engines Example - Cause mysql> SHOW ENGINES; +------------+---------+-------------------------+--------------+ | Engine | Support | Comment | Transactions | +------------+---------+-------------------------+--------------+ | InnoDB | NO | Supports transaction... | NULL | | MEMORY | YES | Hash based, stored i... | NO | | MyISAM | DEFAULT | Default engine as ... | NO | MySQL Idiosyncrasies That BITE - 2010.09
  • 46. Storage Engines Example - Unexpected (2) mysql> CREATE TABLE test1 id INT UNSIGNED NOT NULL ) ENGINE=InnDB DEFAULT CHARSET latin1; ✘ Query OK, 0 rows affected, 2 warnings (0.11 sec) mysql> SHOW WARNINGS; +---------+------+-----------------------------------------------+ | Level | Code | Message | +---------+------+-----------------------------------------------+ | Warning | 1286 | Unknown table engine 'InnDB' | | Warning | 1266 | Using storage engine MyISAM for table 'test1' | +---------+------+-----------------------------------------------+ Be careful of spelling mistakes as well. MySQL Idiosyncrasies That BITE - 2010.09
  • 47. Using SQL_MODE for Storage Engines Recommended Configuration SQL_MODE = STRICT_ALL_TABLES, NO_ZERO_DATE, NO_ZERO_IN_DATE, NO_ENGINE_SUBSTITUTION; http://dev.mysql.com/doc/refman/5.1/en/server-sql-mode.html MySQL Idiosyncrasies That BITE - 2010.09
  • 48. Storage Engines Example - Solution SET SESSION SQL_MODE=NO_ENGINE_SUBSTITUTION; ✔ DROP TABLE IF EXISTS test3; CREATE TABLE test3 ( id INT UNSIGNED NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET latin1; ERROR 1286 (42000): Unknown table engine 'InnoDB' MySQL Idiosyncrasies That BITE - 2010.09
  • 49. 5. ACID MySQL Idiosyncrasies That BITE - 2010.09
  • 50. Atomicity - All or nothing MySQL Idiosyncrasies That BITE - 2010.09
  • 51. Atomicity - Table DROP TABLE IF EXISTS test1; CREATE TABLE test1( id INT UNSIGNED NOT NULL AUTO_INCREMENT, val CHAR(1) NOT NULL, PRIMARY KEY (id) ) ENGINE=MyISAM DEFAULT CHARSET latin1; MySQL Idiosyncrasies That BITE - 2010.09
  • 52. Atomicity - Data INSERT INTO test1(val) VALUES ('a'),('b'),('c'),('d'),('e'),('f'),('g'); mysql> SELECT * FROM test1; +----+-----+ | id | val | +----+-----+ | 1 | a | | 2 | b | | 3 | c | | 4 | d | | 5 | e | | 6 | f | | 7 | g | +----+-----+ 7 rows in set (0.00 sec) mysql> UPDATE test1 SET id = 10 - id; MySQL Idiosyncrasies That BITE - 2010.09
  • 53. Atomicity - Unexpected mysql> UPDATE test1 SET id = 10 - id; ERROR 1062 (23000): Duplicate entry '7' for key ✘ 'PRIMARY' mysql> SELECT * FROM test1; +----+-----+ | id | val | +----+-----+ | 9 | a | | 8 | b | | 3 | c | | 4 | d | | 5 | e | | 6 | f | | 7 | g | +----+-----+ MySQL Idiosyncrasies That BITE - 2010.09
  • 54. Atomicity - Unexpected (2) mysql> ROLLBACK; Query OK, 0 rows affected (0.00 sec) ✘ mysql> SELECT * FROM test1; +----+-----+ | id | val | +----+-----+ | 9 | a | | 8 | b | ... MySQL Idiosyncrasies That BITE - 2010.09
  • 55. Atomicity - Unexpected (2) mysql> ROLLBACK; Query OK, 0 rows affected (0.00 sec) ✘ mysql> SELECT * FROM test1; +----+-----+ | id | val | +----+-----+ | 9 | a | | 8 | b | ... Rollback has no effect on non transactional tables MySQL Idiosyncrasies That BITE - 2010.09
  • 56. Atomicity - Transactional Table ALTER TABLE test1 ENGINE=InnoDB; DELETE FROM test1 WHERE id > 5; SELECT * FROM test1; ✘ +----+-----+ | id | val | +----+-----+ | 3 | c | | 4 | d | | 5 | e | +----+-----+ ROLLBACK; SELECT * FROM test1; +----+-----+ | id | val | +----+-----+ | 3 | c | | 4 | d | | 5 | e | +----+-----+ MySQL Idiosyncrasies That BITE - 2010.09
  • 57. Atomicity - The culprit mysql> SHOW GLOBAL VARIABLES LIKE 'autocommit'; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | autocommit | ON | +---------------+-------+ MySQL Idiosyncrasies That BITE - 2010.09
  • 58. Atomicity - The culprit mysql> SHOW GLOBAL VARIABLES LIKE 'autocommit'; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | autocommit | ON | +---------------+-------+ The default for autocommit is ON. Great care is needed to change the default. MySQL Idiosyncrasies That BITE - 2010.09
  • 59. Atomicity - Recommendations Recommended Practice Always wrap SQL in transactions START TRANSACTION | BEGIN [WORK] COMMIT MySQL Idiosyncrasies That BITE - 2010.09
  • 60. Isolation - Transactions do not affect other transactions MySQL Idiosyncrasies That BITE - 2010.09
  • 61. Transaction Isolation - Levels MySQL supports 4 isolation levels READ-UNCOMMITTED READ-COMMITTED REPEATABLE-READ SERIALIZABLE http://dev.mysql.com/doc/refman/5.1/en/set-transaction.html http://ronaldbradford.com/blog/understanding-mysql-innodb-transaction-isolation-2009-09-24/ MySQL Idiosyncrasies That BITE - 2010.09
  • 62. Transaction Isolation - Default The default is REPEATABLE-READ SET GLOBAL tx_isolation = 'READ-COMMITTED'; This is a mistake http://ronaldbradford.com/blog/dont-assume-common-terminology-2010-03-03/ MySQL Idiosyncrasies That BITE - 2010.09
  • 63. Transaction Isolation - Default The default is REPEATABLE-READ SET GLOBAL tx_isolation = 'READ-COMMITTED'; This is a mistake Common mistake by Oracle DBA's supporting MySQL http://ronaldbradford.com/blog/dont-assume-common-terminology-2010-03-03/ MySQL Idiosyncrasies That BITE - 2010.09
  • 64. Oracle READ COMMITTED != MySQL READ-COMMITTED MySQL Idiosyncrasies That BITE - 2010.09
  • 65. Transaction Isolation - Binary Logging Errors SET GLOBAL tx_isolation='READ-COMMITTED'; Query OK, 0 rows affected (0.00 sec) ✘ mysql> insert into test1 values (1,'x'); ERROR 1598 (HY000): Binary logging not possible. Message: Transaction level 'READ-COMMITTED' in InnoDB is not safe for binlog mode 'STATEMENT' As of 5.1 this requires further configuration considerations MySQL Idiosyncrasies That BITE - 2010.09
  • 66. Durability - No committed transactions are lost MySQL Idiosyncrasies That BITE - 2010.09
  • 67. Data Safety MyISAM writes/flushes data every statement Does not flush indexes (*) InnoDB can relax durability flush all transactions per second innodb_flush_log_at_trx_commit sync_binlog innodb_support_xa MySQL Idiosyncrasies That BITE - 2010.09
  • 68. Auto Recovery InnoDB has it MyISAM does not - (*) Indexes MYISAM-RECOVER=FORCE,BACKUP REPAIR TABLE myisamchk MySQL Idiosyncrasies That BITE - 2010.09
  • 69. Recovery Time InnoDB is consistent Redo Log file size Slow performance in Undo (*) MyISAM grows with data volume MySQL Idiosyncrasies That BITE - 2010.09
  • 70. InnoDB Auto Recovery Example InnoDB: Log scan progressed past the checkpoint lsn 0 188755039 100624 16:37:44 InnoDB: Database was not shut down normally! InnoDB: Starting crash recovery. ✔ InnoDB: Reading tablespace information from the .ibd files... InnoDB: Restoring possible half-written data pages from the doublewrite InnoDB: buffer... InnoDB: Doing recovery: scanned up to log sequence number 0 193997824 InnoDB: Doing recovery: scanned up to log sequence number 0 195151897 InnoDB: 1 transaction(s) which must be rolled back or cleaned up InnoDB: in total 105565 row operations to undo InnoDB: Trx id counter is 0 18688 100624 16:37:45 InnoDB: Starting an apply batch of log records to the database... InnoDB: Progress in percents: 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 ... 99 InnoDB: Apply batch completed InnoDB: Last MySQL binlog file position 0 12051, file name ./binary-log. 000003 InnoDB: Starting in background the rollback of uncommitted transactions ... MySQL Idiosyncrasies That BITE - 2010.09
  • 71. MyISAM Repair needed Example 100126 22:44:35 [ERROR] /var/lib/mysql5/bin/mysqld: Table './XXX/ descriptions' is marked as crashed and should be repaired 100126 22:44:35 [Warning] Checking table: './XXX/descriptions' ✘ 100126 22:44:35 [ERROR] /var/lib/mysql5/bin/mysqld: Table './XXX/taggings' is marked as crashed and should be repaired 100126 22:44:35 [Warning] Checking table: './XXX/taggings' 100126 22:44:35 [ERROR] /var/lib/mysql5/bin/mysqld: Table './XXX/vehicle' is marked as crashed and should be repaired MySQL Idiosyncrasies That BITE - 2010.09
  • 72. 6. Data Security MySQL Idiosyncrasies That BITE - 2010.09
  • 73. User Privileges - Practices Best Practice CREATE USER goodguy@localhost IDENTIFIED BY 'sakila'; GRANT CREATE,SELECT,INSERT,UPDATE,DELETE ON odtug.* TO ✔ goodguy@localhost; Normal Practice CREATE USER superman@'%'; GRANT ALL ON *.* TO superman@'%'; ✘ http://dev.mysql.com/doc/refman/5.1/en/create-user.html http://dev.mysql.com/doc/refman/5.1/en/grant.html MySQL Idiosyncrasies That BITE - 2010.09
  • 74. User Privileges - Normal Bad Practice GRANT ALL ON *.* TO user@’%’ *.* gives you access to all tables in all schemas @’%’ give you access from any external location ALL gives you ALTER, ALTER ROUTINE, CREATE, CREATE ROUTINE, CREATE TEMPORARY TABLES, CREATE USER, CREATE VIEW, DELETE, DROP, EVENT, EXECUTE, FILE, INDEX, INSERT, LOCK TABLES, PROCESS, REFERENCES, RELOAD, REPLICATION CLIENT, REPLICATION SLAVE, SELECT, SHOW DATABASES, SHOW VIEW, SHUTDOWN, SUPER, TRIGGER, UPDATE, USAGE MySQL Idiosyncrasies That BITE - 2010.09
  • 75. User Privileges - Why SUPER is bad SUPER Bypasses read_only Bypasses init_connect Can Disable binary logging Change configuration dynamically No reserved connection MySQL Idiosyncrasies That BITE - 2010.09
  • 76. SUPER and Read Only $ mysql -ugoodguy -psakila odtug mysql> insert into test1(id) values(1); ✔ ERROR 1290 (HY000): The MySQL server is running with the --read-only option so it cannot execute this statement $ mysql -usuperman odtug mysql> insert into test1(id) values(1); Query OK, 1 row affected (0.01 sec) ✘ MySQL Idiosyncrasies That BITE - 2010.09
  • 77. SUPER and Read Only $ mysql -ugoodguy -psakila odtug mysql> insert into test1(id) values(1); ERROR 1290 (HY000): The MySQL server is running with the --read-only option so it cannot execute this statement ✔ $ mysql -usuperman odtug mysql> insert into test1(id) values(1); Query OK, 1 row affected (0.01 sec) ✘ Data inconsistency now exists MySQL Idiosyncrasies That BITE - 2010.09
  • 78. SUPER and Init Connect Common configuration #my.cnf practice to support [client] UTF8 init_connect=SET NAMES utf8 This specifies to use UTF8 for communication with client and data MySQL Idiosyncrasies That BITE - 2010.09
  • 79. SUPER and Init Connect (2) MySQL Idiosyncrasies That BITE - 2010.09
  • 80. SUPER and Init Connect (2) ✔ $ mysql -ugoodguy -psakila odtug mysql> SHOW SESSION VARIABLES LIKE 'ch%'; +--------------------------+----------+ | Variable_name | Value | +--------------------------+----------+ | character_set_client | utf8 | | character_set_connection | utf8 | | character_set_database | latin1 | | character_set_filesystem | binary | | character_set_results | utf8 | | character_set_server | latin1 | | character_set_system | utf8 | +--------------------------+----------+ MySQL Idiosyncrasies That BITE - 2010.09
  • 81. SUPER and Init Connect (2) ✔ $ mysql -usuperman odtug $ mysql -ugoodguy -psakila odtug ✘ mysql> SHOW SESSION VARIABLES LIKE mysql> SHOW SESSION VARIABLES LIKE 'ch%'; 'character%'; +--------------------------+----------+ +--------------------------+----------+ | Variable_name | Value | Variable_name| | Value | +--------------------------+----------+ +--------------------------+----------+ | character_set_client | character_set_client | utf8 | | latin1 | | character_set_connection | utf8 | | character_set_connection | latin1 | | character_set_database character_set_database | | latin1 | | latin1 | | character_set_filesystem | binary | | character_set_filesystem | binary | | character_set_results | character_set_results | utf8 | | latin1 | | character_set_server | character_set_server | latin1 | | latin1 | | character_set_system | character_set_system | utf8 | | utf8 | +--------------------------+----------+ +--------------------------+----------+ MySQL Idiosyncrasies That BITE - 2010.09
  • 82. SUPER and Init Connect (2) ✔ $ mysql -usuperman odtug $ mysql -ugoodguy -psakila odtug ✘ mysql> SHOW SESSION VARIABLES LIKE mysql> SHOW SESSION VARIABLES LIKE 'ch%'; 'character%'; +--------------------------+----------+ +--------------------------+----------+ | Variable_name | Value | Variable_name| | Value | +--------------------------+----------+ +--------------------------+----------+ | character_set_client | character_set_client | utf8 | | latin1 | | character_set_connection | utf8 | | character_set_connection | latin1 | | character_set_database character_set_database | | latin1 | | latin1 | | character_set_filesystem | binary | | character_set_filesystem | binary | | character_set_results | character_set_results | utf8 | | latin1 | | character_set_server | character_set_server | latin1 | | latin1 | | character_set_system | character_set_system | utf8 | | utf8 | +--------------------------+----------+ +--------------------------+----------+ Data integrity can be compromised MySQL Idiosyncrasies That BITE - 2010.09
  • 83. SUPER and Binary Log mysql> SHOW MASTER STATUS; +-------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +-------------------+----------+--------------+------------------+ | binary-log.000001 | 354 | | | +-------------------+----------+--------------+------------------+ mysql> DROP TABLE time_zone_leap_second; mysql> SET SQL_LOG_BIN=0; mysql> DROP TABLE time_zone_name; mysql> SET SQL_LOG_BIN=1; mysql> DROP TABLE time_zone_transition; mysql> SHOW MASTER STATUS; +-------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +-------------------+----------+--------------+------------------+ | binary-log.000001 | 674 | | | +-------------------+----------+--------------+------------------+ MySQL Idiosyncrasies That BITE - 2010.09
  • 84. SUPER and Binary Log (2) $ mysqlbinlog binary-log.000001 --start-position=354 --stop-position=674 # at 354 #100604 18:00:08 server id 1 end_log_pos 450 Query thread_id=1 exec_time=0 error_code=0 use mysql/*!*/; SET TIMESTAMP=1275688808/*!*/; ✘ DROP TABLE time_zone_leap_second /*!*/; # at 579 #100604 18:04:31 server id 1 end_log_pos 674 Query thread_id=2 exec_time=0 error_code=0 use mysql/*!*/; SET TIMESTAMP=1275689071/*!*/; DROP TABLE time_zone_transition /*!*/; DELIMITER ; # End of log file ROLLBACK /* added by mysqlbinlog */; Data auditability is now compromised MySQL Idiosyncrasies That BITE - 2010.09
  • 85. SUPER and reserved connection $ mysql -uroot ✔ mysql> show global variables like 'max_connections'; +-----------------+-------+ | Variable_name | Value | +-----------------+-------+ | max_connections | 3 | +-----------------+-------+ 1 row in set (0.07 sec) mysql> show global status like 'threads_connected'; +-------------------+-------+ | Variable_name | Value | +-------------------+-------+ | Threads_connected | 4 | +-------------------+-------+ When connected correctly, 1 connection is reserved for SUPER MySQL Idiosyncrasies That BITE - 2010.09
  • 86. SUPER and reserved connection - Unexpected $ mysql -uroot ERROR 1040 (HY000): Too many connections ✘ mysql> SHOW PROCESSLIST; +----+------+-----------+-------+---------+------+------------+--------------- | Id | User | Host | db | Command | Time | State | Info +----+------+-----------+-------+---------+------+------------+--------------- | 13 | root | localhost | odtug | Query | 144 | User sleep | UPDATE test1 ... | 14 | root | localhost | odtug | Query | 116 | Locked | select * from test1 | 15 | root | localhost | odtug | Query | 89 | Locked | select * from test1 When application users all use SUPER, administrator can't diagnose problem MySQL Idiosyncrasies That BITE - 2010.09
  • 87. 7. ANSI SQL MySQL Idiosyncrasies That BITE - 2010.09
  • 88. Group By Example SELECT country, COUNT(*) AS color_count FROM flags GROUP BY country; ✔ +-----------+-------------+ | country | color_count | +-----------+-------------+ | Australia | 3 | | Canada | 2 | | Japan | 2 | | Sweden | 2 | | USA | 3 | +-----------+-------------+ SELECT country, COUNT(*) ✘ FROM flags; +-----------+----------+ | country | COUNT(*) | +-----------+----------+ | Australia | 12 | +-----------+----------+ MySQL Idiosyncrasies That BITE - 2010.09
  • 89. Group By Example (2) SET SESSION sql_mode=ONLY_FULL_GROUP_BY; ✔ SELECT country, COUNT(*) FROM flags; ERROR 1140 (42000): Mixing of GROUP columns (MIN(),MAX(),COUNT(),...) with no GROUP columns is illegal if there is no GROUP BY clause http://ExpertPHPandMySQL.com Chapter 1 - Pg 31 MySQL Idiosyncrasies That BITE - 2010.09
  • 90. Using SQL_MODE for ANSI SQL Recommended Configuration SQL_MODE = STRICT_ALL_TABLES, NO_ZERO_DATE, NO_ZERO_IN_DATE, NO_ENGINE_SUBSTITUTION, ONLY_FULL_GROUP_BY; MySQL Idiosyncrasies That BITE - 2010.09
  • 91. 8. Case Sensitivity MySQL Idiosyncrasies That BITE - 2010.09
  • 92. Case Sensitivity String Comparison SELECT 'USA' = UPPER('usa'); +----------------------+ | 'USA' = UPPER('usa') | +----------------------+ | 1 | +----------------------+ SELECT 'USA' = 'USA', 'USA' = 'Usa', 'USA' = 'usa', 'USA' = 'usa' COLLATE latin1_general_cs AS different; +---------------+---------------+---------------+-----------+ | 'USA' = 'USA' | 'USA' = 'Usa' | 'USA' = 'usa' | different | +---------------+---------------+---------------+-----------+ | 1 | 1 | 1 | 0 | +---------------+---------------+---------------+-----------+ SELECT name, address, email FROM customer ✘ This practice is WHERE name = UPPER('bradford') unnecessary as does not utilize index if exists MySQL Idiosyncrasies That BITE - 2010.09
  • 93. Case Sensitivity Tables # Server 1 mysql> CREATE TABLE test1(id INT UNSIGNED NOT NULL); mysql> INSERT INTO test1 VALUES(1); mysql> INSERT INTO TEST1 VALUES(2); # Server 2 mysql> CREATE TABLE test1(id INT UNSIGNED NOT NULL); mysql> INSERT INTO test1 VALUES(1); mysql> INSERT INTO TEST1 VALUES(2); ERROR 1146 (42S02): Table 'test.TEST1' doesn't exist MySQL Idiosyncrasies That BITE - 2010.09
  • 94. Case Sensitivity Tables (2) - Operating Specific Settings # Server 1 - Mac OS X / Windows Default mysql> SHOW GLOBAL VARIABLES LIKE 'lower%'; +------------------------+-------+ | Variable_name | Value | +------------------------+-------+ | lower_case_file_system | ON | | lower_case_table_names | 2 | +------------------------+-------+ # Server 2 - Linux Default mysql> SHOW GLOBAL VARIABLES LIKE 'lower%'; +------------------------+-------+ | Variable_name | Value | +------------------------+-------+ | lower_case_file_system | OFF | | lower_case_table_names | 0 | +------------------------+-------+ Be careful. This default varies depending on Operating System MySQL Idiosyncrasies That BITE - 2010.09
  • 95. Other Gotchas MySQL Idiosyncrasies That BITE - 2010.09
  • 96. Other features the BITE Character Sets Data/Client/Application/Web Sub Queries Rewrite as JOIN when possible Views No always efficient Cascading Configuration Files SQL_MODE's not to use MySQL Idiosyncrasies That BITE - 2010.09
  • 98. DON'T ASSUME MySQL Idiosyncrasies That BITE - 2010.09
  • 99. Conclusion - The story so far MySQL Idiosyncrasies that BITE Definitions, Relational Database [Structure/Requirements], Oracle != MySQL, RDBMS Characteristics, Data Integrity [Expected | Unexpected | Warnings | Unexpected (2) | Warnings (2) ], Using SQL_MODE for Data Integrity, Data Integrity [ Expected ], Data Integrity with Dates [Expected | Unexpected ], Using SQL_MODE for Date Integrity, Data Integrity with Dates [Expected ], Transactions Example [ Tables | SQL | Expected | Using MyISAM | Unexpected [ (2)], Transactions Solution, Why use Non Transactional Tables? Variable Scope Example [ | (2)| (3)| [4])| Syntax Storage Engines Example [Creation | Unexpected | Cause | Unexpected (2) ] Using SQL_MODE for Storage Engines, Storage Engines Example [Solution] Atomicity [ Table | Data | Unexpected | Unexpected (2) | Transactional Table | The culprit | Recommendations ] Transaction Isolation [ Levels | Default | Binary Logging ] Durablilty [ Auto Recovery] User Privileges [Practices | Normal Bad Practice | Why SUPER is bad ] SUPER and [ Read Only | Init Connect [(2)] | Binary Log [2] | Reserved Connection [2] ] Group by Example [2], Using SQL_MODE for ANSI SQL Case Sensitivity [ String Comparison | Tables [2] ] More Gotchas, Character Sets, Sub Queries, Views Conclusion [ DON'T ASSUME | The Story so far | SQL_MODE ] RonaldBradford.com MySQL Idiosyncrasies That BITE - 2010.09
  • 100. Conclusion - SQL_MODE SQL_MODE = ✔ ALLOW_INVALID_DATES, ANSI_QUOTES, ERROR_FOR_DIVISION_ZERO, HIGH_NOT_PRECEDENCE,IGNORE_SPACE,NO_AUTO_CREATE_USER, NO_AUTO_VALUE_ON_ZERO,NO_BACKSLASH_ESCAPES, NO_DIR_IN_CREATE,NO_ENGINE_SUBSTITUTION, NO_FIELD_OPTIONS,NO_KEY_OPTIONS,NO_TABLE_OPTIONS, NO_UNSIGNED_SUBTRACTION,NO_ZERO_DATE, NO_ZERO_IN_DATE,ONLY_FULL_GROUP_BY (5.1.11), PAD_CHAR_TO_FULL_LENGTH (5.1.20), PIPES_AS_CONCAT, REAL_AS_FLOAT,STRICT_ALL_TABLES, STRICT_TRANS_TABLES ANSI, DB2, MAXDB, MSSQL, MYSQL323, MYSQL40, ORACLE, POSTGRESQL,TRADITIONAL http://dev.mysql.com/doc/refman/5.1/en/server-sql-mode.html MySQL Idiosyncrasies That BITE - 2010.09
  • 101. Conclusion - SQL_MODE SQL_MODE = ✔ ALLOW_INVALID_DATES, ANSI_QUOTES, ERROR_FOR_DIVISION_ZERO, HIGH_NOT_PRECEDENCE,IGNORE_SPACE,NO_AUTO_CREATE_USER, NO_AUTO_VALUE_ON_ZERO,NO_BACKSLASH_ESCAPES, NO_DIR_IN_CREATE,NO_ENGINE_SUBSTITUTION, NO_FIELD_OPTIONS,NO_KEY_OPTIONS,NO_TABLE_OPTIONS, NO_UNSIGNED_SUBTRACTION,NO_ZERO_DATE, NO_ZERO_IN_DATE,ONLY_FULL_GROUP_BY (5.1.11), PAD_CHAR_TO_FULL_LENGTH (5.1.20), PIPES_AS_CONCAT, REAL_AS_FLOAT,STRICT_ALL_TABLES, STRICT_TRANS_TABLES ✘ ANSI, DB2, MAXDB, MSSQL, MYSQL323, MYSQL40, ORACLE, POSTGRESQL,TRADITIONAL http://dev.mysql.com/doc/refman/5.1/en/server-sql-mode.html ? MySQL Idiosyncrasies That BITE - 2010.09
  • 102. RonaldBradford.com MySQL4OracleDBA.com MySQL Idiosyncrasies That BITE - 2010.09