MySQL: Know more about open Source Database


Published on

1 Like
  • Be the first to comment

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

No notes for slide

MySQL: Know more about open Source Database

  1. 1. know more about MySQL open source database<br />MySQL: Open Source Database<br />1<br />Salaria Software Services<br />
  2. 2. Storage engines<br />Schema<br />Normalization<br />Data types<br />Indexes<br />Know more about your SQL queries - Using EXPLAIN <br />Character set and Collation<br />Resources<br />2<br />Salaria Software Services<br />Outline<br />
  3. 3. Generics are inefficient<br />• If you have chosen MySQL, take advantage of its strengths<br />• Having an understanding of the database helps you develop better-performing applications<br /><ul><li>better to design a well-performing database-driven application from the start
  4. 4. than try to fix a slow one after the fact!</li></ul>3<br />Salaria Software Services<br />Why is it significant for a developerto know MySQL?<br />
  5. 5. Salaria Software Services<br />4<br />MySQL Pluggable Storage EngineArchitecture<br />MySQL supports several storage engines that act as handlers for different table types<br /><ul><li>No other database vendor offers this capability</li></li></ul><li>Dynamically add and remove storage engines.<br />Change the storage engine on a table with <br />“ALTER TABLE <table> ENGINE=InnoDB;”<br />5<br />Salaria Software Services<br />Storage Engines<br />
  6. 6. Concurrency: some have more granular locks than others<br /><ul><li>right locking can improve performance.</li></ul>• Storage: how the data is stored on disk<br /><ul><li>page size for tables, indexes, format used</li></ul>• Indexes: b-trees, hash<br />• Memory usage<br /><ul><li>caching strategy</li></ul>• Transactions:<br /><ul><li>not every application table needs transactions</li></ul>6<br />Salaria Software Services<br />What makes engines different?<br />
  7. 7. “As a developer, what do I need to know about storage engines, without being a MySQL expert?”<br />• Keep in mind the following questions:<br /><ul><li>What type of data will you be storing?
  8. 8. Is the data constantly changing?
  9. 9. Is the data mostly logs (INSERTs)?
  10. 10. requirements for reports?
  11. 11. need for transaction control?</li></ul>7<br />Salaria Software Services<br />So…<br />
  12. 12. Default MySQL engine<br />• high-speed Query and Insert capability<br /><ul><li>insert uses shared read lock
  13. 13. updates, deletes use table-level locking, slower</li></ul>• full-text indexing<br /><ul><li>Good for text search</li></ul>• Non-transactional, No foreign key support<br />• good choice for :<br /><ul><li>read-only or read-mostly application tables that don't require transactions
  14. 14. Web, data warehousing, logging, auditing</li></ul>8<br />Salaria Software Services<br />MyISAM Pluggable Storage engine<br />
  15. 15. Transaction-safe and ACID (atomicity, consistency, isolation, durability)compliant<br /><ul><li>crash recovery, foreign key constraints</li></ul>• good query performance, depending on indexes<br />• row-level locking, Multi Version Concurrency Control (MVCC)<br /><ul><li>allows fewer row locks by keeping data snapshots</li></ul>– Depending on the isolation level, no locking for SELECT<br />– high concurrency possible<br />• uses more disk space and memory than ISAM<br />• Good for Online transaction processing (OLTP)<br /><ul><li>Lots of users: eBay, Google, Yahoo!, Facebook, etc.</li></ul>9<br />Salaria Software Services<br />InnoDB Storage engine<br />
  16. 16. Entirely in-memory engine<br /><ul><li>stores all data in RAM for extremely fast access
  17. 17. Updated Data is not persisted</li></ul>– table loaded on restart <br />• Hash index used by default<br />• Good for<br /><ul><li>Summary and transient data
  18. 18. "lookup" tables,
  19. 19. calculated table counts,
  20. 20. for caching, temporary tables</li></ul>10<br />Salaria Software Services<br />Memory Engine<br />
  21. 21. Incredible insert speeds<br />• Great compression rates (zlib)?<br /><ul><li>Typically 6-8x smaller than MyISAM</li></ul>• No UPDATEs<br />• Ideal for storing and retrieving large amounts of historical data<br /><ul><li>audit data, log files,Web traffic records
  22. 22. Data that can never be updated</li></ul>11<br />Salaria Software Services<br />Archive engine<br />
  23. 23. Salaria Software Services<br />12<br />Pluggable storage engines offerFlexibility<br />You can use multiple storage engines in a single application<br /><ul><li>A storage engine for the same table on a slave can be different than that of the master</li></ul>• Choose storage engine that's best for your applications requirements<br /><ul><li>can greatly improve performance</li></li></ul><li>Creating a table with a specified engine<br /><ul><li>CREATE TABLE t1 (...)</li></ul> ENGINE=InnoDB;<br />• Changing existing tables<br /><ul><li>ALTER TABLE t1 ENGINE=MyISAM;</li></ul>• Finding all your available engines<br /><ul><li>SHOW STORAGE ENGINES;</li></ul>13<br />Salaria Software Services<br />Using different engines<br />
  24. 24. Basic foundation of performance<br />• Normalization<br />• Data Types<br /><ul><li>Smaller, smaller, smaller</li></ul>- Smaller tables use less disk, less memory, can give better performance<br />• Indexing<br /><ul><li>Speeds up retrieval</li></ul>14<br />Salaria Software Services<br />The schema<br />
  25. 25. Eliminate redundant data:<br /><ul><li>Don't store the same data in more than one table
  26. 26. Only store related data in a table
  27. 27. reduces database size and errors</li></ul>15<br />Salaria Software Services<br />Goal of Normalization<br />
  28. 28. updates are usually faster.<br /><ul><li>there's less data to change.</li></ul>• tables are usually smaller, use less memory, which can give better performance.<br />• better performance for distinct or group by queries<br />16<br />Salaria Software Services<br />Normalization<br />
  29. 29. However Normalized database causes joins for queries<br />• excessively normalized database:<br /><ul><li>queries take more time to complete, as data has to be retrieved from more tables.</li></ul>• Normalized better for writes OLTP<br />• De-normalized better for reads , reporting<br />• Real World Mixture:<br /><ul><li>normalized schema
  30. 30. Cache selected columns in memory table</li></ul>17<br />Salaria Software Services<br />taking normalization way too far<br />
  31. 31. Smaller => less disk => less memory => better performance<br />Use the smallest data type possible<br />• The smaller your data types, The more index (and data) can fit into a block of memory, the faster your queries will be.<br /><ul><li>Period.
  32. 32. Especially for indexed fields</li></ul>18<br />Salaria Software Services<br />Data Types: Smaller, smaller, smaller<br />
  33. 33. MySQL has 9 numeric data types<br /><ul><li>Compared to Oracle's 1</li></ul>• Integer:<br /><ul><li>TINYINT , SMALLINT, MEDIUMINT, INT, BIGINT
  34. 34. Require 8, 16, 24, 32, and 64 bits of space.</li></ul>• Use UNSIGNED when you don't need negative numbers – one more level of data integrity<br />• BIGINT is not needed for AUTO_INCREMENT<br /><ul><li>INT UNSIGNED stores 4.3 billion values!
  35. 35. Summation of values... yes, use BIGINT</li></ul>Floating Point: FLOAT, DOUBLE<br /><ul><li>Approximate calculations</li></ul>• Fixed Point: DECIMAL<br /><ul><li>Always use DECIMAL for monetary/currency fields, never use FLOAT or DOUBLE!</li></ul>• Other: BIT <br /><ul><li>Store 0,1 values</li></ul>19<br />Salaria Software Services<br />Choose your Numeric Data Type<br />
  36. 36. VARCHAR(n) variable length<br /><ul><li>uses only space it needs</li></ul>– Can save disk space = better performance<br /><ul><li>Use :</li></ul>– Max column length > avg<br />– when updates rare (updates fragment)<br />• CHAR(n) fixed length<br /><ul><li>Use:</li></ul>– short strings, Mostly same length, or changed frequently<br />20<br />Salaria Software Services<br />Character Data Types<br />
  37. 37. Always define columns as NOT NULL<br />– unless there is a good reason not to<br /><ul><li>Can save a byte per column
  38. 38. nullablecolumns make indexes, index statistics, and value comparisons more complicated.</li></ul>• Use the same data types for columns that will be compared in JOINs<br /><ul><li>Otherwise converted for comparison</li></ul>• Use BLOBs very sparingly<br /><ul><li>Use the filesystem for what it was intended</li></ul>21<br />Salaria Software Services<br />Appropriate Data Types<br />
  39. 39. “The more records you can fit into a single page of memory/disk, the faster your seeks and scans will be.”<br />• Use appropriate data types<br />• Keep primary keys small<br />• Use TEXT sparingly<br /><ul><li>Consider separate tables</li></ul>• Use BLOBs very sparingly<br /><ul><li>Use the filesystem for what it was intended</li></ul>22<br />Salaria Software Services<br />smaller, smaller, smaller<br />
  40. 40. Indexes Speed up Queries,<br /><ul><li>SELECT...WHERE name = 'carol'
  41. 41. only if there is good selectivity:</li></ul>– % of distinct values in a column<br />• But... each index will slow down INSERT, UPDATE, and DELETE operations<br />23<br />Salaria Software Services<br />Indexes<br />
  42. 42. Always have an index on join conditions<br />• Look to add indexes on columns used in WHERE and GROUP BY expressions<br />• PRIMARY KEY, UNIQUE , and Foreign key Constraint columns are automatically indexed.<br /><ul><li>other columns can be indexed (CREATE INDEX..)</li></ul>24<br />Salaria Software Services<br />Missing Indexes<br />
  43. 43. use the MySQL slow query log and use Explain<br />• Append EXPLAIN to your SELECT statement<br /><ul><li>shows how the MySQL optimizer has chosen to execute the query</li></ul>• You Want to make your queries access less data:<br /><ul><li>are queries accessing too many rows or columns?</li></ul>– select only the columns that you need<br />• Use to see where you should add indexes<br /><ul><li>Consider adding an index for slow queries or cause a lot of load.</li></ul>– ensures that missing indexes are picked up early in the development process<br />25<br />Salaria Software Services<br />Know how your Queries are executed by MySQL<br />
  44. 44. Find and fix problem SQL:<br />• how long a query took<br />• how the optimizer handled it<br /><ul><li>Drill downs, results of EXPLAIN statements</li></ul>• Historical and real-time analysis<br /><ul><li>query execution counts, run time</li></ul>“Its not just slow running queries that are a problem, Sometimes its SQL that executes a lot that kills your system”<br />26<br />Salaria Software Services<br />MySQLQuery Analyzer<br />
  45. 45. Salaria Software Services<br />27<br />Understanding EXPLAIN<br />Just append EXPLAIN to your SELECT statement<br />• Provides the execution plan chosen by the MySQL optimizer for a specific SELECT statement<br /><ul><li>Shows how the MySQL optimizer executes the query</li></ul>• Use to see where you should add indexes<br /><ul><li>ensures that missing indexes are picked up early in the development process</li></li></ul><li>A character set is a set of symbols and encodings. <br />A collation is a set of rules for comparing characters in a character set. <br />MySQL can do these things for you: <br /><ul><li>Store strings using a variety of character sets
  46. 46. Compare strings using a variety of collations
  47. 47. Mix strings with different character sets or collations in the same server, the same database, or even the same table
  48. 48. Allow specification of character set and collation at any level </li></ul> - Mysql > SET NAMES 'utf8'; <br /> - Mysql > SHOW CHARACTER SET<br />28<br />Salaria Software Services<br />Character set and Collation<br />
  49. 49. Two different character sets cannot have the same collation. <br />Each character set has one collation that is the default collation. For example, the default collation for latin1 is latin1_swedish_ci. The output for “SHOW CHARACTER SET” indicates which collation is the default for each displayed character set. <br />There is a convention for collation names: They start with the name of the character set with which they are associated, they usually include a language name, and they end with _ci (case insensitive), _cs (case sensitive), or _bin (binary). <br />29<br />Salaria Software Services<br />Collations have these general characteristics<br />
  50. 50. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><br /><?phpmysql_query('SET NAMES utf8'); ?><br />CREATE DATABASE mydatabase DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;<br />CREATE TABLE mytable (  mydata VARCHAR(128) NOT NULL) ENGINE=MyISAM DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;<br />30<br />Salaria Software Services<br />How to handle Unicode (UTF-8)<br />
  51. 51. MySQL Forge and the Forge Wiki<br /><br />Planet MySQL<br /><br />MySQLDevZone<br /><br />High Performance MySQL book<br />31<br />Salaria Software Services<br />Resources<br />
  52. 52. ?<br />Open Questions<br />32<br />Salaria Software Services<br />
  53. 53. Salaria Software Services<br />33<br />Thanks and Regards<br />Mahesh Salaria<br /><br />Salaria Software Services<br /><br />