Mysql nowwhat

1,187 views

Published on

MySQL Now What? @ RMOUG Quarterly Meeting 2011

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,187
On SlideShare
0
From Embeds
0
Number of Embeds
92
Actions
Shares
0
Downloads
41
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Mysql nowwhat

  1. 1. <Insert Picture Here>MySQL: What do I have or do now?Keith Larsonkeith.larson@oracle.comMySQL Community Managerwww.sqlhjalp.com/pdf/mysql_nowwhat.pdf
  2. 2. The following is intended to outline our general productdirection. It is intended for information purposes only, andmay not be incorporated into any contract. It is not acommitment to deliver any material, code, orfunctionality, and should not be relied upon in makingpurchasing decisions.The development, release, and timing of any features orfunctionality described for Oracle’s products remains atthe sole discretion of Oracle.Safe Harbor Statement
  3. 3. Show you code and options so you understand:–  Access–  Databases–  Tablespace–  Datatypes–  Selects–  Indexes–  Stored Routines–  Triggers–  Views–  Transactions–  Backups–  ReplicatedMySQL: AgendaCopyright Oracle Corporation 2011 3
  4. 4. Oracle MySQLDatabase (files) == Database Server InstanceDatabase Instance (memory) == Database Server InstanceSchema User == DatabaseUser == UserTable Space == Table SpaceStorage EngineMySQL TerminologyCopyright Oracle Corporation 2011 4
  5. 5. $ mysql -u rootERROR 1045 (28000): Access denied for user root@localhost(using password: NO)$ mysql -u root -pEnter password:$ mysqladmin -u root -p password <passwordhere>mysql> UPDATE mysql.userSET Password=PASSWORD(<passwordhere>)WHERE User=root;mysql> FLUSH PRIVILEGES;Copyright Oracle Corporation 2011 5MySQL: Account Access
  6. 6. http://dev.mysql.com/doc/refman/5.5/en/resetting-permissions.htmlDo not know the root password?Stop the service: # /etc/init.d/mysql stopRestart with skip grand: # mysqld_safe … --skip-grant-tables &/usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --user=mysql --log-error=/var/log/mysqld.log --pid-file=/var/lib/mysql/kdlarson-pc.pidConnect as root: # mysql -u rootSet new password :use mysql;mysql> update user set password=PASSWORD("NEW-ROOT-PASSWORD") where User=root;mysql> flush privileges;mysql> quitStop and Start then log in as root with password.MySQL: Account AccessCopyright Oracle Corporation 2011 6
  7. 7. http://dev.mysql.com/doc/refman/5.6/en/grant.htmlmysql> CREATE USER admin@localhost IDENTIFIED BY admin_pass;GRANT ALL ON *.* TO admin@localhost;mysql> flush privileges ;MySQL: Super User AccountsCopyright Oracle Corporation 2011 7
  8. 8. http://dev.mysql.com/doc/refman/5.6/en/grant.htmlmysql> CREATE USER ’clark@localhost IDENTIFIED BYsome_pass;mysql> GRANT ALTER, CREATE VIEW, CREATE, DELETE,DROP, GRANT OPTION, INDEX, INSERT, SELECT,SHOW VIEW, TRIGGER, UPDATE ON *.* TO’clark@localhost ;mysql> CREATE USER ’clark@% IDENTIFIED BYsome_pass;mysql> GRANT ALTER, CREATE VIEW, CREATE, DELETE,DROP, GRANT OPTION, INDEX, INSERT, SELECT,SHOW VIEW, TRIGGER, UPDATE ON *.* TO ’clark@%;mysql> flush privileges ;MySQL: Account Access-- UsersCopyright Oracle Corporation 2011 8
  9. 9. http://dev.mysql.com/doc/refman/5.6/en/grant.htmlCREATE USER adminhelp@% IDENTIFIED BY some_pass;GRANT ALTER, CREATE VIEW, CREATE, DELETE, DROP, GRANT OPTION,INDEX, INSERT, SELECT, SHOW VIEW, TRIGGER, UPDATE , RELOAD ON*.* TO adminhelp@% WITH GRANT OPTION;flush privileges ;CREATE USER adminuser@% IDENTIFIED BY some_pass;GRANT ALTER, CREATE VIEW, CREATE, DELETE, DROP, GRANT OPTION,INDEX, INSERT, SELECT, UPDATE ON *.* TO adminuser@% ;flush privileges ;MySQL: Account Access-- UsersCopyright Oracle Corporation 2011 9
  10. 10. http://dev.mysql.com/doc/refman/5.6/en/show-grants.htmlmysql [localhost] {root} (mysql) > select Host , User FROM mysql.user;+-----------+----------+| Host | User |+-----------+----------+| % | kdladmin || localhost | root || localhost | kdladmin |+-----------+----------+mysql> SHOW GRANTS FOR CURRENT_USER();mysql> SHOW GRANTS FOR root@localhost;+----------------------------------------------------------------------------------------------------------------------------------------+| Grants for root@localhost |+----------------------------------------------------------------------------------------------------------------------------------------+| GRANT ALL PRIVILEGES ON *.* TO root@localhost IDENTIFIED BY PASSWORD *8CD56861FDADF7A264741F27D502D1A8DAE0A8F7WITH GRANT OPTION |+----------------------------------------------------------------------------------------------------------------------------------------+MySQL: What User AccountsCopyright Oracle Corporation 2011 10
  11. 11. http://www.mysql.com/products/workbench/MySQL: WB User AccountsCopyright Oracle Corporation 2011 11
  12. 12. MySQL Logs for the DBAshttp://dev.mysql.com/doc/refman/5.5/en/server-logs.htmlhttp://dev.mysql.com/doc/refman/5.5/en/slave-logs.htmlError Log–  log-errorBinary Log–  Log-bin custom set via my.cnfRelay Log–  Log-bin custom set via my.cnfSlow Query Log–  Log-slow-queries–  Slow-query-time–  log-queries-not-using-indexesGeneral Log–  Log
  13. 13. MySQL Logs for the DBAsSHOW VARIABLES like %log%;+-----------------------------------------+-------------------------------------+| Variable_name | Value |+-----------------------------------------+-------------------------------------+| back_log | 50 || binlog_cache_size | 32768 || binlog_checksum | NONE || binlog_direct_non_transactional_updates | OFF || binlog_format | MIXED || binlog_row_image | FULL || binlog_rows_query_log_events | OFF || binlog_stmt_cache_size | 32768 || expire_logs_days | 0 || general_log | OFF || general_log_file | /var/lib/mysql/kdlarson-pc.log |…...
  14. 14. MySQL Logs for the DBAs… || max_binlog_cache_size | 18446744073709547520 || max_binlog_size | 1073741824 || max_binlog_stmt_cache_size | 18446744073709547520 || max_relay_log_size | 0 || relay_log | || relay_log_basename | || relay_log_index | || relay_log_info_file | relay-log.info || relay_log_info_repository | FILE || relay_log_purge | ON || relay_log_recovery | OFF || relay_log_space_limit | 0 || slow_query_log | OFF || slow_query_log_file | /var/lib/mysql/kdlarson-pc-slow.log || sql_log_bin | ON || sql_log_off | OFF || sync_binlog | 0 || sync_relay_log | 0 || sync_relay_log_info | 0 |+-----------------------------------------+-------------------------------------+
  15. 15. MySQL Logs for the DBAs in WB
  16. 16. http://dev.mysql.com/doc/refman/5.6/en/show.htmlSHOW CREATE TABLE tbl_nameSHOW CREATE PROCEDURE proc_nameSHOW CREATE TRIGGER trigger_nameSHOW CREATE VIEW view_nameSHOW PROCEDURE CODE proc_nameSHOW PROCEDURE STATUS [like_or_where]SHOW [FULL] PROCESSLISTSHOW GRANTS [FOR user]SHOW WARNINGS [LIMIT [offset,] row_count]SHOW {DATABASES | SCHEMAS} [LIKE pattern | WHERE expr]SHOW OPEN TABLES [{FROM | IN} db_name] [LIKE pattern | WHERE expr]SHOW BINARY LOGSSHOW MASTER LOGSMySQL: Show CommandsCopyright Oracle Corporation 2011 16
  17. 17. Use SHOW processlist to find out what is going on:+----+-------+-----------+----+---------+------+--------------+-------------------------------------+| Id | User | Host | db | Command | Time | State | Info |+----+-------+-----------+----+---------+------+--------------+-------------------------------------+| 6 | monty | localhost | bp | Query | 15 | Sending data | select * from station,station as s1 || 8 | monty | localhost | | Query | 0 | | show processlist |+----+-------+-----------+----+---------+------+--------------+-------------------------------------+Use KILL in mysql or mysqladmin to kill off runaway threads.How to find out how MySQL solves a queryRun the following commands and try to understand the output:* SHOW VARIABLES;* SHOW COLUMNS FROM City; (same as Describe City; || Desc City;)* EXPLAIN SELECT ...G* FLUSH STATUS;* SELECT ...;* SHOW STATUS;MySQL: Show ProcesslistCopyright Oracle Corporation 2011 17
  18. 18. A few ways to see what databases you have on your system:–  Review the directories–  Log in to MySQL server–  mysql> create database World;–  mysql> drop database World;–  Show databasesmysql> show databases;– +--------------------+– | Database |– +--------------------+– | information_schema |– | db_example |– | employees |– | exampledb |– | mysql |– | orig |– | performance_schema |– +--------------------+MySQL: Databases/SchemaCopyright Oracle Corporation 2011 18
  19. 19. http://www.mysql.com/products/workbench/MySQL: WB DatabasesCopyright Oracle Corporation 2011 19
  20. 20. mysql> SHOW ENGINES;+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+| Engine | Support | Comment | Transactions | XA | Savepoints |+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+| InnoDB | DEFAULT | Supports transactions, row-level locking, and foreign keys | YES | YES | YES || CSV | YES | CSV storage engine | NO | NO | NO || MyISAM | YES | MyISAM storage engine | NO | NO | NO || BLACKHOLE | YES | /dev/null storage engine (anything you write to it disappears) | NO | NO | NO || MEMORY | YES | Hash based, stored in memory, useful for temporary tables | NO | NO | NO || ARCHIVE | YES | Archive storage engine | NO | NO | NO || PERFORMANCE_SCHEMA | YES | Performance Schema | NO | NO | NO || FEDERATED | NO | Federated MySQL storage engine | NULL | NULL | NULL |+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+MySQL: EnginesCopyright Oracle Corporation 2011 20
  21. 21. mysql> use world;mysql> show tables;+-----------------+| Tables_in_world |+-----------------+| City || Country || CountryLanguage |+-----------------+3 rows in set (0.00 sec)MySQL: Table SpaceCopyright Oracle Corporation 2011 21
  22. 22. mysql> show create table City;CREATE TABLE `City` (`ID` int(11) NOT NULL AUTO_INCREMENT,`Name` char(35) NOT NULL DEFAULT ,`CountryCode` char(3) NOT NULL DEFAULT ,`District` char(20) NOT NULL DEFAULT ,`Population` int(11) NOT NULL DEFAULT 0,PRIMARY KEY (`ID`),KEY `CountryCode` (`CountryCode`),CONSTRAINT `city_ibfk_1` FOREIGN KEY (`CountryCode`) REFERENCES`Country` (`Code`)) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1MySQL: Table SpaceCopyright Oracle Corporation 2011 22
  23. 23. mysql > desc City;+-------------+----------+------+-----+---------+----------------+| Field | Type | Null | Key | Default | Extra |+-------------+----------+------+-----+---------+----------------+| ID | int(11) | NO | PRI | NULL | auto_increment || Name | char(35) | NO | | | || CountryCode | char(3) | NO | MUL | | || District | char(20) | NO | | | || Population | int(11) | NO | | 0 | |+-------------+----------+------+-----+---------+----------------+5 rows in set (0.06 sec)MySQL: Table SpaceCopyright Oracle Corporation 2011 23
  24. 24. http://www.mysql.com/products/workbench/MySQL: WB Table SpaceCopyright Oracle Corporation 2011 24
  25. 25. http://dev.mysql.com/doc/refman/5.5/en/data-types.htmlTEXT TYPESCHAR( ) A fixed section from 0 to 255 characters long. 4 bytesVARCHAR( ) A variable section from 0 to 255 characters long.TINYTEXT A string with a maximum length of 255 characters.TEXT A string with a maximum length of 65535 characters.BLOB A string with a maximum length of 65535 characters.MEDIUMTEXT A string with a maximum length of 16777215 characters.MEDIUMBLOB A string with a maximum length of 16777215 characters.LONGTEXT A string with a maximum length of 4294967295 characters.LONGBLOB A string with a maximum length of 4294967295 characters.CREATE TABLE `example_table` (...`value` varchar(100) DEFAULT NULL,...MySQL: DatatypesCopyright Oracle Corporation 2011 25
  26. 26. http://dev.mysql.com/doc/refman/5.5/en/data-types.htmlNUMBER TYPESTINYINT( ) -128 to 127 normal 0 to 255 UNSIGNED. 1 byte storageSMALLINT( ) -32768 to 32767 normal 0 to 65535 UNSIGNED. 2 byte storageMEDIUMINT( ) -8388608 to 8388607 normal 0 to 16777215 UNSIGNED. 3 byte storageINT( ) -2147483648 to 2147483647 normal 0 to 4294967295 UNSIGNED. 4 byte storageBIGINT( ) -9223372036854775808 to 9223372036854775807 normal0 to 18446744073709551615 UNSIGNED. 8 byte storageFLOAT A small number with a floating decimal point.DOUBLE( , ) A large number with a floating decimal point.DECIMAL( , ) A DOUBLE stored as a string , allowing for a fixed decimal point.MySQL uses 4 bytes for single-precision values and 8 bytes for double-precision valuesCreate Table: CREATE TABLE `example_table` (`example_table_id` int(10) unsigned NOT NULL AUTO_INCREMENT,.....or`example_table_id` bigint(12) unsigned NOT NULL auto_incrementMySQL: DatatypesCopyright Oracle Corporation 2011 26
  27. 27. http://dev.mysql.com/doc/refman/5.5/en/data-types.htmlDATE TYPESDATE YYYY-MM-DD.DATETIME YYYY-MM-DD HH:MM:SS.TIMESTAMP YYYYMMDDHHMMSS.TIME HH:MM:SS.Create Table: CREATE TABLE `example_table` (`example_table_id` int(10) unsigned NOT NULL AUTO_INCREMENT,`user_id` int(9) unsigned DEFAULT NULL,`date_recorded` datetime DEFAULT NULL,PRIMARY KEY (`example_table_id`),UNIQUE KEY `user_id` (`user_id`),KEY `date_recorded` (`date_recorded`)) ENGINE=InnoDBMySQL: DatatypesCopyright Oracle Corporation 2011 27
  28. 28. http://dev.mysql.com/doc/refman/5.5/en/data-types.htmlMISC TYPESENUM ( ) Short for ENUMERATION whichmeans that each column may have one of aspecified possible values.SET Similar to ENUM except each columnmay have more than one of the specifiedpossible values.…..`transfer_method` enum(OFF,EMAIL,FTP,BATCH POST,FTP-SSL,REAL TIMEPOST,CUSTOM) default NULL,`transfer_method` SET(OFF,EMAIL,FTP,BATCH POST,FTP-SSL’) default NULL,….MySQL: DatatypesCopyright Oracle Corporation 2011 28
  29. 29. extract basic database information using the SELECT commandWhat is wrong?mysql> SELECT-> C.Name , C.Population , CY.LifeExpectancy-> FROM City C-> INNER JOIN Country CY ON C.CountryCode = CY.Code-> WHERE CY.CountryCode = USA limit 5;MySQL: SelectsCopyright Oracle Corporation 2011 29
  30. 30. extract basic database information using the SELECT commandmysql> SELECT-> C.Name , C.Population , CY.LifeExpectancy-> FROM City C-> INNER JOIN Country CY ON C.CountryCode = CY.Code-> WHERE CY.CountryCode = USA limit 5;ERROR 1054 (42S22): Unknown column CY.CountryCode in where clauseMySQL: SelectsCopyright Oracle Corporation 2011 30
  31. 31. Use IN not ranges!Joins and IN are better than sub-queries!SELECT C.Name , C.Population , CY.LifeExpectancyFROM City CINNER JOIN Country CY ON C.CountryCode = CY.CodeWHERE C.CountryCode = USA limit 5;+--------------+------------+----------------+| Name | Population | LifeExpectancy |+--------------+------------+----------------+| New York | 8008278 | 77.1 || Los Angeles | 3694820 | 77.1 || Chicago | 2896016 | 77.1 || Houston | 1953631 | 77.1 || Philadelphia | 1517550 | 77.1 |+--------------+------------+----------------+MySQL: SelectsCopyright Oracle Corporation 2011 31
  32. 32. SELECT Continent , AVG(LifeExpectancy) as LifeExpectancy , AVG(Population) asPopulationFROM CountryGROUP BY ContinentORDER BY Continent DESC;+---------------+----------------+---------------+| Continent | LifeExpectancy | Population |+---------------+----------------+---------------+| South America | 70.94615 | 24698571.4286 || Antarctica | NULL | 0.0000 || Oceania | 69.71500 | 1085755.3571 || Africa | 52.57193 | 13525431.0345 || North America | 72.99189 | 13053864.8649 || Europe | 75.14773 | 15871186.9565 || Asia | 67.44118 | 72647562.7451 |+---------------+----------------+---------------MySQL: SelectsCopyright Oracle Corporation 2011 32
  33. 33. Joins and IN are better than sub-queries!SELECT C.Name , C.Population , CY.LifeExpectancyFROM City C INNER JOIN Country CY ON C.CountryCode = CY.CodeWHERE C.CountryCode IN (SELECT CountryCode FROM City CC WHEREPopulation > 3694820);+------------------+------------+----------------+| Name | Population | LifeExpectancy |+------------------+------------+----------------+| Mumbai (Bombay) | 10500000 | 62.5 || Seoul | 9981619 | 74.4 || Shanghai | 9696300 | 71.4 || Jakarta | 9604900 | 68.0 || Karachi | 9269265 | 61.1 || Istanbul | 8787958 | 71.0 || Moscow | 8389200 | 67.2 || New York | 8008278 | 77.1 |MySQL: Selects with sub-queriesCopyright Oracle Corporation 2011 33
  34. 34. MySQLData Dictionary = INFORMATION_SCHEMASee aggregated size per schema per engine:SELECT TABLE_SCHEMA, ENGINE, COUNT(*) AS count_tables,SUM(DATA_LENGTH+INDEX_LENGTH) AS size,SUM(INDEX_LENGTH) AS index_size FROMINFORMATION_SCHEMA.TABLESWHERE TABLE_SCHEMA NOT IN (mysql, INFORMATION_SCHEMA)AND ENGINE IS NOT NULL GROUP BY TABLE_SCHEMA, ENGINEhttp://code.openark.org/blog/mysql/useful-database-analysis-queries-with-information_schema
  35. 35. MySQLData Dictionary = INFORMATION_SCHEMA+--------------------+--------------------+--------------+------------+------------+| TABLE_SCHEMA | ENGINE | count_tables | size | index_size |+--------------------+--------------------+--------------+------------+------------+ || performance_schema | PERFORMANCE_SCHEMA | 48 | 0 | 0 || telco | InnoDB | 1 | 143425536 | 52592640 || test | InnoDB | 1 | 16384 | 0 || test | MyISAM | 2 | 2048 | 2048 || wb | MyISAM | 10 | 320223 | 47104 || world | InnoDB | 3 | 802816 | 196608 |+--------------------+--------------------+--------------+------------+------------+http://code.openark.org/blog/mysql/useful-database-analysis-queries-with-information_schema
  36. 36. MySQL Performance Schemause performance_schema;mysql> SELECT THREAD_ID, NUMBER_OF_BYTES-> FROM events_waits_history-> WHERE EVENT_NAME LIKE wait/io/file/%-> AND NUMBER_OF_BYTES IS NOT NULL;+-----------+-----------------+| THREAD_ID | NUMBER_OF_BYTES |+-----------+-----------------+| 11 | 66 || 11 | 47 || 11 | 139 || 5 | 24 || 5 | 834 |+-----------+-----------------+
  37. 37. MySQL Performance
  38. 38. MySQL: Performance optionsMySQL startup optionsInnodb can handle more memory and more cores..The buffer pool is for caching data and indexes in memory so set thefollowing to < 80% of the machine physical memory.Important performance options for indexes are:innodb_buffer_pool_size < 80% of memory. # default value is 8Minnodb_buffer_pool_instances = 1 #Increase for multi-core boxesinnodb_log_file_size=2G. #dependent on recovery speed required.innodb_thread_concurrency= (Cores/threads *2) + disksinnodb_flush_log_at_trx_commit = 1# innodb_file_per_table #depends on how many tables used. Get the big picture 1st.Copyright Oracle Corporation 2011 38
  39. 39. Laptop example for my.cnf[mysql.server]key_buffer_size=16Msort_buffer_size=4Mread_buffer_size=256Kbinlog_cache_size=64Kmax_connections=500wait_timeout=1800bulk_insert_buffer_size= 16Mmax_allowed_packet = 50Mtmp_table_size= 1Gtmpdir= /tmp/table_cache= 1024query_cache_size=128Mdefault-table-type= innodbrelay-log =/var/lib/mysql/kdlarson-relay-binCopyright Oracle Corporation 2011 39#innodbinnodb_data_home_dir = /var/lib/mysqlinnodb_log_group_home_dir = /var/lib/mysqlinnodb_data_file_path = ibdata1:10G:autoextendinnodb_buffer_pool_size=1Ginnodb_additional_mem_pool_size=40Minnodb_log_files_in_group=2# Set the log file size to about# 25 % of the buffer pool sizeinnodb_log_file_size=100Minnodb_log_buffer_size=128Minnodb_lock_wait_timeout=600innodb_table_locks = 0innodb_status_file = 1MySQL: Performance options
  40. 40. URLS to help for later:http://dev.mysql.com/doc/refman/5.6/en/mysql-indexes.htmlhttp://dev.mysql.com/doc/refman/5.6/en/show-index.htmlhttp://dev.mysql.com/doc/refman/5.6/en/create-index.htmlhttp://learnmysql.blogspot.com/2010/11/mysql-query-and-index-tuning.htmlhttp://www.slideshare.net/manikandakumar/mysql-query-and-index-tuninghttp://www.slideshare.net/osscube/indexing-the-mysql-index-key-to-performance-tuninghttp://effectivemysql.com/downloads/ImprovingPerformanceWithBetterIndexes-OOW-2011.pdfhttp://prajwal-tuladhar.net.np/2009/09/23/474/things-you-should-know-about-mysql-index/http://dev.mysql.com/doc/refman/5.5/en/innodb-monitors.htmlhttp://dev.mysql.com/doc/refman/5.5/en/innodb-parameters.htmlhttp://dev.mysql.com/doc/refman/5.5/en/innodb-buffer-pool.htmlhttp://www.mysqlperformanceblog.com/2006/07/17/show-innodb-status-walk-through/http://dev.mysql.com/doc/refman/5.5/en/server-parameters.htmlhttp://dev.mysql.com/doc/refman/5.5/en/server-system-variables.html#sysvar_key_buffer_sizehttp://dev.mysql.com/doc/refman/5.5/en/server-system-variables.html#sysvar_table_open_cachehttp://www.mysqlperformanceblog.com/2007/11/01/innodb-performance-optimization-basics/http://www.mysqlperformanceblog.com/2007/11/03/choosing-innodb_buffer_pool_size/MySQL: IndexesCopyright Oracle Corporation 2011 40
  41. 41. MySQL: Indexes (keys)When MySQL uses indexesUsing >, >=, =, <, <=, IF NULL and BETWEEN on a key.o SELECT * FROM table_name WHERE key_part1=1 and key_part2 > 5;o SELECT * FROM table_name WHERE key_part1 IS NULL;* When you use a LIKE that doesnt start with a wildcard.o SELECT * FROM table_name WHERE key_part1 LIKE jani%* Retrieving rows from other tables when performing joins.o SELECT * from t1,t2 where t1.col=t2.key_part* Find the MAX() or MIN() value for a specific index.o SELECT MIN(key_part2),MAX(key_part2) FROM table_name wherekey_part1=10* ORDER BY or GROUP BY on a prefix of a key.o SELECT * FROM foo ORDER BY key_part1,key_part2,key_part3* When all columns used in the query are part of one key.o SELECT key_part3 FROM table_name WHERE key_part1=1Copyright Oracle Corporation 2011 41
  42. 42. MySQL: Indexes (keys)When MySQL doesnt use an index* Indexes are NOT used if MySQL can calculate that it will probably be faster toscan the whole table.For example if key_part1 is evenly distributed between 1 and 100, its not good to usean index in the following query:o SELECT * FROM table_name where key_part1 > 1 and key_part1 < 90* If you are using HEAP tables and you dont search on all key parts with =* When you use ORDER BY on a HEAP table* If you are not using the first key parto SELECT * FROM table_name WHERE key_part2=1* If you are using LIKE that starts with a wildcardo SELECT * FROM table_name WHERE key_part1 LIKE %jani%* When you search on one index and do an ORDER BY on anothero SELECT * from table_name WHERE key_part1 = # ORDER BY key2Copyright Oracle Corporation 2011 42
  43. 43. MySQL: Indexes (keys)Optimizing tablesUse NOT NULL for columns which will not store null values. This is particularlyimportant for columns which you index.`e_id` bigint(12) unsigned NOT NULLDont create indexes you are not going to use.Use the fact that MySQL can search on a prefix of an index; If you have and INDEX(a,b), you dont need an index on (a).UNIQUE KEY `uq_id` (`u_id`,`q_id`),KEY `q_id` (`q_id`),Instead of creating an index on long CHAR/VARCHAR column, index just a prefix ofthe column to save space.CREATE TABLE `table_name` (`hostname` varchar(255) NOT NULL,KEY `hostname` (`hostname`(10))) ENGINE=InnoDBCopyright Oracle Corporation 2011 43
  44. 44. MySQL: Indexes (keys)SHOW INDEXES FROM CityG*************************** 1. row ***************************Table: CityNon_unique: 0Key_name: PRIMARYSeq_in_index: 1Column_name: IDCollation: ACardinality: 4051Sub_part: NULLPacked: NULLNull:Index_type: BTREEComment:Index_comment:Copyright Oracle Corporation 2011 44*************************** 2. row ***************************Table: CityNon_unique: 1Key_name: CountryCodeSeq_in_index: 1Column_name: CountryCodeCollation: ACardinality: 506Sub_part: NULLPacked: NULLNull:Index_type: BTREEComment:Index_comment:2 rows in set (0.02 sec)
  45. 45. More Urls for you to use laterhttp://effectivemysql.com/downloads/ExplainingTheMySQLEXPLAIN-OOW-2011.pdfhttp://prajwal-tuladhar.net.np/2009/09/26/481/know-more-about-mysql-explain/http://www.slideshare.net/ligaya/explainMySQL: Index – Use Explain!Copyright Oracle Corporation 2011 45
  46. 46. Use EXPLAIN on every query that you think is too slow!mysql> explain select t3.DateOfAction, t1.TransactionID-> from t1 join t2 join t3-> where t2.ID = t1.TransactionID and t3.ID = t2.GroupID-> order by t3.DateOfAction, t1.TransactionID;+-------+--------+---------------+---------+---------+------------------+------+---------------------------------+| table | type | possible_keys | key | key_len | ref | rows | Extra |+-------+--------+---------------+---------+---------+------------------+------+---------------------------------+| t1 | ALL | NULL | NULL | NULL | NULL | 11 | Using temporary; Using filesort || t2 | ref | ID | ID | 4 | t1.TransactionID | 13 | || t3 | eq_ref | PRIMARY | PRIMARY | 4 | t2.GroupID | 1 | |+-------+--------+---------------+---------+---------+------------------+------+---------------------------------+Types ALL and range signal a potential problem.MySQL: Index – Use Explain!Copyright Oracle Corporation 2011 46
  47. 47. mysql> EXPLAIN SELECT C.Name , T.Name FROM world.City C INNER JOINworld.Country T ON C.CountryCode = T.Code;+----+-------------+-------+------+---------------+-------------+---------+--------------+------+-------+| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra|+----+-------------+-------+------+---------------+-------------+---------+--------------+------+-------+| 1 | SIMPLE | T | ALL | PRIMARY | NULL | NULL | NULL | 264 ||| 1 | SIMPLE | C | ref | CountryCode | CountryCode | 3 | world.T.Code | 8 ||+----+-------------+-------+------+---------------+-------------+---------+--------------+------+-------+MySQL: Index – Use Explain!Copyright Oracle Corporation 2011 47
  48. 48. MySQL: Index – Use Explain!Copyright Oracle Corporation 2011 48
  49. 49. http://dev.mysql.com/doc/refman/5.5/en/stored-routines.htmlThe CREATE ROUTINE , ALTER ROUTINE , EXECUTE privilege is needed for storedroutines.mysql> delimiter //mysql> CREATE PROCEDURE simpleproc (OUT param1 INT)BEGINSELECT COUNT(*) INTO param1 FROM t;END//Query OK, 0 rows affected (0.00 sec)mysql> delimiter ;mysql> CALL simpleproc(@a);Query OK, 0 rows affected (0.00 sec)mysql> SELECT @a;+------+| @a |+------+| 3 |+------+1 row in set (0.00 sec)MySQL: Stored RoutinesCopyright Oracle Corporation 2011 49
  50. 50. MySQL: Stored RoutinesCopyright Oracle Corporation 2011 50
  51. 51. http://dev.mysql.com/doc/refman/5.5/en/create-trigger.htmlCREATE TABLE test1(a1 INT);CREATE TABLE test2(a2 INT);CREATE TABLE test3(a3 INT NOT NULL AUTO_INCREMENT PRIMARY KEY);CREATE TABLE test4(a4 INT NOT NULL AUTO_INCREMENT PRIMARY KEY,b4 INT DEFAULT 0);delimiter |CREATE TRIGGER testref BEFORE INSERT ON test1FOR EACH ROW BEGININSERT INTO test2 SET a2 = NEW.a1;DELETE FROM test3 WHERE a3 = NEW.a1;UPDATE test4 SET b4 = b4 + 1 WHERE a4 = NEW.a1;END; |delimiter ;MySQL: TriggersCopyright Oracle Corporation 2011 51
  52. 52. mysql> INSERT INTO test3 (a3)VALUES (NULL), (NULL), (NULL), (NULL), (NULL), (NULL), (NULL), (NULL), (NULL), (NULL);Query OK, 10 rows affected (0.04 sec)Records: 10 Duplicates: 0 Warnings: 0mysql> INSERT INTO test4 (a4) VALUES (0), (0), (0), (0), (0), (0), (0), (0), (0), (0);Query OK, 10 rows affected (0.01 sec)Records: 10 Duplicates: 0 Warnings: 0INSERT INTO test1 VALUES (1), (3), (1), (7), (1), (8), (4), (4);Query OK, 8 rows affected (0.01 sec)Records: 8 Duplicates: 0 Warnings: 0## Warnings: 1Statement is unsafe because it invokes a trigger or a stored function that inserts into anAUTO_INCREMENT column. Inserted values cannot be logged correctly.set BINLOG_FORMAT = MIXED;MySQL: TriggersCopyright Oracle Corporation 2011 52
  53. 53. http://dev.mysql.com/doc/refman/5.5/en/create-trigger.htmlmysql> SELECT * FROM test1;+------+| a1 |+------+| 1 || 3 || 1 || 7 || 1 || 8 || 4 || 4 |+------+8 rows in set (0.00 sec)MySQL: TriggersCopyright Oracle Corporation 2011 53mysql> SELECT * FROM test3;+----+| a3 |+----+| 2 || 5 || 6 || 9 || 10 |+----+5 rows in set (0.00 sec)mysql> SELECT * FROM test4;+----+------+| a4 | b4 |+----+------+| 1 | 3 || 2 | 0 || 3 | 1 || 4 | 2 || 5 | 0 || 6 | 0 || 7 | 1 || 8 | 1 || 9 | 0 || 10 | 0 |+----+------+10 rows in set (0.00 sec)mysql> SELECT * FROM test2;+------+| a2 |+------+| 1 || 3 || 1 || 7 || 1 || 8 || 4 || 4 |+------+8 rows in set (0.00 s
  54. 54. MySQL: TriggersCopyright Oracle Corporation 2011 54
  55. 55. > CREATE VIEW `world`.`city_view` ASSELECT C.Name as cityname , T.Name as countrynameFROM world.City CINNER JOIN world.Country T ON C.CountryCode = T.Code;Query OK, 0 rows affected (0.16 sec)SELECT cityname , countryname from city_view where countryname = "Zimbabwe";+--------------+-------------+| cityname | countryname |+--------------+-------------+| Harare | Zimbabwe || Bulawayo | Zimbabwe || Chitungwiza | Zimbabwe || Mount Darwin | Zimbabwe || Mutare | Zimbabwe || Gweru | Zimbabwe |+--------------+-------------+6 rows in set (0.13 sec)MySQL: ViewsCopyright Oracle Corporation 2011 55
  56. 56. MySQL: ViewsCopyright Oracle Corporation 2011 56
  57. 57. http://dev.mysql.com/doc/refman/5.5/en/commit.htmlTo disable autocommit mode, use the following statement:mysql> show variables like %autocommit%;+---------------+-------+| Variable_name | Value |+---------------+-------+| autocommit | ON |+---------------+-------+1 row in set (0.00 sec)mysql> SET autocommit=0;Query OK, 0 rows affected (0.00 sec)mysql> show variables like %autocommit%;+---------------+-------+| Variable_name | Value |+---------------+-------+| autocommit | OFF |+---------------+-------+1 row in set (0.01 sec)MySQL: TransactionsCopyright Oracle Corporation 2011 57
  58. 58. http://dev.mysql.com/doc/refman/5.5/en/commit.htmlTo disable autocommit mode for a single series of statementsuse the START TRANSACTION statement:START TRANSACTION;SELECT @A:=SUM(salary) FROM table1 WHERE type=1;UPDATE table2 SET summary=@A WHERE type=1;COMMIT;MySQL: TransactionsCopyright Oracle Corporation 2011 58
  59. 59. The best way to backup depends on the applicationPossible solutions:MySQL Enterprise BackupReplicationmysqldump…Best solution is using all three, I will explain.# mysqldump -p --all-databases --master-data=1 > /tmp/example_dump.sqlNot an online solution. Can/will lock tables.Copyright Oracle Corporation 2011 59MySQL: Backups
  60. 60. MySQL Enterprise Backup•  Online Backup for InnoDB•  Full, Incremental, Partial Backups (scriptable interface)•  Compression•  Point in Time, Full, Partial Recovery options•  Metadata on status, progress, history•  Unlimited Database Size•  Cross-Platform•  Windows, Linux, Unix•  Certified with Oracle Secure BackupMEB BackupFilesMySQLDatabase FilesmysqlbackupEnsures quick, online backup and recovery of your MySQL apps.Copyright Oracle Corporation 2011 60
  61. 61. MySQL Enterprise BackupUsage:ibbackup [--incremental lsn] [--sleep ms] [--suspend-at-end] [--compress [level]] [--include regexp] my.cnf backup-my.cnforibbackup --apply-log [--use-memory mb] [--uncompress] backup-my.cnforibbackup --apply-log --incremental [--use-memory mb] [--uncompress] incremental-backup-my.cnf full-backup-my.cnfThe backup program does NOT make a backup of the .frm files of the tables,and it does not make backups of MyISAM tables. To back up these items, either:- Use the mysqlbackup program.- Make backups of the .frm files with the Unix tar or the Windows WinZip or an equivalent tool both BEFORE andAFTER ibbackup finishes its work,and also store the MySQL binlog segment that is generated between themomentyou copy the .frm files to a backup and the moment ibbackup finishes its work.For extra safety, also use:mysqldump -l -d yourdatabasenameto dump the table CREATE statements in a human-readable form beforeibbackup finishes its work.Copyright Oracle Corporation 2011 61MySQL: Backups
  62. 62. MySQL: Replication OverviewNative in MySQLUsed for Scalability and HAAsynchronous as standardSemi-Synchronous supportadded in MySQL 5.5Each slave adds minimal load onmasterCopyright Oracle Corporation 2011 62
  63. 63. MySQL: Replication TopologiesMulti-Master RingCopyright Oracle Corporation 2011 63
  64. 64. MySQL: Replication OverviewNative in MySQLUsed for Scalability and HAAsynchronous as standardSemi-Synchronous supportadded in MySQL 5.5Each slave adds minimal load onmasterCopyright Oracle Corporation 2011 64
  65. 65. MySQL: Replicated?Copyright Oracle Corporation 2011 65Is your database replicated?mysql> SHOW MASTER STATUS;+---------------+----------+--------------+------------------+| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |+---------------+----------+--------------+------------------+| mysql-bin.003 | 73 | test | manual,mysql |+---------------+----------+--------------+------------------+mysql> SHOW SLAVE HOSTS;+------------+-----------+------+-----------+| Server_id | Host | Port | Master_id |+------------+-----------+------+-----------+| 192168010 | iconnect2 | 3306 | 192168011 || 1921680101 | athena | 3306 | 192168011 |+------------+-----------+------+-----------+
  66. 66. MySQL: Replicated Set upCopyright Oracle Corporation 2011 66 http://dev.mysql.com/doc/refman/5.5/en/replication-howto.html Master Bin logs enabled.  [mysqld]  log-bin=mysql-bin  server-id=1 Backup ( mysqldump or enterprise backup)# mysqldump -p --all-databases --master-data=1 > /tmp/example_dump.sql Slave my.cnf server id  [mysqld]  server-id=2 Replication user  mysql> CREATE USER repl@%.mydomain.com IDENTIFIED BYslavepass;  mysql> GRANT REPLICATION SLAVE ON *.* TOrepl@%.mydomain.com;
  67. 67. MySQL: Replicated Set upCopyright Oracle Corporation 2011 67http://dev.mysql.com/doc/refman/5.5/en/replication-howto.htmlImport data into slaveshell> mysql < /tmp/example_dump.sql Start the slave, using the --skip-slave-start Set master relationship with information from your dump  CHANGE MASTER TO  -> MASTER_HOST=master_host_name,  -> MASTER_USER=replication_user_name,  -> MASTER_PASSWORD=replication_password,  -> MASTER_LOG_FILE=recorded_log_file_name,  -> MASTER_LOG_POS=recorded_log_position; Start slave
  68. 68. ApplicationMySQL ServerSE2SE1Storage EnginesMasterBinlogReplicationMySQL ServerApplicationSlaveRelayBinlogL R R ASE2SE1Storage EnginesLogging/ReplicationSemi-SyncReplicatorRelay Log/ApplierSemi-SyncReceiverAckAvailable as two separate loadable componentsfor the master and the slaveCopyright Oracle Corporation 2011 68MySQL: 5.5 Semisynchronous ReplicationCopyright Oracle Corporation 2011 68
  69. 69. MySQL: Migrating to Semisynchronous Replication1. Install the plugin for the master and slave(s)master> INSTALL PLUGIN rpl_semi_sync_master SONAMEsemisync_master.so;slave> INSTALL PLUGIN rpl_semi_sync_slave SONAME semisync_slave.so;2. Enable semisynchronous replicationmaster> SET GLOBAL rpl_semi_sync_master_enabled = on;slave> SET GLOBAL rpl_semi_sync_slave_enabled = on;slave> STOP SLAVE IO_THREAD; START SLAVE IO_THREAD;3. Confirm that there is now 1 semisynchronous slavemaster> SHOW STATUS LIKE Rpl_semi_sync_master_clients;+------------------------------+-------+| Variable_name | Value |+------------------------------+-------+| Rpl_semi_sync_master_clients | 1 |+------------------------------+-------+Copyright Oracle Corporation 2011 69
  70. 70. Copyright Oracle Corporation 2011 70MySQL: Enterprise Monitor 2.3
  71. 71. •  Auto-detects, groups/maintainsMaster/Slave topologies•  Consolidated, real time status/synch check•  Notifications on Synch Issues•  Proactive vs reactiveSaves you time monitoring andcollecting replication status/synchdata from MySQL command line.Copyright Oracle Corporation 2011 71MySQL: Automated Replication Monitor
  72. 72. • Centralized monitoring of querieswithout Slow Query Log, SHOWPROCESSLIST;• Enabled via MySQL Connectors• Aggregated view of queryexecution counts, time, and rows• Visual “grab and go” correlationwith Monitor graphs• Traces query executions back tosource codeSaves you time parsing atomicexecutions from logs. Findsproblems you cannot find yourself.MySQL: Query Analyzer
  73. 73. Example query execwith variablesubstitutionTrace query execback to source codeFull exec EXPLAINMySQL: Query Execution Drill Downs
  74. 74. • 24 X 7 Problem ResolutionServices• Unlimited Support Incidents• Knowledge Base• Maintenance Releases, Bugfixes, Patches, Updates• MySQL Consultative Support• Staffed by experienced,seasoned MySQL EngineersOracle Premier Support for MySQL
  75. 75. http://education.oracle.com/pls/web_prod-plq-dad/db_pages.getpage?page_id=402&p_nl=JMSQAn Oracle certification recognizes and rewards your skills, dedication, andmotivation in the highly competitive technology industry.–  Oracle Certified Associate, MySQL 5.0/5.1/5.5•  Exam Number: 1Z0-870–  Oracle Certified Professional, MySQL 5.0 Developer•  Exam Number: 1Z0-871•  Exam Number: 1Z0-872–  Oracle Certified Professional, MySQL 5.0 Database Administrator•  Exam Number: 1Z0-873•  Exam Number: 1Z0-874–  Oracle Certified Expert, MySQL 5.1 Cluster DatabaseAdministrator•  PRIOR CERTIFICATION :Oracle Certified Professional, MySQL 5 DatabaseAdministrator•  Exam Number: 1Z0-875Copyright Oracle Corporation 2011 75MySQL: Certifications
  76. 76. mysql.com•  Download MySQL 5.5, MySQL Cluster 7.1 GA, GPL Products•  MySQL Products, Editions, Licensing Options•  TCO calculator•  Upcoming Events•  Customer use cases and success storiesdev.mysql.com•  Download MySQL 5.6 DMR and Labs “early access” features•  Developer Zone Articles, How to’s•  Community ResourceseDelivery.com• Download and evaluate all MySQL productsCopyright Oracle Corporation 2011 76MySQL: Additional Resources
  77. 77. Planet.mysql.com•  Blog feeds from the experts and the communityBooks:• MySQL by Paul DuBois• MySQL Administrators Bible• High Performance MySQL: Optimization, Backups, Replication• Effective MySQL bookforums.mysql.comCommunity interactionWorkbench–  http://www.mysql.com/products/workbench/demo.htmlCopyright Oracle Corporation 2011 77MySQL: Additional Resources
  78. 78. Video by Oracle ACE and MySQL expert Ronald Bradford: Top 20 Database Design Tips EveryArchitect Needs To Know : http://ow.ly/6FpzBNeed examples to apply immediately to improve database and application performances?This MySQL book can help. http://ow.ly/6TYpuFree download of chapter 1 "The Five Minute DBA" of Effective MySQL book by RonaldBradford at http://ow.ly/6TYSRFree chapter describes analysis tools used to help optimize a MySQL statement includingEXPLAIN and SHOW CREATE TABLE http://ow.ly/6TZhkTest Data used in this presentation is available at : http://dev.mysql.com/doc/index-other.htmlCopyright Oracle Corporation 2011 78MySQL: Additional Resources
  79. 79. Facebook: http://www.facebook.com/groups/mysqlcolorado/Meetup.com : http://www.meetup.com/Colorado-MySQL-Meetup-GroupCopyright Oracle Corporation 2011 79MySQL: Colorado Meetup GroupMeet other Colorado users of MySQL forconversation and fun! Come to a localMySQL Meetup to talk about the worldsmost popular open source database, getgreat tips and share new ideas. All levelsof experience welcome!
  80. 80. Movies http://www.buzzom.com/wp-content/uploads/2011/06/atthemovies.jpgSuperman http://www.supermanfun.com/wp-content/uploads/2011/08/Superman-Clark-Kent-Old-Pics-217x300.jpgclark kent http://1.bp.blogspot.com/-QO-jbX7c2Mk/TqkUu-Pad2I/AAAAAAAAKV8/DUTB60Fe1QI/s1600/clark%2Bkent.jpgKent a& superman http://www.toptenz.net/wp-content/uploads/2009/06/clark-kent-superman.jpgSupermanlogo http://www.superman-picture.com/images/superad.gifBooks http://publishingperspectives.com/wp-content/uploads/2011/05/stack-of-books.jpgPull back curtain on Oz http://911u.org/CoDR/images/TWOO1.jpgOrganized movies http://www.whirlwind.nu/images/journal_101119_movies_and_books_rainbow.jpgShow tables http://storefixtures.files.wordpress.com/2008/12/tables.jpg?w=200&h=192Datatypes http://mysql.lamphost.net/tech-resources/articles/mysql-cluster-50.gifthe riddler http://3.bp.blogspot.com/_57XeKo2AVGk/TNfvV8CmamI/AAAAAAAADds/zG9SGr3Dv9s/s1600/The_Riddler_3%5B1%5D.pngWorld http://www.scientificamerican.com/media/inline/3E0F9160-E7F2-99DF-358998AA3C1A910F_1.jpgPile of VHS http://punksonfilm.com/wordpress/wp-content/uploads/2010/07/vhs-pile.jpgOrganized movies http://1.bp.blogspot.com/_Yz5dzGyEAnQ/S0EHwl-_-ZI/AAAAAAAAAA8/bbSuUFf8Zpw/s320/IMG_0644.JPGIndex Cards "DNA" By ayalan http://prajwal-tuladhar.net.np/2009/09/23/474/things-you-should-know-about-mysql-index/Safe http://www.thomasgregory.com/Portals/40231/images//Vault.JPGCopyright Oracle Corporation 2011 80Credits
  81. 81. <Insert Picture Here>Thanks for attending!keith.larson@oracle.com

×