mysql

23
2012 Chris Henry @chrishnry MySQL Essentials Sunday, January 20, 13

Upload: chris-henry

Post on 15-Jan-2015

563 views

Category:

Documents


1 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Mysql

2012

Chris Henry@chrishnry

MySQL Essentials

Sunday, January 20, 13

Page 2: Mysql

hello.

Sunday, January 20, 13

Page 3: Mysql

MySQL @ Behance

Powers Be.net17 Dell R710s120GBXFS Filesystem

Percona 5.5

Sunday, January 20, 13

Page 4: Mysql

What Version of MySQL?

- Does it actually matter? Fuck yes.

mysql> SELECT VERSION()

Welcome to the MySQL monitor.Your MySQL connection id is 54248515Server version: 5.5.24-55-log Percona Server (GPL), Release rel26.0, Revision 256

Sunday, January 20, 13

Page 6: Mysql

MySQL Engines

• Pluggable architecture.• INNODB vs MyISAM• Transactions• Locking• Fulltext

• Should be InnoDB for most things

http://dev.mysql.com/doc/refman/5.5/en/storage-engines.html

Sunday, January 20, 13

Page 7: Mysql

Configuration

• table_open_cache• innodb_buffer_pool • max_connections• key_buffer_size

Sunday, January 20, 13

Page 8: Mysql

Slow Log

my.cnf

slow-query-log-file = /var/lib/mysqllogs/slow-loglong-query-time = 2

Sunday, January 20, 13

Page 9: Mysql

Tuning - Easy Wins

• skip-name-resolve• Linux Swappiness

Sunday, January 20, 13

Page 10: Mysql

Slow Log Output

# Query_time: 0.015029 Lock_time: 0.000044 Rows_sent: 481 Rows_examined: 5080 Rows_affected: 0 Rows_read: 481# Bytes_sent: 4888 Tmp_tables: 1 Tmp_disk_tables: 0 Tmp_table_sizes: 126992# Filesort: Yes Filesort_on_disk: No Merge_passes: 0# InnoDB_IO_r_ops: 0 InnoDB_IO_r_bytes: 0 InnoDB_IO_r_wait: 0.000000# InnoDB_rec_lock_wait: 0.000000 InnoDB_queue_wait: 0.000000SELECT DISTINCT proj.id FROM projects proj INNER JOIN project_summary ps ON proj.id=ps.proj_id WHERE proj.published = 1 AND proj.privacy2 = 1 ORDER BY ps.apps DESC LIMIT 0, 481;

Sunday, January 20, 13

Page 11: Mysql

EXPLAIN Output

*************************** 1. row *************************** id: 1 select_type: SIMPLE table: proj type: refpossible_keys: PRIMARY,published,privacy2, key: privacy2 key_len: 2 ref: const,const rows: 1 filtered: 100.00 Extra: Using index; Using temporary; Using filesort*************************** 2. row *************************** id: 1 select_type: SIMPLE table: ps type: refpossible_keys: proj_id key: proj_id key_len: 4 ref: be_net2.proj.id rows: 1 filtered: 100.00 Extra: Distinct2 rows in set, 1 warning (0.00 sec)

Sunday, January 20, 13

Page 12: Mysql

Table Design

• Data Types

• Relations

• Normal Form

Sunday, January 20, 13

Page 13: Mysql

Data Types

• Right tool for the job

• Numeric

• String

• Temporal

• Spatial

Sunday, January 20, 13

Page 14: Mysql

Integer Data Types

• How big?

• Signed or unsigned?

• What the hell does that number in parens mean?

• Integer types are great for storing things like type attributes.

Sunday, January 20, 13

Page 15: Mysql

Integer Table

CREATE TABLE IF NOT EXISTS `integers` ( `tiny` tinyint(4) NOT NULL, `tiny_zerofill` tinyint(10) unsigned zerofill NOT NULL, `small` smallint(6) NOT NULL, `medium` mediumint(9) NOT NULL, `int` int(11) NOT NULL, `int_unsigned` int(10) unsigned NOT NULL, `big` bigint(20) NOT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

Sunday, January 20, 13

Page 16: Mysql

Decimals

• Fixed Point

• DECIMAL (5,2)

Sunday, January 20, 13

Page 17: Mysql

Text Datatypes

• CHAR (0 to 255)

• VARCHAR (0 to 65,535) + length

Sunday, January 20, 13

Page 18: Mysql

Bigger Text Datatypes

• TINYTEXT• TEXT• MEDIUMTEXT • LONGTEXT

http://dev.mysql.com/doc/refman/5.5/en/string-type-overview.html

Sunday, January 20, 13

Page 19: Mysql

Character Sets + Collation

• A Character Set is a set of symbols and encodings.

• Collation is the set of rules for comparing characters. • utf8 / utf8_unicode_ci is best bet for most Western languages.

Sunday, January 20, 13

Page 20: Mysql

Temporal

• DATE• TIME• DATETIME• TIMESTAMP

http://dev.mysql.com/doc/refman/5.5/en/date-and-time-types.html

Sunday, January 20, 13

Page 21: Mysql

Table Design

• Primary Key

• Compound Keys

• Indexes

http://dev.mysql.com/doc/refman/5.5/en/date-and-time-types.html

Sunday, January 20, 13

Page 22: Mysql

Querying

• Sakila Database

• DVD Rental Store

http://dev.mysql.com/doc/sakila/en/index.html

Sunday, January 20, 13

Page 23: Mysql

That’s it!

Thank you all for coming!

Feedback is welcome!

Sunday, January 20, 13