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