O slideshow foi denunciado.
Utilizamos seu perfil e dados de atividades no LinkedIn para personalizar e exibir anúncios mais relevantes. Altere suas preferências de anúncios quando desejar.

My sql susecon_crashcourse_2012

1.180 visualizações

Publicada em

MySQL SUSECON crashcourse 2012

  • Seja o primeiro a comentar

My sql susecon_crashcourse_2012

  1. 1. http://hiconsumption.com/2012/07/star-wars-recreations-of-iconic-photographs-by-david-eger/
  2. 2. <Insert Picture Here>MySQL: Crash CourseKeith Larsonkeith.larson@oracle.comMySQL Community Managerhttp://sqlhjalp.com/pdf/MySQL_susecon_crashcourse_2012.pdf
  3. 3. The following is intended to outline our general productdirection. It is intended for information purposes only,and may 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 StatementTM & © 2012 Lucasfilm Ltd. All rights reserved. ©2012 The LEGO Group. All rights reserved.http://jennysung.com/wp-content/uploads/2012/03/stormtroopers1.jpg
  4. 4. Who am I and who are you?Who are you?– DBAs?– Developers?• PHP• Perl• Python• .net– Admins?keith.larson@oracle.comMySQL Community Managerhttp://www.sqlhjalp.com/Started with MySQL during the dot.com days.Primary real world work was with a MySQL InnoDB replicated chain environment thateasily held over 4 billion rows of user data.Numerous other sites developed on LAMP stack over the last 13 years.Copyright Oracle Corporation 2012 4http://3.bp.blogspot.com/-IXu-9-s-8Fg/TvTs8JbgvdI/AAAAAAAAJpo/at-pjGsDlXg/s1600/Bat%2BStorm%2BTrooper%2BLego.jpg
  5. 5. • This is about you and your needs...• I can also cover..• Oracles Investment into MySQL• A high-level overview• Familiarize with the key concepts• MySQL EditionsSession AgendaCopyright Oracle Corporation 2012 5Investment Overview Key Concepts Editions
  6. 6. Copyright Oracle Corporation 2012 6
  7. 7. mysql> The official way to pronounce“MySQL” is “My Ess Que Ell”but we do not mind if you pronounce it as“my sequel”It was named after Montys Daughter Maria.PronunciationCopyright Oracle Corporation 2012 7
  8. 8. • Oracles Investment into MySQLSession AgendaCopyright Oracle Corporation 2012 8http://www.thesambarnes.com/wp-content/themes/ImpreZZ/images/profitable-web-project-decisions.jpgTM & © LUCASFILM LTD. 2012. ALL RIGHTS RESERVED
  9. 9. MySQL On the Coverhttp://www.oracle.com/technetwork/issue-archive/2011/11-jan/index-191276.htmlCopyright Oracle Corporation 2012 9
  10. 10. Built togetherTested togetherManaged togetherServiced togetherBased on open standardsLower costLower riskMore reliableHardware and SoftwareEngineered to Work TogetherMySQL Completes The Stack
  11. 11. Q2 CY2010 Q3 CY2010 Q4 CY2010 Q1 CY2011• MySQL Workbench 5.2GA!• MySQL Database 5.5• MySQL Enterprise Backup 3.5• MySQL Enterprise Monitor 2.3• MySQL Cluster Manager 1.1All GA!A Better MySQLQ2 CY2011•MySQL Enterprise Monitor 2.2•MySQL Cluster 7.1• MySQL Cluster Manager 1.0All GA!• MySQL Database5.6• MySQL Cluster 7.2DMR*and MySQL Labs!(“early and often”)*Development Milestone ReleaseContinuous Innovation with more productreleases than ever beforeCopyright Oracle Corporation 2012 11
  12. 12. Enterprise 2.0SaaS, CloudWeb OEM / ISV’sTelecommunicationsRely on MySQLIndustry Leading CustomersCopyright Oracle Corporation 2012 12
  13. 13. >70% of Oracle Shops run MySQLCopyright Oracle Corporation 2012 13MySQL: Still Free, Open to the CommunityAvailable to download and use under the GPL:• MySQL Database (Community Server)• MySQL Cluster• MySQL Workbench Community Edition• MySQL Connectors• MySQL Proxy• Documentation (free to use, not covered under GPL)• Forums
  14. 14. • A high-level overviewSession AgendaCopyright Oracle Corporation 2012 14http://farm3.static.flickr.com/2646/3678467304_66908d66d4.jpgTM & © LUCASFILM LTD. 2012. ALL RIGHTS RESERVED
  15. 15. MySQL Terminology• Database ( Files )• Database Instance ( memory)memory• Schema• User• Table Space• Database Server Instance• Database Server Instance• Database• User• Table Space• Storage EngineCopyright Oracle Corporation 2012 15
  16. 16. http://carlos.syr.edu/oracle-database-architecture/Oracle Database ArchitectureCopyright Oracle Corporation 2012 16
  17. 17. MySQL Database ArchitectureCopyright Oracle Corporation 2012 17
  18. 18. Error Log– log-errorBinary Log– Log-bin custom set via my.cnfSlow Query Log– Log-slow-queries– Slow-query-time– log-queries-not-using-indexesGeneral Log– Loghttp://dev.mysql.com/doc/refman/5.5/en/server-logs.htmlMySQL LogsCopyright Oracle Corporation 2012 18
  19. 19. Standalone (mysqld)UNIX daemonWindows serviceRegular process on UNIX or WindowsEmbedded (libmysqld)Shared / Dynamic libraryMySQL ServerCopyright Oracle Corporation 2012 19
  20. 20. InnoDBDB1InnoDBDB2MyISAMDB3InnoDB MyISAMCoreSQL-query processing, …Plugin 1 Plugin 2 Plugin 3The CorePluginsStorage EnginesFull-text searchpluginsAudit pluginsAuthenticationplugins…MySQL Server ComponentsCopyright Oracle Corporation 2012 20
  21. 21. The Storage Engine (SE) defines datastorage and retrievalEvery regular table belongs to some SEMost notable Storage Engines:InnoDB (default since 5.5)– fully transactional SEMyISAM (default prior to 5.5)– NON-transactional SEMySQL Storage EnginesCopyright Oracle Corporation 2012 21mysql> 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`)) ENGINE=InnoDB DEFAULT CHARSET=latin1;mysql> SHOW ENGINES;
  22. 22. Select a specialized storage engine for a particular applicationneed.InnoDB: a high-reliability and high-performance storage enginefor MySQL designed for transaction processing. It follows theACID model. Row-level locking and Oracle-style consistentreads increase multi-user concurrency and performanceMyISAM: -oldest storage engine has many featuresthat have been developed over years.Memory: creates tables with contents that are stored in memory.MySQL Cluster offers the same features as the MEMORYengine with higher performance levels, and providesadditional featuresMySQL Storage EnginesCopyright Oracle Corporation 2012 22
  23. 23. CSV: data file is a plain text fileARCHIVE: is used for storing large amounts of data withoutindexes in a very small footprint.BLACKHOLE:accepts data but throwsit away and does not store it but thebinary log is enabled.MySQL Storage EnginesCopyright Oracle Corporation 2012 23
  24. 24. Open SourceLimitations Relating to Storage EnginesHorizontal partitioning(distribute rows, not columns)Partitioning functions:The modulusRangeInternal hashing functionLinear hashing functionMySQL Partitioninghttp://dev.mysql.com/doc/refman/5.1/en/partitioning-overview.htmlhttp://dev.mysql.com/tech-resources/articles/mysql_55_partitioning.htmlCopyright Oracle Corporation 2012 24CREATE TABLE members (firstname VARCHAR(25) NOT NULL,lastname VARCHAR(25) NOT NULL,username VARCHAR(16) NOT NULL,email VARCHAR(35),joined DATE NOT NULL)PARTITION BY RANGE( YEAR(joined) ) (PARTITION p0 VALUES LESS THAN (1960),PARTITION p1 VALUES LESS THAN (1970),PARTITION p2 VALUES LESS THAN (1980),PARTITION p3 VALUES LESS THAN (1990),PARTITION p4 VALUES LESS THAN MAXVALUE);
  25. 25. MySQL ReplicationCopyright Oracle Corporation 2012 25
  26. 26. Top used Feature in MySQLUsed for Scalability and HAWrite to one masterRead from many slaves, easily add more asneededPerfect for read/write intensive appsAsynchronous as standardSemi-Synchronous support added in MySQL 5.5Each slave adds minimal load on masterReplication formats:Statement-based replication (SBR): propagateSQL statementsRow-based replication (RBR): propagate rowchangesMixed-based replication: SBR or RBR dependingon the queryhttp://sqlhjalp.blogspot.com/2012/09/mysql-replication-101-overview.htmlMySQL Replication OverviewCopyright Oracle Corporation 2012 26http://www.betabeat.com/2011/09/02/clone-wars-rise-of-the-fast-follower-startups/TM & © LUCASFILM LTD. 2012. ALL RIGHTS RESERVED
  27. 27. sqlhjalp.com/pdf/2012_Scale_Replication.pdf available for reviewMySQL Replication TopologiesCopyright Oracle Corporation 2012 27
  28. 28. http://sqlhjalp.com/pdf/2012_Scale_Replication.pdfMySQL Replication OverviewCopyright Oracle Corporation 2012 28Replication ThreadsBinlog dump threadSlave I/O threadSlave SQL threadReplication Filesrelay logmaster info logrelay log info log
  29. 29. http://sqlhjalp.com/pdf/2012_Scale_Replication.pdfMySQL Replication OverviewCopyright Oracle Corporation 2012 29
  30. 30. http://sqlhjalp.com/pdf/2012_Scale_Replication.pdfMySQL Replication OverviewCopyright Oracle Corporation 2012 30
  31. 31. The way depends on the applicationPossible solutions:Replication?mysqldumpMySQL Enterprise BackupBest solution is using all three.# mysqldump -p --all-databases --master-data=1 > /tmp/example_dump.sqlNot an online solution. Can/will lock tables.MySQL BackupCopyright Oracle Corporation 2012 31http://farm6.static.flickr.com/5024/5554971120_df447dd31c.jpgTM & © LUCASFILM LTD. 2012. ALL RIGHTS RESERVED
  32. 32. Online Backup for InnoDBSupport for MyISAM (Read-only)High Performance Backup & RestoreCompressed BackupFull BackupIncremental BackupPartial BackupsPoint in Time RecoveryUnlimited Database SizeCross-PlatformWindows, Linux, UnixEnsures quick, online backup and recovery of your MySQL apps.MySQL Enterprise BackupCopyright Oracle Corporation 2012 32
  33. 33. Usage: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 2012 33MySQL Enterprise Backup
  34. 34. • Familiarize with the key conceptsSession AgendaCopyright Oracle Corporation 2012 34http://www.flickr.com/photos/kalexanderson/7658081474/in/set-72157628651430439/TM & © LUCASFILM LTD. 2012. ALL RIGHTS RESERVED
  35. 35. Highly configurableCommand line optionsConfiguration files (plain-text, INI-like files with groups)Several configuration files (/etc, $HOME, …)The last value takes precedenceDefault configuration are examples and might be not so good forPerformance, ...MySQL ConfigurationCopyright Oracle Corporation 2012 35One example: SQL MODEVery important variableAffects data consistency!It might be remembered …… or it might be notThus: set it once for allRecommended value:STRICT_ALL_TABLES |NO_ZERO_DATE |NO_ZERO_IN_DATE |NO_ENGINE_SUBSTITUTION |NO_AUTO_CREATE_USER |IGNORE_SPACE |ERROR_FOR_DIVISION_BY_ZERO
  36. 36. Secure the installationDon’t run under ‘root’Have separate directories (configuration, data, binarylogs, …)Change ‘root’ passwordRemove default accountsPost-installation steps: SecurityMySQLCopyright Oracle Corporation 2012 36
  37. 37. MySQL products support unicode.Full Unicode 5.0 is supported for data, and for metadata wesupport only characters for Basic Multilingual Plane (BMP).Database or schema in Oracle world.Current database (per connection)Database – a set of files in “the data directory”System database (mysql)Virtual databases:INFORMATION_SCHEMAPERFORMANCE_SCHEMAMySQL ConceptsCopyright Oracle Corporation 2012 37
  38. 38. User: username@hostnamePrecisely: ‘user-name-mask’@’host-name-mask’Host name – client host name (“from” host name)User name mask:can be empty (anonymous user) – all usersHost name mask:can be empty – all host names (%)can have ‘%’ (e.g.: %.foo.com)MySQL PrivilegesCopyright Oracle Corporation 2012 38
  39. 39. Connecting as foo from localhost…Should be foo@%, right ?The most specific values are usedBUT: host name matching is done before user namemysql -u foo -h localhost -p‘’@localhost will be chosen !MySQL Privileges+-----------+-------------------+| Host | User |+-----------+-------------------+| localhost | || localhost | bar || % | foo || localhost | root |+-----------+-------------------+Copyright Oracle Corporation 2012 39
  40. 40. $ mysql -u rootERROR 1045 (28000): Access denied for user root@localhost (usingpassword: 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 2012 40MySQL Privileges -- Root
  41. 41. Do not know the root password?Stop the service: # /etc/init.d/mysql stopRestart with skip grand: # mysqld_safe --skip-grant-tables &Connect 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 the service: # /etc/init.d/mysql stopStart the service: # /etc/init.d/mysql startLog in as root with password. # mysql -u root -pCopyright Oracle Corporation 2012 41http://dev.mysql.com/doc/refman/5.5/en/resetting-permissions.htmlMySQL Privileges
  42. 42. mysql> CREATE USER monty@localhost IDENTIFIED BY some_pass;mysql> GRANT ALTER, CREATE VIEW, CREATE, DELETE, DROP,GRANT OPTION, INDEX, INSERT, SELECT, SHOW VIEW, TRIGGER,UPDATE ON *.* TO monty@localhostWITH GRANT OPTION;mysql> CREATE USER monty@% IDENTIFIED BY some_pass;mysql> GRANT ALTER, CREATE VIEW, CREATE, DELETE, DROP,GRANT OPTION, INDEX, INSERT, SELECT, SHOW VIEW, TRIGGER,UPDATE ON *.* TO monty@%mysql> flush privileges ;Copyright Oracle Corporation 2012 42MySQL Privileges -- usershttp://dev.mysql.com/doc/refman/5.6/en/grant.html
  43. 43. mysql> CREATE USER admin@localhost IDENTIFIED BY admin_pass;mysql> GRANT ALL ON *.* TO admin@localhost;mysql> flush privileges ;MySQL Super User AccountsCopyright Oracle Corporation 2012 43http://dev.mysql.com/doc/refman/5.6/en/grant.htmlhttp://www.boston.com/partners/greader/prfmkt/images/X00-m5707191.jpgTM & © LUCASFILM LTD. 2012. ALL RIGHTS RESERVED
  44. 44. SHOW 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 2012 44http://dev.mysql.com/doc/refman/5.6/en/show.html
  45. 45. 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 ...G* EXPLAIN SELECT ...G* FLUSH STATUS;* SELECT ...;* SHOW STATUS;MySQL Show ProcesslistCopyright Oracle Corporation 2012 45
  46. 46. A few ways to see what databases you have on your system:– cd /var/lib/mysql• Review the directories– Log in to MySQL server• Show databases– mysql -u root -p» Enter password:– show databases;– +--------------------+– | Database |– +--------------------+– | information_schema |– | db_example |– | employees |– | exampledb |– | mysql |– | orig |– | performance_schema |– +--------------------+MySQL Databases/SchemaCopyright Oracle Corporation 2012 46
  47. 47. mysql> use world; ---- DATABASE / SCHEMAmysql> show tables; ---- TABLE SPACE+-----------------+| Tables_in_world |+-----------------+| City || Country || CountryLanguage |+-----------------+3 rows in set (0.00 sec)MySQL Table SpaceCopyright Oracle Corporation 2012 47
  48. 48. 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=latin1Copyright Oracle Corporation 2012 48MySQL Table Space
  49. 49. 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)Copyright Oracle Corporation 2012 49MySQL Table Space
  50. 50. TEXT TYPESCHAR( )A fixed section from 0 to 255 characters long.VARCHAR( ) 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 2012 50http://dev.mysql.com/doc/refman/5.5/en/data-types.html
  51. 51. NUMBER TYPESTINYINT( ) -128 to 127 normal 0 to 255 UNSIGNED.SMALLINT( ) -32768 to 32767 normal 0 to 65535 UNSIGNED.MEDIUMINT( ) -8388608 to 8388607 normal 0 to 16777215 UNSIGNED.INT( ) -2147483648 to 2147483647 normal 0 to 4294967295 UNSIGNED.BIGINT( )-9223372036854775808 to 9223372036854775807 normal0 to 18446744073709551615 UNSIGNED.FLOAT 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.Create 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_incrementCopyright Oracle Corporation 2012 51http://dev.mysql.com/doc/refman/5.5/en/data-types.htmlMySQL Datatypes
  52. 52. DATE 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=InnoDBCopyright Oracle Corporation 2012 52http://dev.mysql.com/doc/refman/5.5/en/data-types.htmlMySQL Datatypes
  53. 53. MISC TYPESENUM ( ) Short for ENUMERATION which means that each column mayhave one of a specified possible values.SET Similar to ENUM except each column may have more than one of thespecified possible values.…..`transfer_method` enum(OFF,EMAIL,FTP,BATCH POST,FTP-SSL,REALTIME POST,CUSTOM) default NULL,….Copyright Oracle Corporation 2012 53http://dev.mysql.com/doc/refman/5.5/en/data-types.htmlMySQL Datatypes
  54. 54. 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 Indexeshttp://prajwal-tuladhar.net.np/2009/09/23/474/things-you-should-know-about-mysql-index/Copyright Oracle Corporation 2012 54
  55. 55. 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 2012 55MySQL Indexes (keys)
  56. 56. MySQL Index optionsMySQL startup optionsWhen tuning a MySQL server, the two most important variables to configureare key_buffer_size and table_open_cache.The buffer pool is for caching data and indexes in memory so set thefollowing to < 80% of the machine physical memory.Important options are:innodb_buffer_pool_size < 80% of memory. # default value is 8Minnodb_log_file_size=2G. #dependent on recovery speed required.innodb_log_buffer_size=4Minnodb_thread_concurrency=8 # Defaultinnodb_flush_method=O_DIRECT # double buffering and swap are bad# innodb_file_per_table #depends on how many tables used. Get the big picture 1st.Copyright Oracle Corporation 2012 56
  57. 57. 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 2012 57
  58. 58. 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` char(255) NOT NULL,KEY `hostname` (`hostname`(10))) ENGINE=InnoDBCopyright Oracle Corporation 2012 58MySQL Indexes (keys)
  59. 59. 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 2012 59
  60. 60. 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 2012 60
  61. 61. More Urls for you to use later about Explainhttp://dev.mysql.com/doc/refman/5.5/en/explain.htmlhttp://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 2012 61
  62. 62. The 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 2012 62http://dev.mysql.com/doc/refman/5.5/en/stored-routines.html
  63. 63. CREATE 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 2012 63http://dev.mysql.com/doc/refman/5.5/en/create-trigger.html
  64. 64. > 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 2012 64http://dev.mysql.com/doc/refman/5.5/en/views.html
  65. 65. To 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 2012 65http://dev.mysql.com/doc/refman/5.5/en/commit.html
  66. 66. To 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 2012 66http://dev.mysql.com/doc/refman/5.5/en/commit.html
  67. 67. • MySQL EditionsSession AgendaCopyright Oracle Corporation 2012 67http://www.flickr.com/photos/kalexanderson/7719347704/in/set-72157628651430439/TM & © LUCASFILM LTD. 2012. ALL RIGHTS RESERVED
  68. 68. MySQL WorkbenchCopyright Oracle Corporation 2012 68http://sqlhjalp.blogspot.com/2012/08/workbench-database-migration-wizard.html
  69. 69. Workbench – visual database design application that can be used to efficiently design,manage and document database schemataConnectors – ODBC, Java, .Net, MXJ, C/C++, DBI, Ruby, Python, etc.Community: -- http://dev.mysql.com/downloads/Freely downloadable version of the worlds most popular open source database.It is available under the GPL license and is supported by a huge and activecommunity of open source developers.Enterprise: -- eDelivery.com (Free for 30 days)Paid subscription includes support and the following• MySQL Enterprise Backup• MySQL Enterprise Security– External Authentication• MySQL Enterprise Scalability– Thread Pool• MySQL Enterprise High Availability– Oracle VM Template– Windows Clustering• MySQL Enterprise MonitorFree for 30 day evaluationMySQL VersionsCopyright Oracle Corporation 2012 69
  70. 70. Choosing the version5.1 – previous GA version5.5 – the latest GA version5.6 – development releaseChoosing the editionCommunity Edition (Community Server)Enterprise Editions (even MySQL Classic and MySQLStandard)Source or BinaryDownloadMySQL
  71. 71. • Reach out to the community– Irc on freenode– Forums.mysql.com• Oracle Support• CertificationsHow can I get help ?MySQL SupportCopyright Oracle Corporation 2012 71Copyright Oracle Corporation 2012 71http://www.flickr.com/photos/kalexanderson/6294075026/sizes/l/in/photostream/TM & © LUCASFILM LTD. 2012. ALL RIGHTS RESERVED
  72. 72. • 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 MySQLMySQL Support
  73. 73. Most secure, scalable MySQL Database, Online Backup,Development/Monitoring Tools, backed by Oracle PremierLifetime SupportOracle PremierSupportOracle ProductCertifications/IntegrationsMySQL EnterpriseHigh AvailabilityMySQL EnterpriseSecurityMySQL EnterpriseScalabilityMySQL EnterpriseBackupMySQL EnterpriseMonitor/Query AnalyzerMySQL WorkbenchMySQL Enterprise EditionCopyright Oracle Corporation 2012 73
  74. 74. mysql.comTCO calculator: http://www.mysql.com/tcosavings/White Papers: http://www.mysql.com/why-mysql/white-papers/Customer use cases and success stories:http://www.mysql.com/why-mysql/case-studies/dev.mysql.comDownloads: http://dev.mysql.com/downloads/Documentation: http://dev.mysql.com/doc/Forums: http://forums.mysql.com/PlanetMySQL: http://planet.mysql.comList of resources (books) : http://dev.mysql.com/resources/MySQL Resourceshttp://1.bp.blogspot.com/-FjX1nJexGeI/T7luugwIslI/AAAAAAAAAWU/PtCMZBfm1OA/s1600/internet.jpg
  75. 75. eDelivery.comDownload and evaluate all MySQL productsWiki:https://wikis.oracle.com/display/mysql/Homehttp://forge.mysql.com/wiki/Main_Page – Older Not used as much--50 things to know before migrating Oracle to MySQLIt is a little old but worth the readwww.xaprb.com/blog/2009/03/13/50-things-to-know-before-migrating-oracle-to-mysql/MySQL ResourcesCopyright Oracle Corporation 2012 75http://1.bp.blogspot.com/-FjX1nJexGeI/T7luugwIslI/AAAAAAAAAWU/PtCMZBfm1OA/s1600/internet.jpg
  76. 76. Copyright Oracle Corporation 2012 76keith.larson@oracle.com@larsonkeith“When I left you, I was but the learner, now I am the master.”http://www.flickr.com/photos/kalexanderson/7964300088/sizes/l/in/photostream/
  77. 77. <Insert Picture Here>Thanks for attending!Keith Larsonkeith.larson@oracle.comMySQL Community Managerhttp://sqlhjalp.com/pdf/MySQL_susecon_crashcourse_2012.pdf