17 Salaria Software Services taking normalization way too far
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.
– % of distinct values in a column • But... each index will slow down INSERT, UPDATE, and DELETE operations 23 Salaria Software Services Indexes
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..)
24 Salaria Software Services Missing Indexes
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 25 Salaria Software Services Know how your Queries are executed by MySQL
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” 26 Salaria Software Services MySQLQuery Analyzer
Salaria Software Services 27 Understanding EXPLAIN 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
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:
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 28 Salaria Software Services Character set and Collation
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). 29 Salaria Software Services Collations have these general characteristics
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <?phpmysql_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; 30 Salaria Software Services How to handle Unicode (UTF-8)
MySQL Forge and the Forge Wiki http://forge.mysql.com/ Planet MySQL http://planetmysql.org/ MySQLDevZone http://dev.mysql.com/ High Performance MySQL book 31 Salaria Software Services Resources