SlideShare a Scribd company logo
1 of 73
Download to read offline
Ā©	
 Ā 2011	
 Ā ā€“	
 Ā 2013	
 Ā PERCONA	
 Ā 
Extensible Data Modeling with MySQL
Bill Karwin
Percona Live MySQL Conference & ExpoBill Karwin
Percona Live 2013
Ā©	
 Ā 2011	
 Ā ā€“	
 Ā 2013	
 Ā PERCONA	
 Ā 
	

ā€œI need to add a 
new columnā€” 
but I donā€™t want
ALTER TABLE to
lock the application
for a long time.ā€
Ā©	
 Ā 2011	
 Ā ā€“	
 Ā 2013	
 Ā PERCONA	
 Ā 
How MySQL Does ALTER TABLE
1.ā€Æ Lock the table.
2.ā€Æ Make a new, empty the table like the original.
3.ā€Æ Modify the columns of the new empty table.
4.ā€Æ Copy all rows of data from original to new tableā€¦
no matter how long it takes.
5.ā€Æ Swap the old and new tables.
6.ā€Æ Unlock the tables  drop the original.
Ā©	
 Ā 2011	
 Ā ā€“	
 Ā 2013	
 Ā PERCONA	
 Ā 
Extensibility
ā€¢ā€Æ How can we add new attributes without the pain of
schema changes?
ā€“ā€Æ Object-oriented modeling
ā€“ā€Æ Sparse columns
ā€¢ā€Æ Especially to support user-defined attributes at
runtime or after deployment:
ā€“ā€Æ Content management systems
ā€“ā€Æ E-commerce frameworks
ā€“ā€Æ Games
Ā©	
 Ā 2011	
 Ā ā€“	
 Ā 2013	
 Ā PERCONA	
 Ā 
Solutions
ā€¢ā€Æ ā€œExtra Columnsā€
ā€¢ā€Æ Entity-Attribute-Value
ā€¢ā€Æ Class Table Inheritance
ā€¢ā€Æ Serialized LOB  Inverted Indexes
ā€¢ā€Æ Online Schema Changes
ā€¢ā€Æ Non-Relational Databases
Ā©	
 Ā 2011	
 Ā ā€“	
 Ā 2013	
 Ā PERCONA	
 Ā 
EXTRA COLUMNS
Ā©	
 Ā 2011	
 Ā ā€“	
 Ā 2013	
 Ā PERCONA	
 Ā 
Table with Fixed Columns
CREATE TABLE Title (!
id int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,!
title text NOT NULL,!
imdb_index varchar(12) DEFAULT NULL,!
kind_id int(11) NOT NULL,!
production_year int(11) DEFAULT NULL,!
imdb_id int(11) DEFAULT NULL,!
phonetic_code varchar(5) DEFAULT NULL,!
episode_of_id int(11) DEFAULT NULL,!
season_nr int(11) DEFAULT NULL,!
episode_nr int(11) DEFAULT NULL,!
series_years varchar(49) DEFAULT NULL,!
title_crc32 int(10) unsigned DEFAULT NULL!
);!
Ā©	
 Ā 2011	
 Ā ā€“	
 Ā 2013	
 Ā PERCONA	
 Ā 
Table with Extra Columns
CREATE TABLE Title (!
id int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,!
title text NOT NULL,!
imdb_index varchar(12) DEFAULT NULL,!
kind_id int(11) NOT NULL,!
production_year int(11) DEFAULT NULL,!
imdb_id int(11) DEFAULT NULL,!
phonetic_code varchar(5) DEFAULT NULL,!
extra_data1 TEXT DEFAULT NULL,!
extra_data2 TEXT DEFAULT NULL, !
extra_data3 TEXT DEFAULT NULL,!
extra_data4 TEXT DEFAULT NULL,!
extra_data5 TEXT DEFAULT NULL,!
extra_data6 TEXT DEFAULT NULL,!
);!
use for whatever comes up
that we didnā€™t think of at the
start of the project
Ā©	
 Ā 2011	
 Ā ā€“	
 Ā 2013	
 Ā PERCONA	
 Ā 
Adding a New Attribute
UPDATE Title ā€Ø
SET extra_data3 = 'PG-13'ā€Ø
WHERE id = 207468;! remember which column
you used for each new
attribute!
Ā©	
 Ā 2011	
 Ā ā€“	
 Ā 2013	
 Ā PERCONA	
 Ā 
Pros and Cons
ā€¢ā€Æ Good solution:
ā€“ā€Æ No ALTER TABLE necessary to use a column for a new
attributeā€”only a project decision is needed.
ā€“ā€Æ Related to Single Table Inheritance (STI)
http://martinfowler.com/eaaCatalog/singleTableInheritance.html
Ā©	
 Ā 2011	
 Ā ā€“	
 Ā 2013	
 Ā PERCONA	
 Ā 
Pros and Cons
ā€¢ā€Æ Bad solution:
ā€“ā€Æ If you run out of extra columns, then youā€™re back to
ALTER TABLE.
ā€“ā€Æ Anyone can put any data in the columnsā€”you canā€™t
assume consistent usage on every row.
ā€“ā€Æ Columns lack descriptive names or the right data type.
Ā©	
 Ā 2011	
 Ā ā€“	
 Ā 2013	
 Ā PERCONA	
 Ā 
ENTITY-ATTRIBUTE-VALUE
Ā©	
 Ā 2011	
 Ā ā€“	
 Ā 2013	
 Ā PERCONA	
 Ā 
EAV
ā€¢ā€Æ Store each attribute in a row instead of a column.
CREATE TABLE Attributes (ā€Ø
entity INT NOT NULL,ā€Ø
attribute VARCHAR(20) NOT NULL,ā€Ø
value TEXT,ā€Ø
FOREIGN KEY (entity) ā€Ø
REFERENCES Title (id)ā€Ø
);!
Ā©	
 Ā 2011	
 Ā ā€“	
 Ā 2013	
 Ā PERCONA	
 Ā 
Example EAV Data
SELECT * FROM Attributes;!
+--------+-----------------+---------------------+!
| entity | attribute | value |!
+--------+-----------------+---------------------+!
| 207468 | title | Goldfinger |!
| 207468 | production_year | 1964 |!
| 207468 | rating | 7.8 |!
| 207468 | length | 110 min |!
+--------+-----------------+---------------------+!
!
!
!
Ā©	
 Ā 2011	
 Ā ā€“	
 Ā 2013	
 Ā PERCONA	
 Ā 
Adding a New Attribute
ā€¢ā€Æ Simply use INSERT with a new attribute name.
INSERT INTO Attributes (entity, attribute, value)ā€Ø
VALUES (207468, 'budget', '$3,000,000');!
Ā©	
 Ā 2011	
 Ā ā€“	
 Ā 2013	
 Ā PERCONA	
 Ā 
Query EAV as a Pivot
SELECT a.entity AS id,!
a.value AS title,!
y.value AS production_year,!
r.value AS rating,ā€Ø
b.value AS budget!
FROM Attributes AS a!
JOIN Attributes AS y USING (entity)!
JOIN Attributes AS r USING (entity)!
JOIN Attributes AS b USING (entity)!
WHERE a.attribute = 'title'!
AND y.attribute = 'production_year'!
AND r.attribute = 'rating'ā€Ø
AND b.attribute = 'budget';!
+--------+------------+-----------------+--------+------------+!
| id | title | production_year | rating | budget |!
+--------+------------+-----------------+--------+------------+!
| 207468 | Goldfinger | 1964 | 7.8 | $3,000,000 |!
+--------+------------+-----------------+--------+------------+!
another join required for
each additional attribute
Ā©	
 Ā 2011	
 Ā ā€“	
 Ā 2013	
 Ā PERCONA	
 Ā 
Ā©	
 Ā 2011	
 Ā ā€“	
 Ā 2013	
 Ā PERCONA	
 Ā 
Sounds Simple Enough, Butā€¦
ā€¢ā€Æ NOT NULL doesnā€™t work
ā€¢ā€Æ FOREIGN KEY doesnā€™t work
ā€¢ā€Æ UNIQUE KEY doesnā€™t work
ā€¢ā€Æ Data types donā€™t work
ā€¢ā€Æ Searches donā€™t scale
ā€¢ā€Æ Indexes and storage are inefficient
Ā©	
 Ā 2011	
 Ā ā€“	
 Ā 2013	
 Ā PERCONA	
 Ā 
Constraints Donā€™t Work
CREATE TABLE Attributes (ā€Ø
entity INT NOT NULL,ā€Ø
attribute VARCHAR(20) NOT NULL,ā€Ø
value TEXT NOT NULL,ā€Ø
FOREIGN KEY (entity) ā€Ø
REFERENCES Title (id)ā€Ø
FOREIGN KEY (value) ā€Ø
REFERENCES Ratings (rating)ā€Ø
);!
constraints apply to all
rows, not just rows for a
specific attribute type
Ā©	
 Ā 2011	
 Ā ā€“	
 Ā 2013	
 Ā PERCONA	
 Ā 
Data Types Donā€™t Work
INSERT INTO Attributes (entity, attribute, value)ā€Ø
VALUES (207468, 'budget', 'banana');!
database cannot
prevent application
storing nonsensical
data
Ā©	
 Ā 2011	
 Ā ā€“	
 Ā 2013	
 Ā PERCONA	
 Ā 
Add Typed Value Columns?
CREATE TABLE Attributes (ā€Ø
entity INT NOT NULL,ā€Ø
attribute VARCHAR(20) NOT NULL,ā€Ø
intvalue BIGINT,ā€Ø
floatvalue FLOAT,ā€Ø
textvalue TEXT,ā€Ø
datevalue DATE,ā€Ø
datetimevalue DATETIME, ā€Ø
FOREIGN KEY (entity) ā€Ø
REFERENCES Title (id)ā€Ø
);!
now my application needs to
know which data type column
to use for each attribute when
inserting and querying
Ā©	
 Ā 2011	
 Ā ā€“	
 Ā 2013	
 Ā PERCONA	
 Ā 
Searches Donā€™t Scale
ā€¢ā€Æ You must hard-code each attribute name,
ā€“ā€Æ One JOIN per attribute!
ā€¢ā€Æ Alternatively, you can query all attributes, but the
result is one attribute per row:
SELECT attribute, value ā€Ø
FROM Attributes ā€Ø
WHERE entity = 207468;!
ā€“ā€Æ ā€¦and sort it out in your application code.
Ā©	
 Ā 2011	
 Ā ā€“	
 Ā 2013	
 Ā PERCONA	
 Ā 
Indexes and Storage Are Inefficient
ā€¢ā€Æ Many rows, with few distinct attribute names.
ā€“ā€Æ Poor index cardinality.
ā€¢ā€Æ The entity and attribute columns use extra
space for every attribute of every ā€œrow.ā€
ā€“ā€Æ In a conventional table, the entity is the primary key, so
itā€™s stored only once per row.
ā€“ā€Æ The attribute name is in the table definition, so itā€™s
stored only once per table.
Ā©	
 Ā 2011	
 Ā ā€“	
 Ā 2013	
 Ā PERCONA	
 Ā 
Pros and Cons
ā€¢ā€Æ Good solution:
ā€“ā€Æ No ALTER TABLE needed againā€”ever!
ā€“ā€Æ Supports ultimate flexibility, potentially any ā€œrowā€ can
have its own distinct set of attributes.
Ā©	
 Ā 2011	
 Ā ā€“	
 Ā 2013	
 Ā PERCONA	
 Ā 
Pros and Cons
ā€¢ā€Æ Bad solution:
ā€“ā€Æ SQL operations become more complex.
ā€“ā€Æ Lots of application code required to reinvent features
that an RDBMS already provides.
ā€“ā€Æ Doesnā€™t scale wellā€”pivots required.
Ā©	
 Ā 2011	
 Ā ā€“	
 Ā 2013	
 Ā PERCONA	
 Ā 
CLASS TABLE INHERITANCE
Ā©	
 Ā 2011	
 Ā ā€“	
 Ā 2013	
 Ā PERCONA	
 Ā 
Subtypes
ā€¢ā€Æ Titles includes:
ā€“ā€Æ Films
ā€“ā€Æ TV shows
ā€“ā€Æ TV episodes
ā€“ā€Æ Video games
ā€¢ā€Æ Some attributes apply to all, other attributes apply
to one subtype or the other.
Ā©	
 Ā 2011	
 Ā ā€“	
 Ā 2013	
 Ā PERCONA	
 Ā 
Title Table
CREATE TABLE Title (!
id int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,!
title text NOT NULL,!
imdb_index varchar(12) DEFAULT NULL,!
kind_id int(11) NOT NULL,!
production_year int(11) DEFAULT NULL,!
imdb_id int(11) DEFAULT NULL,!
phonetic_code varchar(5) DEFAULT NULL,!
episode_of_id int(11) DEFAULT NULL,!
season_nr int(11) DEFAULT NULL,!
episode_nr int(11) DEFAULT NULL,!
series_years varchar(49) DEFAULT NULL,!
title_crc32 int(10) unsigned DEFAULT NULL!
);!
only for tv shows
Ā©	
 Ā 2011	
 Ā ā€“	
 Ā 2013	
 Ā PERCONA	
 Ā 
Title Table with Subtype Tables
CREATE TABLE Title (!
id int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,!
title text NOT NULL,!
imdb_index varchar(12) DEFAULT NULL,!
kind_id int(11) NOT NULL,!
production_year int(11) DEFAULT NULL,!
imdb_id int(11) DEFAULT NULL,!
phonetic_code varchar(5) DEFAULT NULL,!
title_crc32 int(10) unsigned DEFAULT NULL,!
PRIMARY KEY (id)!
);!
!
CREATE TABLE Film (!
id int(11) NOT NULL PRIMARY KEY,ā€Ø
aspect_ratio varchar(20),!
FOREIGN KEY (id) REFERENCES Title(id)!
);!
!
CREATE TABLE TVShow (!
id int(11) NOT NULL PRIMARY KEY,!
episode_of_id int(11) DEFAULT NULL,!
season_nr int(11) DEFAULT NULL,!
episode_nr int(11) DEFAULT NULL,!
series_years varchar(49) DEFAULT NULL,!
FOREIGN KEY (id) REFERENCES Title(id)!
);!
Title	
 Ā 
Film	
 Ā  TVShow	
 Ā 
1:1 1:1
Ā©	
 Ā 2011	
 Ā ā€“	
 Ā 2013	
 Ā PERCONA	
 Ā 
Adding a New Subtype
ā€¢ā€Æ Create a new tableā€”without locking existing tables.
CREATE TABLE VideoGames (ā€Ø
id int(11) NOT NULL PRIMARY KEY,ā€Ø
platforms varchar(100) NOT NULL,ā€Ø
FOREIGN KEY (id) ā€Ø
REFERENCES Title(id)ā€Ø
);!
Ā©	
 Ā 2011	
 Ā ā€“	
 Ā 2013	
 Ā PERCONA	
 Ā 
Pros and Cons
ā€¢ā€Æ Good solution:
ā€“ā€Æ Best to support a finite set of subtypes, which are likely
unchanging after creation.
ā€“ā€Æ Data types and constraints work normally.
ā€“ā€Æ Easy to create or drop subtype tables.
ā€“ā€Æ Easy to query attributes common to all subtypes.
ā€“ā€Æ Subtype tables are shorter, indexes are smaller.
Ā©	
 Ā 2011	
 Ā ā€“	
 Ā 2013	
 Ā PERCONA	
 Ā 
Pros and Cons
ā€¢ā€Æ Bad solution:
ā€“ā€Æ Adding one entry takes two INSERT statements.
ā€“ā€Æ Querying attributes of subtypes requires a join.
ā€“ā€Æ Querying all types with subtype attributes requires
multiple joins (as many as subtypes).
ā€“ā€Æ Adding a common attribute locks a large table.
ā€“ā€Æ Adding an attribute to a populated subtype locks a
smaller table.
Ā©	
 Ā 2011	
 Ā ā€“	
 Ā 2013	
 Ā PERCONA	
 Ā 
SERIALIZED LOB
Ā©	
 Ā 2011	
 Ā ā€“	
 Ā 2013	
 Ā PERCONA	
 Ā 
What is Serializing?
ā€¢ā€Æ Objects in your applications can be represented in
serialized formā€”i.e., convert the object to a scalar
string that you can save and load back as an object.
ā€“ā€Æ Java objects implementing Serializable and
processed with writeObject()!
ā€“ā€Æ PHP variables processed with serialize()!
ā€“ā€Æ Python objects processed with pickle.dump()!
ā€“ā€Æ Data encoded with XML, JSON, YAML, etc.
Ā©	
 Ā 2011	
 Ā ā€“	
 Ā 2013	
 Ā PERCONA	
 Ā 
What Is a LOB?
ā€¢ā€Æ The BLOB or TEXT datatypes can store long
sequences of bytes or characters, such as a string.
ā€¢ā€Æ You can store the string representing your object
into a single BLOB or TEXT column.
ā€“ā€Æ You donā€™t need to define SQL columns for each field of
your object.
Ā©	
 Ā 2011	
 Ā ā€“	
 Ā 2013	
 Ā PERCONA	
 Ā 
Title Table with Serialized LOB
CREATE TABLE Title (ā€Ø
id int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,ā€Ø
title text NOT NULL,ā€Ø
imdb_index varchar(12) DEFAULT NULL,ā€Ø
kind_id int(11) NOT NULL,ā€Ø
production_year int(11) DEFAULT NULL,ā€Ø
imdb_id int(11) DEFAULT NULL,ā€Ø
phonetic_code varchar(5) DEFAULT NULL,ā€Ø
title_crc32 int(10) unsigned DEFAULT NULLā€Ø
extra_info TEXT ā€Ø
);!
holds everything else,
plus anything we
didnā€™t think of
Ā©	
 Ā 2011	
 Ā ā€“	
 Ā 2013	
 Ā PERCONA	
 Ā 
Adding a New Attribute
UPDATE Titleā€Ø
SET extra_info = ā€Ø
'{ā€Ø
episode_of_id: 1291895, ā€Ø
season_nr: 5, ā€Ø
episode_nr: 6ā€Ø
}'ā€Ø
WHERE id = 1292057;!
JSON example
Ā©	
 Ā 2011	
 Ā ā€“	
 Ā 2013	
 Ā PERCONA	
 Ā 
Using XML in MySQL
ā€¢ā€Æ MySQL has limited support for XML.
SELECT id, title, ā€Ø
ExtractValue(extra_info, '/episode_nr') ā€Ø
AS episode_nr ā€Ø
FROM Titleā€Ø
WHERE ExtractValue(extra_info, ā€Ø
'/episode_of_id') = 1292057;!
ā€¢ā€Æ Forces table-scans, not possible to use indexes.
http://dev.mysql.com/doc/refman/5.6/en/xml-functions.html
Ā©	
 Ā 2011	
 Ā ā€“	
 Ā 2013	
 Ā PERCONA	
 Ā 
Dynamic Columns in MariaDB
CREATE TABLE Title (ā€Ø
id int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,ā€Ø
title text NOT NULL,ā€Ø
...ā€Ø
extra_info BLOB ā€Ø
);!
INSERT INTO Title (title, extra_info) ā€Ø
VALUES ('Trials and Tribble-ations', ā€Ø
COLUMN_CREATE('episode_of_id', '1291895', ā€Ø
'episode_nr', '5',ā€Ø
'season_nr', '6'));!
https://kb.askmonty.org/en/dynamic-columns/
Ā©	
 Ā 2011	
 Ā ā€“	
 Ā 2013	
 Ā PERCONA	
 Ā 
Pros and Cons
ā€¢ā€Æ Good solution:
ā€“ā€Æ Store any object and add new custom fields at any time.
ā€“ā€Æ No need to do ALTER TABLE to add custom fields.
Ā©	
 Ā 2011	
 Ā ā€“	
 Ā 2013	
 Ā PERCONA	
 Ā 
Pros and Cons
ā€¢ā€Æ Bad solution:
ā€“ā€Æ Not indexable.
ā€“ā€Æ Must return the whole object, not an individual field.
ā€“ā€Æ Must write the whole object to update a single field.
ā€“ā€Æ Hard to use a custom field in a WHERE clause, GROUP BY
or ORDER BY.
ā€“ā€Æ No support in the database for data types or
constraints, e.g. NOT NULL, UNIQUE, FOREIGN KEY.
Ā©	
 Ā 2011	
 Ā ā€“	
 Ā 2013	
 Ā PERCONA	
 Ā 
INVERTED INDEXES
Ā©	
 Ā 2011	
 Ā ā€“	
 Ā 2013	
 Ā PERCONA	
 Ā 
Use This with Serialized LOB
ā€¢ā€Æ Helps to mitigate some of the weaknesses.
Ā©	
 Ā 2011	
 Ā ā€“	
 Ā 2013	
 Ā PERCONA	
 Ā 
How This Works
ā€¢ā€Æ Create a new table for each field of the LOB that
you want to address individually:
CREATE TABLE Title_EpisodeOf (ā€Ø
episode_of_id INT NOT NULL,ā€Ø
id INT NOT NULL,ā€Ø
PRIMARY KEY (episode_of_id, id),ā€Ø
FOREIGN KEY (id)ā€Ø
REFERENCES Title (id)ā€Ø
);!
hereā€™s where you get
the index support
Ā©	
 Ā 2011	
 Ā ā€“	
 Ā 2013	
 Ā PERCONA	
 Ā 
How This Works
ā€¢ā€Æ For each LOB containing an ā€œepisode_of_idā€ field,
insert a row to the attribute table with its value.
INSERT INTO Title_EpisodeOf ā€Ø
VALUES (1291895, 1292057);!
ā€¢ā€Æ If another title doesnā€™t have this field,
then you donā€™t create a referencing row.
Ā©	
 Ā 2011	
 Ā ā€“	
 Ā 2013	
 Ā PERCONA	
 Ā 
Query for Recent Users
SELECT u.*ā€Ø
FROM Title_EpisodeOf AS eā€Ø
JOIN Title AS t USING (id)ā€Ø
WHERE e.episode_of_id = '1291895';!
This is a primary key lookup.
It matches only titles that have such a field,
and whose value matches the condition
Ā©	
 Ā 2011	
 Ā ā€“	
 Ā 2013	
 Ā PERCONA	
 Ā 
Pros and Cons
ā€¢ā€Æ Good solution:
ā€“ā€Æ Preserves the advantage of Serialized LOB.
ā€“ā€Æ Adds support for SQL data types, and UNIQUE and
FOREIGN KEY constraints.
ā€“ā€Æ You can index any custom fieldā€”without locking the
master table.
Ā©	
 Ā 2011	
 Ā ā€“	
 Ā 2013	
 Ā PERCONA	
 Ā 
Pros and Cons
ā€¢ā€Æ Bad solution:
ā€“ā€Æ Redundant storage.
ā€“ā€Æ Itā€™s up to you to keep attribute tables in sync manually
(or with triggers).
ā€“ā€Æ Requires JOIN to fetch the master row.
ā€“ā€Æ You must plan which columns you want to be indexed
(but this is true of conventional columns too).
ā€“ā€Æ Still no support for NOT NULL constraint.
Ā©	
 Ā 2011	
 Ā ā€“	
 Ā 2013	
 Ā PERCONA	
 Ā 
ONLINE SCHEMA CHANGES
Ā©	
 Ā 2011	
 Ā ā€“	
 Ā 2013	
 Ā PERCONA	
 Ā 
pt-online-schema-change
ā€¢ā€Æ Performs online, non-blocking ALTER TABLE.
ā€“ā€Æ Captures concurrent updates to a table while
restructuring.
ā€“ā€Æ Some risks and caveats exist; please read the manual
and test carefully.
ā€¢ā€Æ Free toolā€”part of Percona Toolkit.
ā€“ā€Æ http://www.percona.com/doc/percona-toolkit/pt-online-schema-
change.html
Ā©	
 Ā 2011	
 Ā ā€“	
 Ā 2013	
 Ā PERCONA	
 Ā 
How MySQL Does ALTER TABLE
1.ā€Æ Lock the table.
2.ā€Æ Make a new, empty the table like the original.
3.ā€Æ Modify the columns of the new empty table.
4.ā€Æ Copy all rows of data from original to new table.
5.ā€Æ Swap the old and new tables.
6.ā€Æ Unlock the tables  drop the original.
Ā©	
 Ā 2011	
 Ā ā€“	
 Ā 2013	
 Ā PERCONA	
 Ā 
How pt-osc Does ALTER TABLE
Lock the table.
1.ā€Æ Make a new, empty the table like the original.
2.ā€Æ Modify the columns of the new empty table.
3.ā€Æ Copy all rows of data from original to new table.
a.ā€Æ Iterate over the table in chunks, in primary key order.
b.ā€Æ Use triggers to capture ongoing changes in the
original, and apply them to the new table.
4.ā€Æ Swap the tables, then drop the original.
Unlock the tables.
Ā©	
 Ā 2011	
 Ā ā€“	
 Ā 2013	
 Ā PERCONA	
 Ā 
Visualize This (1)
cast_info after
trigger
cast_info new
Ā©	
 Ā 2011	
 Ā ā€“	
 Ā 2013	
 Ā PERCONA	
 Ā 
Visualize This (2)
cast_info after
trigger
cast_info new
Ā©	
 Ā 2011	
 Ā ā€“	
 Ā 2013	
 Ā PERCONA	
 Ā 
Visualize This (3)
cast_info after
trigger
cast_info new
Ā©	
 Ā 2011	
 Ā ā€“	
 Ā 2013	
 Ā PERCONA	
 Ā 
Visualize This (4)
cast_info after
trigger
cast_info new
Ā©	
 Ā 2011	
 Ā ā€“	
 Ā 2013	
 Ā PERCONA	
 Ā 
Visualize This (5)
cast_info after
trigger
cast_info new
Ā©	
 Ā 2011	
 Ā ā€“	
 Ā 2013	
 Ā PERCONA	
 Ā 
Visualize This (6)
cast_info cast_info old
DROP
Ā©	
 Ā 2011	
 Ā ā€“	
 Ā 2013	
 Ā PERCONA	
 Ā 
Adding a New Attribute
ā€¢ā€Æ Design the ALTER TABLE statement, but donā€™t
execute it yet.
mysql ALTER TABLE cast_info ā€Ø
ADD COLUMN source INT NOT NULL;!
ā€¢ā€Æ Equivalent pt-online-schema-change command:
$ pt-online-schema-change ā€Ø
h=localhost,D=imdb,t=cast_info ā€Ø
--alter ADD COLUMN source INT NOT NULL!
Ā©	
 Ā 2011	
 Ā ā€“	
 Ā 2013	
 Ā PERCONA	
 Ā 
Execute
$ pt-online-schema-change h=localhost,D=imdb,t=cast_info ā€Ø
--alter ADD COLUMN source INT NOT NULL --execute!
!
Altering `imdb`.`cast_info`...!
Creating new table...!
Created new table imdb._cast_info_new OK.!
Altering new table...!
Altered `imdb`.`_cast_info_new` OK.!
Creating triggers...!
Created triggers OK.!
Copying approximately 22545051 rows...!
Copying `imdb`.`cast_info`: 10% 04:05 remain!
Copying `imdb`.`cast_info`: 19% 04:07 remain!
Copying `imdb`.`cast_info`: 28% 03:44 remain!
Copying `imdb`.`cast_info`: 37% 03:16 remain!
Copying `imdb`.`cast_info`: 47% 02:47 remain!
Copying `imdb`.`cast_info`: 56% 02:18 remain!
Copying `imdb`.`cast_info`: 64% 01:53 remain!
Copying `imdb`.`cast_info`: 73% 01:28 remain!
Copying `imdb`.`cast_info`: 82% 00:55 remain!
Copying `imdb`.`cast_info`: 91% 00:26 remain!
Copied rows OK.!
Swapping tables...!
Swapped original and new tables OK.!
Dropping old table...!
Dropped old table `imdb`.`_cast_info_old` OK.!
Dropping triggers...!
Dropped triggers OK.!
Successfully altered `imdb`.`cast_info`.!
!
!
Ā©	
 Ā 2011	
 Ā ā€“	
 Ā 2013	
 Ā PERCONA	
 Ā 
Self-Adjusting
ā€¢ā€Æ Copies rows in chunks which the tool sizes
dynamically.
ā€¢ā€Æ The tool throttles back if it increases load too much
or if it causes any replication slaves to lag.
ā€¢ā€Æ The tool tries to set its lock timeouts to let
applications be more likely to succeed.
Ā©	
 Ā 2011	
 Ā ā€“	
 Ā 2013	
 Ā PERCONA	
 Ā 
Why Shouldnā€™t I Use This?
ā€¢ā€Æ Is your table small enough that ALTER is already
quick enough?
ā€¢ā€Æ Is your change already very quick, for example
DROP KEY in InnoDB?
ā€¢ā€Æ Will pt-online-schema-change take too long or
increase the load too much?
Ā©	
 Ā 2011	
 Ā ā€“	
 Ā 2013	
 Ā PERCONA	
 Ā 
Pros and Cons
ā€¢ā€Æ Good solution:
ā€“ā€Æ ALTER TABLE to add conventional columns without
the pain of locking.
Ā©	
 Ā 2011	
 Ā ā€“	
 Ā 2013	
 Ā PERCONA	
 Ā 
Pros and Cons
ā€¢ā€Æ Bad solution:
ā€“ā€Æ Can take up to 4Ɨ more time than ALTER TABLE.
ā€“ā€Æ Table must have a PRIMARY key.
ā€“ā€Æ Table must not have triggers.
ā€“ā€Æ No need if your table is small and ALTER TABLE
already runs quickly enough.
ā€“ā€Æ No need for some ALTER TABLE operations that donā€™t
restructure the table (e.g. dropping indexes, adding
comments).
Ā©	
 Ā 2011	
 Ā ā€“	
 Ā 2013	
 Ā PERCONA	
 Ā 
NON-RELATIONAL DATABASES
Ā©	
 Ā 2011	
 Ā ā€“	
 Ā 2013	
 Ā PERCONA	
 Ā 
No Rules to Break
ā€¢ā€Æ To be relational, a table must have a fixed set of
columns on every row.
ā€¢ā€Æ No such rule exists in a non-relational model; you
can store a distinct set of fields per record.
ā€¢ā€Æ No schema makes NoSQL more flexible.
Ā©	
 Ā 2011	
 Ā ā€“	
 Ā 2013	
 Ā PERCONA	
 Ā 
Adding a New Attribute
ā€¢ā€Æ Document-oriented databases are designed to
support defining distinct attributes per document.
ā€¢ā€Æ But you lose advantages of relational databases:
ā€“ā€Æ Data types
ā€“ā€Æ Constraints
ā€“ā€Æ Uniform structure of records
Ā©	
 Ā 2011	
 Ā ā€“	
 Ā 2013	
 Ā PERCONA	
 Ā 
SUMMARY
Ā©	
 Ā 2011	
 Ā ā€“	
 Ā 2013	
 Ā PERCONA	
 Ā 
Summary
Solu%on	
 Ā  Lock-Ā­ā€free	
 Ā  Flexible	
 Ā  Select	
 Ā  Filter	
 Ā  Indexed	
 Ā  Data	
 Ā Types	
 Ā  Constraints	
 Ā 
Extra	
 Ā 
Columns	
 Ā 
no*	
 Ā  no	
 Ā  yes	
 Ā  yes	
 Ā  yes*	
 Ā  no	
 Ā  no	
 Ā 
EAV	
 Ā  yes	
 Ā  yes	
 Ā  yes*	
 Ā  yes	
 Ā  yes*	
 Ā  no*	
 Ā  no	
 Ā 
CTI	
 Ā  no*	
 Ā  no	
 Ā  yes	
 Ā  yes	
 Ā  yes	
 Ā  yes	
 Ā  yes	
 Ā 
LOB	
 Ā  yes	
 Ā  yes	
 Ā  no	
 Ā  no	
 Ā  no	
 Ā  no	
 Ā  no	
 Ā 
Inverted	
 Ā 
Index	
 Ā 
yes	
 Ā  yes	
 Ā  yes	
 Ā  yes	
 Ā  yes	
 Ā  yes	
 Ā  yes	
 Ā 
OSC	
 Ā  yes	
 Ā  no	
 Ā  yes	
 Ā  yes	
 Ā  yes	
 Ā  yes	
 Ā  yes	
 Ā 
NoSQL	
 Ā  yes	
 Ā  yes	
 Ā  yes	
 Ā  yes	
 Ā  yes	
 Ā  no*	
 Ā  no	
 Ā 
* conditions or exceptions apply.
Ā©	
 Ā 2011	
 Ā ā€“	
 Ā 2013	
 Ā PERCONA	
 Ā 
Senior Industry Experts	

In-Person and Online Classes	

Custom Onsite Training	

http://percona.com/training
Ā©	
 Ā 2011	
 Ā ā€“	
 Ā 2013	
 Ā PERCONA	
 Ā 
ā€œRate This Sessionā€	

http://www.percona.com/live/mysql-conference-2013/
sessions/extensible-data-modeling-mysql
Ā©	
 Ā 2011	
 Ā ā€“	
 Ā 2013	
 Ā PERCONA	
 Ā 
SQL Antipatterns: 
Avoiding the Pitfalls of
Database Programming	

by Bill Karwin	

	

Available in print, epub, mobi, pdf. 
Delivery options for Kindle or Dropbox.	

	

http://pragprog.com/book/bksqla/
Extensible Data Modeling

More Related Content

What's hot

MySQL Performance Tuning. Part 1: MySQL Configuration (includes MySQL 5.7)
MySQL Performance Tuning. Part 1: MySQL Configuration (includes MySQL 5.7)MySQL Performance Tuning. Part 1: MySQL Configuration (includes MySQL 5.7)
MySQL Performance Tuning. Part 1: MySQL Configuration (includes MySQL 5.7)Aurimas Mikalauskas
Ā 
Introduction to MongoDB
Introduction to MongoDBIntroduction to MongoDB
Introduction to MongoDBMike Dirolf
Ā 
Oracle Forms : Multiple Forms
Oracle Forms : Multiple FormsOracle Forms : Multiple Forms
Oracle Forms : Multiple FormsSekhar Byna
Ā 
PostgreSQL Database Slides
PostgreSQL Database SlidesPostgreSQL Database Slides
PostgreSQL Database Slidesmetsarin
Ā 
Trees and Hierarchies in SQL
Trees and Hierarchies in SQLTrees and Hierarchies in SQL
Trees and Hierarchies in SQLEduard Hildebrandt
Ā 
Railway Oriented Programming
Railway Oriented ProgrammingRailway Oriented Programming
Railway Oriented ProgrammingScott Wlaschin
Ā 
PostgreSQL Tutorial for Beginners | Edureka
PostgreSQL Tutorial for Beginners | EdurekaPostgreSQL Tutorial for Beginners | Edureka
PostgreSQL Tutorial for Beginners | EdurekaEdureka!
Ā 
MySQL Performance Tuning: Top 10 Tips
MySQL Performance Tuning: Top 10 TipsMySQL Performance Tuning: Top 10 Tips
MySQL Performance Tuning: Top 10 TipsOSSCube
Ā 
MySQL 8.0 EXPLAIN ANALYZE
MySQL 8.0 EXPLAIN ANALYZEMySQL 8.0 EXPLAIN ANALYZE
MySQL 8.0 EXPLAIN ANALYZENorvald Ryeng
Ā 
From cache to in-memory data grid. Introduction to Hazelcast.
From cache to in-memory data grid. Introduction to Hazelcast.From cache to in-memory data grid. Introduction to Hazelcast.
From cache to in-memory data grid. Introduction to Hazelcast.Taras Matyashovsky
Ā 
Introduction to SQL Antipatterns
Introduction to SQL AntipatternsIntroduction to SQL Antipatterns
Introduction to SQL AntipatternsKrishnakumar S
Ā 
Planning your Next-Gen Change Data Capture (CDC) Architecture in 2019 - Strea...
Planning your Next-Gen Change Data Capture (CDC) Architecture in 2019 - Strea...Planning your Next-Gen Change Data Capture (CDC) Architecture in 2019 - Strea...
Planning your Next-Gen Change Data Capture (CDC) Architecture in 2019 - Strea...Impetus Technologies
Ā 
Reporting with Oracle Application Express (APEX)
Reporting with Oracle Application Express (APEX)Reporting with Oracle Application Express (APEX)
Reporting with Oracle Application Express (APEX)Dimitri Gielis
Ā 
From Java 11 to 17 and beyond.pdf
From Java 11 to 17 and beyond.pdfFrom Java 11 to 17 and beyond.pdf
From Java 11 to 17 and beyond.pdfJosƩ Paumard
Ā 
Java EE Introduction
Java EE IntroductionJava EE Introduction
Java EE Introductionejlp12
Ā 
More mastering the art of indexing
More mastering the art of indexingMore mastering the art of indexing
More mastering the art of indexingYoshinori Matsunobu
Ā 

What's hot (20)

MySQL Performance Tuning. Part 1: MySQL Configuration (includes MySQL 5.7)
MySQL Performance Tuning. Part 1: MySQL Configuration (includes MySQL 5.7)MySQL Performance Tuning. Part 1: MySQL Configuration (includes MySQL 5.7)
MySQL Performance Tuning. Part 1: MySQL Configuration (includes MySQL 5.7)
Ā 
Introduction to MongoDB
Introduction to MongoDBIntroduction to MongoDB
Introduction to MongoDB
Ā 
Oracle Forms : Multiple Forms
Oracle Forms : Multiple FormsOracle Forms : Multiple Forms
Oracle Forms : Multiple Forms
Ā 
PostgreSQL Database Slides
PostgreSQL Database SlidesPostgreSQL Database Slides
PostgreSQL Database Slides
Ā 
Trees and Hierarchies in SQL
Trees and Hierarchies in SQLTrees and Hierarchies in SQL
Trees and Hierarchies in SQL
Ā 
Sql query patterns, optimized
Sql query patterns, optimizedSql query patterns, optimized
Sql query patterns, optimized
Ā 
Railway Oriented Programming
Railway Oriented ProgrammingRailway Oriented Programming
Railway Oriented Programming
Ā 
Models for hierarchical data
Models for hierarchical dataModels for hierarchical data
Models for hierarchical data
Ā 
PostgreSQL Tutorial for Beginners | Edureka
PostgreSQL Tutorial for Beginners | EdurekaPostgreSQL Tutorial for Beginners | Edureka
PostgreSQL Tutorial for Beginners | Edureka
Ā 
MySQL Performance Tuning: Top 10 Tips
MySQL Performance Tuning: Top 10 TipsMySQL Performance Tuning: Top 10 Tips
MySQL Performance Tuning: Top 10 Tips
Ā 
Angular
AngularAngular
Angular
Ā 
MySQL 8.0 EXPLAIN ANALYZE
MySQL 8.0 EXPLAIN ANALYZEMySQL 8.0 EXPLAIN ANALYZE
MySQL 8.0 EXPLAIN ANALYZE
Ā 
From cache to in-memory data grid. Introduction to Hazelcast.
From cache to in-memory data grid. Introduction to Hazelcast.From cache to in-memory data grid. Introduction to Hazelcast.
From cache to in-memory data grid. Introduction to Hazelcast.
Ā 
Introduction to SQL Antipatterns
Introduction to SQL AntipatternsIntroduction to SQL Antipatterns
Introduction to SQL Antipatterns
Ā 
Planning your Next-Gen Change Data Capture (CDC) Architecture in 2019 - Strea...
Planning your Next-Gen Change Data Capture (CDC) Architecture in 2019 - Strea...Planning your Next-Gen Change Data Capture (CDC) Architecture in 2019 - Strea...
Planning your Next-Gen Change Data Capture (CDC) Architecture in 2019 - Strea...
Ā 
Reporting with Oracle Application Express (APEX)
Reporting with Oracle Application Express (APEX)Reporting with Oracle Application Express (APEX)
Reporting with Oracle Application Express (APEX)
Ā 
From Java 11 to 17 and beyond.pdf
From Java 11 to 17 and beyond.pdfFrom Java 11 to 17 and beyond.pdf
From Java 11 to 17 and beyond.pdf
Ā 
Java EE Introduction
Java EE IntroductionJava EE Introduction
Java EE Introduction
Ā 
More mastering the art of indexing
More mastering the art of indexingMore mastering the art of indexing
More mastering the art of indexing
Ā 
Load Data Fast!
Load Data Fast!Load Data Fast!
Load Data Fast!
Ā 

Viewers also liked

Mysql For Developers
Mysql For DevelopersMysql For Developers
Mysql For DevelopersCarol McDonald
Ā 
Galera cluster for high availability
Galera cluster for high availability Galera cluster for high availability
Galera cluster for high availability Mydbops
Ā 
MySQL InnoDB Cluster and MySQL Group Replication @HKOSC 2017
MySQL InnoDB Cluster and MySQL Group Replication @HKOSC 2017MySQL InnoDB Cluster and MySQL Group Replication @HKOSC 2017
MySQL InnoDB Cluster and MySQL Group Replication @HKOSC 2017Ivan Ma
Ā 
Hbaseęŗē åˆęŽ¢
Hbaseęŗē åˆęŽ¢Hbaseęŗē åˆęŽ¢
Hbaseęŗē åˆęŽ¢zhaolinjnu
Ā 
High Availability Using MySQL Group Replication
High Availability Using MySQL Group ReplicationHigh Availability Using MySQL Group Replication
High Availability Using MySQL Group ReplicationOSSCube
Ā 
Reducing Risk When Upgrading MySQL
Reducing Risk When Upgrading MySQLReducing Risk When Upgrading MySQL
Reducing Risk When Upgrading MySQLKenny Gryp
Ā 
Percona XtraDB Cluster vs Galera Cluster vs MySQL Group Replication
Percona XtraDB Cluster vs Galera Cluster vs MySQL Group ReplicationPercona XtraDB Cluster vs Galera Cluster vs MySQL Group Replication
Percona XtraDB Cluster vs Galera Cluster vs MySQL Group ReplicationKenny Gryp
Ā 
A New Architecture for Group Replication in Data Grid
A New Architecture for Group Replication in Data GridA New Architecture for Group Replication in Data Grid
A New Architecture for Group Replication in Data GridEditor IJCATR
Ā 
MySQL Group Replication
MySQL Group ReplicationMySQL Group Replication
MySQL Group ReplicationManish Kumar
Ā 
MySQL Storage Engines - which do you use? TokuDB? MyRocks? InnoDB?
MySQL Storage Engines - which do you use? TokuDB? MyRocks? InnoDB?MySQL Storage Engines - which do you use? TokuDB? MyRocks? InnoDB?
MySQL Storage Engines - which do you use? TokuDB? MyRocks? InnoDB?Sveta Smirnova
Ā 
Advanced Percona XtraDB Cluster in a nutshell... la suite
Advanced Percona XtraDB Cluster in a nutshell... la suiteAdvanced Percona XtraDB Cluster in a nutshell... la suite
Advanced Percona XtraDB Cluster in a nutshell... la suiteKenny Gryp
Ā 
Everything You Need to Know About MySQL Group Replication
Everything You Need to Know About MySQL Group ReplicationEverything You Need to Know About MySQL Group Replication
Everything You Need to Know About MySQL Group ReplicationNuno Carvalho
Ā 
MySQL InnoDB Cluster - Group Replication
MySQL InnoDB Cluster - Group ReplicationMySQL InnoDB Cluster - Group Replication
MySQL InnoDB Cluster - Group ReplicationFrederic Descamps
Ā 
MHA (MySQL High Availability): Getting started & moving past quirks
MHA (MySQL High Availability): Getting started & moving past quirksMHA (MySQL High Availability): Getting started & moving past quirks
MHA (MySQL High Availability): Getting started & moving past quirksColin Charles
Ā 
Lessons Learned: Troubleshooting Replication
Lessons Learned: Troubleshooting ReplicationLessons Learned: Troubleshooting Replication
Lessons Learned: Troubleshooting ReplicationSveta Smirnova
Ā 
MySQL InnoDB ęŗē å®žēŽ°åˆ†ęž(äø€)
MySQL InnoDB ęŗē å®žēŽ°åˆ†ęž(äø€)MySQL InnoDB ęŗē å®žēŽ°åˆ†ęž(äø€)
MySQL InnoDB ęŗē å®žēŽ°åˆ†ęž(äø€)frogd
Ā 
Multi Source Replication With MySQL 5.7 @ Verisure
Multi Source Replication With MySQL 5.7 @ VerisureMulti Source Replication With MySQL 5.7 @ Verisure
Multi Source Replication With MySQL 5.7 @ VerisureKenny Gryp
Ā 
MySQL Server Defaults
MySQL Server DefaultsMySQL Server Defaults
MySQL Server DefaultsMorgan Tocker
Ā 
MySQL Group Replication
MySQL Group ReplicationMySQL Group Replication
MySQL Group ReplicationKenny Gryp
Ā 

Viewers also liked (20)

Mysql For Developers
Mysql For DevelopersMysql For Developers
Mysql For Developers
Ā 
Galera cluster for high availability
Galera cluster for high availability Galera cluster for high availability
Galera cluster for high availability
Ā 
SQL Outer Joins for Fun and Profit
SQL Outer Joins for Fun and ProfitSQL Outer Joins for Fun and Profit
SQL Outer Joins for Fun and Profit
Ā 
MySQL InnoDB Cluster and MySQL Group Replication @HKOSC 2017
MySQL InnoDB Cluster and MySQL Group Replication @HKOSC 2017MySQL InnoDB Cluster and MySQL Group Replication @HKOSC 2017
MySQL InnoDB Cluster and MySQL Group Replication @HKOSC 2017
Ā 
Hbaseęŗē åˆęŽ¢
Hbaseęŗē åˆęŽ¢Hbaseęŗē åˆęŽ¢
Hbaseęŗē åˆęŽ¢
Ā 
High Availability Using MySQL Group Replication
High Availability Using MySQL Group ReplicationHigh Availability Using MySQL Group Replication
High Availability Using MySQL Group Replication
Ā 
Reducing Risk When Upgrading MySQL
Reducing Risk When Upgrading MySQLReducing Risk When Upgrading MySQL
Reducing Risk When Upgrading MySQL
Ā 
Percona XtraDB Cluster vs Galera Cluster vs MySQL Group Replication
Percona XtraDB Cluster vs Galera Cluster vs MySQL Group ReplicationPercona XtraDB Cluster vs Galera Cluster vs MySQL Group Replication
Percona XtraDB Cluster vs Galera Cluster vs MySQL Group Replication
Ā 
A New Architecture for Group Replication in Data Grid
A New Architecture for Group Replication in Data GridA New Architecture for Group Replication in Data Grid
A New Architecture for Group Replication in Data Grid
Ā 
MySQL Group Replication
MySQL Group ReplicationMySQL Group Replication
MySQL Group Replication
Ā 
MySQL Storage Engines - which do you use? TokuDB? MyRocks? InnoDB?
MySQL Storage Engines - which do you use? TokuDB? MyRocks? InnoDB?MySQL Storage Engines - which do you use? TokuDB? MyRocks? InnoDB?
MySQL Storage Engines - which do you use? TokuDB? MyRocks? InnoDB?
Ā 
Advanced Percona XtraDB Cluster in a nutshell... la suite
Advanced Percona XtraDB Cluster in a nutshell... la suiteAdvanced Percona XtraDB Cluster in a nutshell... la suite
Advanced Percona XtraDB Cluster in a nutshell... la suite
Ā 
Everything You Need to Know About MySQL Group Replication
Everything You Need to Know About MySQL Group ReplicationEverything You Need to Know About MySQL Group Replication
Everything You Need to Know About MySQL Group Replication
Ā 
MySQL InnoDB Cluster - Group Replication
MySQL InnoDB Cluster - Group ReplicationMySQL InnoDB Cluster - Group Replication
MySQL InnoDB Cluster - Group Replication
Ā 
MHA (MySQL High Availability): Getting started & moving past quirks
MHA (MySQL High Availability): Getting started & moving past quirksMHA (MySQL High Availability): Getting started & moving past quirks
MHA (MySQL High Availability): Getting started & moving past quirks
Ā 
Lessons Learned: Troubleshooting Replication
Lessons Learned: Troubleshooting ReplicationLessons Learned: Troubleshooting Replication
Lessons Learned: Troubleshooting Replication
Ā 
MySQL InnoDB ęŗē å®žēŽ°åˆ†ęž(äø€)
MySQL InnoDB ęŗē å®žēŽ°åˆ†ęž(äø€)MySQL InnoDB ęŗē å®žēŽ°åˆ†ęž(äø€)
MySQL InnoDB ęŗē å®žēŽ°åˆ†ęž(äø€)
Ā 
Multi Source Replication With MySQL 5.7 @ Verisure
Multi Source Replication With MySQL 5.7 @ VerisureMulti Source Replication With MySQL 5.7 @ Verisure
Multi Source Replication With MySQL 5.7 @ Verisure
Ā 
MySQL Server Defaults
MySQL Server DefaultsMySQL Server Defaults
MySQL Server Defaults
Ā 
MySQL Group Replication
MySQL Group ReplicationMySQL Group Replication
MySQL Group Replication
Ā 

Similar to Extensible Data Modeling

Dan Hotka's Top 10 Oracle 12c New Features
Dan Hotka's Top 10 Oracle 12c New FeaturesDan Hotka's Top 10 Oracle 12c New Features
Dan Hotka's Top 10 Oracle 12c New FeaturesEmbarcadero Technologies
Ā 
Optimizer percona live_ams2015
Optimizer percona live_ams2015Optimizer percona live_ams2015
Optimizer percona live_ams2015Manyi Lu
Ā 
Pl sql best practices document
Pl sql best practices documentPl sql best practices document
Pl sql best practices documentAshwani Pandey
Ā 
Creating and Managing Tables -Oracle Data base
Creating and Managing Tables -Oracle Data base Creating and Managing Tables -Oracle Data base
Creating and Managing Tables -Oracle Data base Salman Memon
Ā 
Less07 schema
Less07 schemaLess07 schema
Less07 schemaImran Ali
Ā 
Laracon EU 2018: OMG MySQL 8.0 is out! are we there yet?
Laracon EU 2018: OMG MySQL 8.0 is out! are we there yet?Laracon EU 2018: OMG MySQL 8.0 is out! are we there yet?
Laracon EU 2018: OMG MySQL 8.0 is out! are we there yet?Gabriela Ferrara
Ā 
Producing Readable Output with iSQL*Plus - Oracle Data Base
Producing Readable Output with iSQL*Plus - Oracle Data BaseProducing Readable Output with iSQL*Plus - Oracle Data Base
Producing Readable Output with iSQL*Plus - Oracle Data BaseSalman Memon
Ā 
Ryan-Symposium-v5
Ryan-Symposium-v5Ryan-Symposium-v5
Ryan-Symposium-v5Kevin Ryan
Ā 
Inno db 5_7_features
Inno db 5_7_featuresInno db 5_7_features
Inno db 5_7_featuresTinku Ajit
Ā 
1 - Introduction to PL/SQL
1 - Introduction to PL/SQL1 - Introduction to PL/SQL
1 - Introduction to PL/SQLrehaniltifat
Ā 
PL/SQL New and Advanced Features for Extreme Performance
PL/SQL New and Advanced Features for Extreme PerformancePL/SQL New and Advanced Features for Extreme Performance
PL/SQL New and Advanced Features for Extreme PerformanceZohar Elkayam
Ā 
Chetan postgresql partitioning
Chetan postgresql partitioningChetan postgresql partitioning
Chetan postgresql partitioningOpenSourceIndia
Ā 
Chetan postgresql partitioning
Chetan postgresql partitioningChetan postgresql partitioning
Chetan postgresql partitioningsuniltomar04
Ā 
Materialized Column: An Efficient Way to Optimize Queries on Nested Columns
Materialized Column: An Efficient Way to Optimize Queries on Nested ColumnsMaterialized Column: An Efficient Way to Optimize Queries on Nested Columns
Materialized Column: An Efficient Way to Optimize Queries on Nested ColumnsDatabricks
Ā 
TSQL in SQL Server 2012
TSQL in SQL Server 2012TSQL in SQL Server 2012
TSQL in SQL Server 2012Eduardo Castro
Ā 

Similar to Extensible Data Modeling (20)

Dan Hotka's Top 10 Oracle 12c New Features
Dan Hotka's Top 10 Oracle 12c New FeaturesDan Hotka's Top 10 Oracle 12c New Features
Dan Hotka's Top 10 Oracle 12c New Features
Ā 
Optimizer percona live_ams2015
Optimizer percona live_ams2015Optimizer percona live_ams2015
Optimizer percona live_ams2015
Ā 
Pl sql best practices document
Pl sql best practices documentPl sql best practices document
Pl sql best practices document
Ā 
Creating and Managing Tables -Oracle Data base
Creating and Managing Tables -Oracle Data base Creating and Managing Tables -Oracle Data base
Creating and Managing Tables -Oracle Data base
Ā 
Les09
Les09Les09
Les09
Ā 
Erik_van_Roon.pdf
Erik_van_Roon.pdfErik_van_Roon.pdf
Erik_van_Roon.pdf
Ā 
Less07 schema
Less07 schemaLess07 schema
Less07 schema
Ā 
Sql server T-sql basics ppt-3
Sql server T-sql basics  ppt-3Sql server T-sql basics  ppt-3
Sql server T-sql basics ppt-3
Ā 
Les09.ppt
Les09.pptLes09.ppt
Les09.ppt
Ā 
Laracon EU 2018: OMG MySQL 8.0 is out! are we there yet?
Laracon EU 2018: OMG MySQL 8.0 is out! are we there yet?Laracon EU 2018: OMG MySQL 8.0 is out! are we there yet?
Laracon EU 2018: OMG MySQL 8.0 is out! are we there yet?
Ā 
Producing Readable Output with iSQL*Plus - Oracle Data Base
Producing Readable Output with iSQL*Plus - Oracle Data BaseProducing Readable Output with iSQL*Plus - Oracle Data Base
Producing Readable Output with iSQL*Plus - Oracle Data Base
Ā 
Ryan-Symposium-v5
Ryan-Symposium-v5Ryan-Symposium-v5
Ryan-Symposium-v5
Ā 
Inno db 5_7_features
Inno db 5_7_featuresInno db 5_7_features
Inno db 5_7_features
Ā 
1 - Introduction to PL/SQL
1 - Introduction to PL/SQL1 - Introduction to PL/SQL
1 - Introduction to PL/SQL
Ā 
PL/SQL New and Advanced Features for Extreme Performance
PL/SQL New and Advanced Features for Extreme PerformancePL/SQL New and Advanced Features for Extreme Performance
PL/SQL New and Advanced Features for Extreme Performance
Ā 
Chetan postgresql partitioning
Chetan postgresql partitioningChetan postgresql partitioning
Chetan postgresql partitioning
Ā 
Chetan postgresql partitioning
Chetan postgresql partitioningChetan postgresql partitioning
Chetan postgresql partitioning
Ā 
Resume
Resume Resume
Resume
Ā 
Materialized Column: An Efficient Way to Optimize Queries on Nested Columns
Materialized Column: An Efficient Way to Optimize Queries on Nested ColumnsMaterialized Column: An Efficient Way to Optimize Queries on Nested Columns
Materialized Column: An Efficient Way to Optimize Queries on Nested Columns
Ā 
TSQL in SQL Server 2012
TSQL in SQL Server 2012TSQL in SQL Server 2012
TSQL in SQL Server 2012
Ā 

More from Karwin Software Solutions LLC (9)

InnoDB Locking Explained with Stick Figures
InnoDB Locking Explained with Stick FiguresInnoDB Locking Explained with Stick Figures
InnoDB Locking Explained with Stick Figures
Ā 
Survey of Percona Toolkit
Survey of Percona ToolkitSurvey of Percona Toolkit
Survey of Percona Toolkit
Ā 
Schemadoc
SchemadocSchemadoc
Schemadoc
Ā 
Percona toolkit
Percona toolkitPercona toolkit
Percona toolkit
Ā 
MySQL 5.5 Guide to InnoDB Status
MySQL 5.5 Guide to InnoDB StatusMySQL 5.5 Guide to InnoDB Status
MySQL 5.5 Guide to InnoDB Status
Ā 
Requirements the Last Bottleneck
Requirements the Last BottleneckRequirements the Last Bottleneck
Requirements the Last Bottleneck
Ā 
Mentor Your Indexes
Mentor Your IndexesMentor Your Indexes
Mentor Your Indexes
Ā 
Sql Injection Myths and Fallacies
Sql Injection Myths and FallaciesSql Injection Myths and Fallacies
Sql Injection Myths and Fallacies
Ā 
Full Text Search In PostgreSQL
Full Text Search In PostgreSQLFull Text Search In PostgreSQL
Full Text Search In PostgreSQL
Ā 

Recently uploaded

AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
Ā 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsNanddeep Nachan
Ā 
Mcleodganj Call Girls šŸ„° 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls šŸ„° 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls šŸ„° 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls šŸ„° 8617370543 Service Offer VIP Hot ModelDeepika Singh
Ā 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusZilliz
Ā 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdfSandro Moreira
Ā 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
Ā 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDropbox
Ā 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamUiPathCommunity
Ā 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistandanishmna97
Ā 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
Ā 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
Ā 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
Ā 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Orbitshub
Ā 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...apidays
Ā 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxRemote DBA Services
Ā 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Zilliz
Ā 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesrafiqahmad00786416
Ā 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Victor Rentea
Ā 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native ApplicationsWSO2
Ā 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MIND CTI
Ā 

Recently uploaded (20)

AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
Ā 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
Ā 
Mcleodganj Call Girls šŸ„° 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls šŸ„° 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls šŸ„° 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls šŸ„° 8617370543 Service Offer VIP Hot Model
Ā 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
Ā 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
Ā 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
Ā 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
Ā 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
Ā 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
Ā 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Ā 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
Ā 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Ā 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Ā 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
Ā 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
Ā 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Ā 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
Ā 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Ā 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
Ā 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
Ā 

Extensible Data Modeling

  • 1. Ā© Ā 2011 Ā ā€“ Ā 2013 Ā PERCONA Ā  Extensible Data Modeling with MySQL Bill Karwin Percona Live MySQL Conference & ExpoBill Karwin Percona Live 2013
  • 2. Ā© Ā 2011 Ā ā€“ Ā 2013 Ā PERCONA Ā  ā€œI need to add a new columnā€” but I donā€™t want ALTER TABLE to lock the application for a long time.ā€
  • 3. Ā© Ā 2011 Ā ā€“ Ā 2013 Ā PERCONA Ā  How MySQL Does ALTER TABLE 1.ā€Æ Lock the table. 2.ā€Æ Make a new, empty the table like the original. 3.ā€Æ Modify the columns of the new empty table. 4.ā€Æ Copy all rows of data from original to new tableā€¦ no matter how long it takes. 5.ā€Æ Swap the old and new tables. 6.ā€Æ Unlock the tables drop the original.
  • 4. Ā© Ā 2011 Ā ā€“ Ā 2013 Ā PERCONA Ā  Extensibility ā€¢ā€Æ How can we add new attributes without the pain of schema changes? ā€“ā€Æ Object-oriented modeling ā€“ā€Æ Sparse columns ā€¢ā€Æ Especially to support user-defined attributes at runtime or after deployment: ā€“ā€Æ Content management systems ā€“ā€Æ E-commerce frameworks ā€“ā€Æ Games
  • 5. Ā© Ā 2011 Ā ā€“ Ā 2013 Ā PERCONA Ā  Solutions ā€¢ā€Æ ā€œExtra Columnsā€ ā€¢ā€Æ Entity-Attribute-Value ā€¢ā€Æ Class Table Inheritance ā€¢ā€Æ Serialized LOB Inverted Indexes ā€¢ā€Æ Online Schema Changes ā€¢ā€Æ Non-Relational Databases
  • 6. Ā© Ā 2011 Ā ā€“ Ā 2013 Ā PERCONA Ā  EXTRA COLUMNS
  • 7. Ā© Ā 2011 Ā ā€“ Ā 2013 Ā PERCONA Ā  Table with Fixed Columns CREATE TABLE Title (! id int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,! title text NOT NULL,! imdb_index varchar(12) DEFAULT NULL,! kind_id int(11) NOT NULL,! production_year int(11) DEFAULT NULL,! imdb_id int(11) DEFAULT NULL,! phonetic_code varchar(5) DEFAULT NULL,! episode_of_id int(11) DEFAULT NULL,! season_nr int(11) DEFAULT NULL,! episode_nr int(11) DEFAULT NULL,! series_years varchar(49) DEFAULT NULL,! title_crc32 int(10) unsigned DEFAULT NULL! );!
  • 8. Ā© Ā 2011 Ā ā€“ Ā 2013 Ā PERCONA Ā  Table with Extra Columns CREATE TABLE Title (! id int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,! title text NOT NULL,! imdb_index varchar(12) DEFAULT NULL,! kind_id int(11) NOT NULL,! production_year int(11) DEFAULT NULL,! imdb_id int(11) DEFAULT NULL,! phonetic_code varchar(5) DEFAULT NULL,! extra_data1 TEXT DEFAULT NULL,! extra_data2 TEXT DEFAULT NULL, ! extra_data3 TEXT DEFAULT NULL,! extra_data4 TEXT DEFAULT NULL,! extra_data5 TEXT DEFAULT NULL,! extra_data6 TEXT DEFAULT NULL,! );! use for whatever comes up that we didnā€™t think of at the start of the project
  • 9. Ā© Ā 2011 Ā ā€“ Ā 2013 Ā PERCONA Ā  Adding a New Attribute UPDATE Title ā€Ø SET extra_data3 = 'PG-13'ā€Ø WHERE id = 207468;! remember which column you used for each new attribute!
  • 10. Ā© Ā 2011 Ā ā€“ Ā 2013 Ā PERCONA Ā  Pros and Cons ā€¢ā€Æ Good solution: ā€“ā€Æ No ALTER TABLE necessary to use a column for a new attributeā€”only a project decision is needed. ā€“ā€Æ Related to Single Table Inheritance (STI) http://martinfowler.com/eaaCatalog/singleTableInheritance.html
  • 11. Ā© Ā 2011 Ā ā€“ Ā 2013 Ā PERCONA Ā  Pros and Cons ā€¢ā€Æ Bad solution: ā€“ā€Æ If you run out of extra columns, then youā€™re back to ALTER TABLE. ā€“ā€Æ Anyone can put any data in the columnsā€”you canā€™t assume consistent usage on every row. ā€“ā€Æ Columns lack descriptive names or the right data type.
  • 12. Ā© Ā 2011 Ā ā€“ Ā 2013 Ā PERCONA Ā  ENTITY-ATTRIBUTE-VALUE
  • 13. Ā© Ā 2011 Ā ā€“ Ā 2013 Ā PERCONA Ā  EAV ā€¢ā€Æ Store each attribute in a row instead of a column. CREATE TABLE Attributes (ā€Ø entity INT NOT NULL,ā€Ø attribute VARCHAR(20) NOT NULL,ā€Ø value TEXT,ā€Ø FOREIGN KEY (entity) ā€Ø REFERENCES Title (id)ā€Ø );!
  • 14. Ā© Ā 2011 Ā ā€“ Ā 2013 Ā PERCONA Ā  Example EAV Data SELECT * FROM Attributes;! +--------+-----------------+---------------------+! | entity | attribute | value |! +--------+-----------------+---------------------+! | 207468 | title | Goldfinger |! | 207468 | production_year | 1964 |! | 207468 | rating | 7.8 |! | 207468 | length | 110 min |! +--------+-----------------+---------------------+! ! ! !
  • 15. Ā© Ā 2011 Ā ā€“ Ā 2013 Ā PERCONA Ā  Adding a New Attribute ā€¢ā€Æ Simply use INSERT with a new attribute name. INSERT INTO Attributes (entity, attribute, value)ā€Ø VALUES (207468, 'budget', '$3,000,000');!
  • 16. Ā© Ā 2011 Ā ā€“ Ā 2013 Ā PERCONA Ā  Query EAV as a Pivot SELECT a.entity AS id,! a.value AS title,! y.value AS production_year,! r.value AS rating,ā€Ø b.value AS budget! FROM Attributes AS a! JOIN Attributes AS y USING (entity)! JOIN Attributes AS r USING (entity)! JOIN Attributes AS b USING (entity)! WHERE a.attribute = 'title'! AND y.attribute = 'production_year'! AND r.attribute = 'rating'ā€Ø AND b.attribute = 'budget';! +--------+------------+-----------------+--------+------------+! | id | title | production_year | rating | budget |! +--------+------------+-----------------+--------+------------+! | 207468 | Goldfinger | 1964 | 7.8 | $3,000,000 |! +--------+------------+-----------------+--------+------------+! another join required for each additional attribute
  • 17. Ā© Ā 2011 Ā ā€“ Ā 2013 Ā PERCONA Ā 
  • 18. Ā© Ā 2011 Ā ā€“ Ā 2013 Ā PERCONA Ā  Sounds Simple Enough, Butā€¦ ā€¢ā€Æ NOT NULL doesnā€™t work ā€¢ā€Æ FOREIGN KEY doesnā€™t work ā€¢ā€Æ UNIQUE KEY doesnā€™t work ā€¢ā€Æ Data types donā€™t work ā€¢ā€Æ Searches donā€™t scale ā€¢ā€Æ Indexes and storage are inefficient
  • 19. Ā© Ā 2011 Ā ā€“ Ā 2013 Ā PERCONA Ā  Constraints Donā€™t Work CREATE TABLE Attributes (ā€Ø entity INT NOT NULL,ā€Ø attribute VARCHAR(20) NOT NULL,ā€Ø value TEXT NOT NULL,ā€Ø FOREIGN KEY (entity) ā€Ø REFERENCES Title (id)ā€Ø FOREIGN KEY (value) ā€Ø REFERENCES Ratings (rating)ā€Ø );! constraints apply to all rows, not just rows for a specific attribute type
  • 20. Ā© Ā 2011 Ā ā€“ Ā 2013 Ā PERCONA Ā  Data Types Donā€™t Work INSERT INTO Attributes (entity, attribute, value)ā€Ø VALUES (207468, 'budget', 'banana');! database cannot prevent application storing nonsensical data
  • 21. Ā© Ā 2011 Ā ā€“ Ā 2013 Ā PERCONA Ā  Add Typed Value Columns? CREATE TABLE Attributes (ā€Ø entity INT NOT NULL,ā€Ø attribute VARCHAR(20) NOT NULL,ā€Ø intvalue BIGINT,ā€Ø floatvalue FLOAT,ā€Ø textvalue TEXT,ā€Ø datevalue DATE,ā€Ø datetimevalue DATETIME, ā€Ø FOREIGN KEY (entity) ā€Ø REFERENCES Title (id)ā€Ø );! now my application needs to know which data type column to use for each attribute when inserting and querying
  • 22. Ā© Ā 2011 Ā ā€“ Ā 2013 Ā PERCONA Ā  Searches Donā€™t Scale ā€¢ā€Æ You must hard-code each attribute name, ā€“ā€Æ One JOIN per attribute! ā€¢ā€Æ Alternatively, you can query all attributes, but the result is one attribute per row: SELECT attribute, value ā€Ø FROM Attributes ā€Ø WHERE entity = 207468;! ā€“ā€Æ ā€¦and sort it out in your application code.
  • 23. Ā© Ā 2011 Ā ā€“ Ā 2013 Ā PERCONA Ā  Indexes and Storage Are Inefficient ā€¢ā€Æ Many rows, with few distinct attribute names. ā€“ā€Æ Poor index cardinality. ā€¢ā€Æ The entity and attribute columns use extra space for every attribute of every ā€œrow.ā€ ā€“ā€Æ In a conventional table, the entity is the primary key, so itā€™s stored only once per row. ā€“ā€Æ The attribute name is in the table definition, so itā€™s stored only once per table.
  • 24. Ā© Ā 2011 Ā ā€“ Ā 2013 Ā PERCONA Ā  Pros and Cons ā€¢ā€Æ Good solution: ā€“ā€Æ No ALTER TABLE needed againā€”ever! ā€“ā€Æ Supports ultimate flexibility, potentially any ā€œrowā€ can have its own distinct set of attributes.
  • 25. Ā© Ā 2011 Ā ā€“ Ā 2013 Ā PERCONA Ā  Pros and Cons ā€¢ā€Æ Bad solution: ā€“ā€Æ SQL operations become more complex. ā€“ā€Æ Lots of application code required to reinvent features that an RDBMS already provides. ā€“ā€Æ Doesnā€™t scale wellā€”pivots required.
  • 26. Ā© Ā 2011 Ā ā€“ Ā 2013 Ā PERCONA Ā  CLASS TABLE INHERITANCE
  • 27. Ā© Ā 2011 Ā ā€“ Ā 2013 Ā PERCONA Ā  Subtypes ā€¢ā€Æ Titles includes: ā€“ā€Æ Films ā€“ā€Æ TV shows ā€“ā€Æ TV episodes ā€“ā€Æ Video games ā€¢ā€Æ Some attributes apply to all, other attributes apply to one subtype or the other.
  • 28. Ā© Ā 2011 Ā ā€“ Ā 2013 Ā PERCONA Ā  Title Table CREATE TABLE Title (! id int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,! title text NOT NULL,! imdb_index varchar(12) DEFAULT NULL,! kind_id int(11) NOT NULL,! production_year int(11) DEFAULT NULL,! imdb_id int(11) DEFAULT NULL,! phonetic_code varchar(5) DEFAULT NULL,! episode_of_id int(11) DEFAULT NULL,! season_nr int(11) DEFAULT NULL,! episode_nr int(11) DEFAULT NULL,! series_years varchar(49) DEFAULT NULL,! title_crc32 int(10) unsigned DEFAULT NULL! );! only for tv shows
  • 29. Ā© Ā 2011 Ā ā€“ Ā 2013 Ā PERCONA Ā  Title Table with Subtype Tables CREATE TABLE Title (! id int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,! title text NOT NULL,! imdb_index varchar(12) DEFAULT NULL,! kind_id int(11) NOT NULL,! production_year int(11) DEFAULT NULL,! imdb_id int(11) DEFAULT NULL,! phonetic_code varchar(5) DEFAULT NULL,! title_crc32 int(10) unsigned DEFAULT NULL,! PRIMARY KEY (id)! );! ! CREATE TABLE Film (! id int(11) NOT NULL PRIMARY KEY,ā€Ø aspect_ratio varchar(20),! FOREIGN KEY (id) REFERENCES Title(id)! );! ! CREATE TABLE TVShow (! id int(11) NOT NULL PRIMARY KEY,! episode_of_id int(11) DEFAULT NULL,! season_nr int(11) DEFAULT NULL,! episode_nr int(11) DEFAULT NULL,! series_years varchar(49) DEFAULT NULL,! FOREIGN KEY (id) REFERENCES Title(id)! );! Title Ā  Film Ā  TVShow Ā  1:1 1:1
  • 30. Ā© Ā 2011 Ā ā€“ Ā 2013 Ā PERCONA Ā  Adding a New Subtype ā€¢ā€Æ Create a new tableā€”without locking existing tables. CREATE TABLE VideoGames (ā€Ø id int(11) NOT NULL PRIMARY KEY,ā€Ø platforms varchar(100) NOT NULL,ā€Ø FOREIGN KEY (id) ā€Ø REFERENCES Title(id)ā€Ø );!
  • 31. Ā© Ā 2011 Ā ā€“ Ā 2013 Ā PERCONA Ā  Pros and Cons ā€¢ā€Æ Good solution: ā€“ā€Æ Best to support a finite set of subtypes, which are likely unchanging after creation. ā€“ā€Æ Data types and constraints work normally. ā€“ā€Æ Easy to create or drop subtype tables. ā€“ā€Æ Easy to query attributes common to all subtypes. ā€“ā€Æ Subtype tables are shorter, indexes are smaller.
  • 32. Ā© Ā 2011 Ā ā€“ Ā 2013 Ā PERCONA Ā  Pros and Cons ā€¢ā€Æ Bad solution: ā€“ā€Æ Adding one entry takes two INSERT statements. ā€“ā€Æ Querying attributes of subtypes requires a join. ā€“ā€Æ Querying all types with subtype attributes requires multiple joins (as many as subtypes). ā€“ā€Æ Adding a common attribute locks a large table. ā€“ā€Æ Adding an attribute to a populated subtype locks a smaller table.
  • 33. Ā© Ā 2011 Ā ā€“ Ā 2013 Ā PERCONA Ā  SERIALIZED LOB
  • 34. Ā© Ā 2011 Ā ā€“ Ā 2013 Ā PERCONA Ā  What is Serializing? ā€¢ā€Æ Objects in your applications can be represented in serialized formā€”i.e., convert the object to a scalar string that you can save and load back as an object. ā€“ā€Æ Java objects implementing Serializable and processed with writeObject()! ā€“ā€Æ PHP variables processed with serialize()! ā€“ā€Æ Python objects processed with pickle.dump()! ā€“ā€Æ Data encoded with XML, JSON, YAML, etc.
  • 35. Ā© Ā 2011 Ā ā€“ Ā 2013 Ā PERCONA Ā  What Is a LOB? ā€¢ā€Æ The BLOB or TEXT datatypes can store long sequences of bytes or characters, such as a string. ā€¢ā€Æ You can store the string representing your object into a single BLOB or TEXT column. ā€“ā€Æ You donā€™t need to define SQL columns for each field of your object.
  • 36. Ā© Ā 2011 Ā ā€“ Ā 2013 Ā PERCONA Ā  Title Table with Serialized LOB CREATE TABLE Title (ā€Ø id int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,ā€Ø title text NOT NULL,ā€Ø imdb_index varchar(12) DEFAULT NULL,ā€Ø kind_id int(11) NOT NULL,ā€Ø production_year int(11) DEFAULT NULL,ā€Ø imdb_id int(11) DEFAULT NULL,ā€Ø phonetic_code varchar(5) DEFAULT NULL,ā€Ø title_crc32 int(10) unsigned DEFAULT NULLā€Ø extra_info TEXT ā€Ø );! holds everything else, plus anything we didnā€™t think of
  • 37. Ā© Ā 2011 Ā ā€“ Ā 2013 Ā PERCONA Ā  Adding a New Attribute UPDATE Titleā€Ø SET extra_info = ā€Ø '{ā€Ø episode_of_id: 1291895, ā€Ø season_nr: 5, ā€Ø episode_nr: 6ā€Ø }'ā€Ø WHERE id = 1292057;! JSON example
  • 38. Ā© Ā 2011 Ā ā€“ Ā 2013 Ā PERCONA Ā  Using XML in MySQL ā€¢ā€Æ MySQL has limited support for XML. SELECT id, title, ā€Ø ExtractValue(extra_info, '/episode_nr') ā€Ø AS episode_nr ā€Ø FROM Titleā€Ø WHERE ExtractValue(extra_info, ā€Ø '/episode_of_id') = 1292057;! ā€¢ā€Æ Forces table-scans, not possible to use indexes. http://dev.mysql.com/doc/refman/5.6/en/xml-functions.html
  • 39. Ā© Ā 2011 Ā ā€“ Ā 2013 Ā PERCONA Ā  Dynamic Columns in MariaDB CREATE TABLE Title (ā€Ø id int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,ā€Ø title text NOT NULL,ā€Ø ...ā€Ø extra_info BLOB ā€Ø );! INSERT INTO Title (title, extra_info) ā€Ø VALUES ('Trials and Tribble-ations', ā€Ø COLUMN_CREATE('episode_of_id', '1291895', ā€Ø 'episode_nr', '5',ā€Ø 'season_nr', '6'));! https://kb.askmonty.org/en/dynamic-columns/
  • 40. Ā© Ā 2011 Ā ā€“ Ā 2013 Ā PERCONA Ā  Pros and Cons ā€¢ā€Æ Good solution: ā€“ā€Æ Store any object and add new custom fields at any time. ā€“ā€Æ No need to do ALTER TABLE to add custom fields.
  • 41. Ā© Ā 2011 Ā ā€“ Ā 2013 Ā PERCONA Ā  Pros and Cons ā€¢ā€Æ Bad solution: ā€“ā€Æ Not indexable. ā€“ā€Æ Must return the whole object, not an individual field. ā€“ā€Æ Must write the whole object to update a single field. ā€“ā€Æ Hard to use a custom field in a WHERE clause, GROUP BY or ORDER BY. ā€“ā€Æ No support in the database for data types or constraints, e.g. NOT NULL, UNIQUE, FOREIGN KEY.
  • 42. Ā© Ā 2011 Ā ā€“ Ā 2013 Ā PERCONA Ā  INVERTED INDEXES
  • 43. Ā© Ā 2011 Ā ā€“ Ā 2013 Ā PERCONA Ā  Use This with Serialized LOB ā€¢ā€Æ Helps to mitigate some of the weaknesses.
  • 44. Ā© Ā 2011 Ā ā€“ Ā 2013 Ā PERCONA Ā  How This Works ā€¢ā€Æ Create a new table for each field of the LOB that you want to address individually: CREATE TABLE Title_EpisodeOf (ā€Ø episode_of_id INT NOT NULL,ā€Ø id INT NOT NULL,ā€Ø PRIMARY KEY (episode_of_id, id),ā€Ø FOREIGN KEY (id)ā€Ø REFERENCES Title (id)ā€Ø );! hereā€™s where you get the index support
  • 45. Ā© Ā 2011 Ā ā€“ Ā 2013 Ā PERCONA Ā  How This Works ā€¢ā€Æ For each LOB containing an ā€œepisode_of_idā€ field, insert a row to the attribute table with its value. INSERT INTO Title_EpisodeOf ā€Ø VALUES (1291895, 1292057);! ā€¢ā€Æ If another title doesnā€™t have this field, then you donā€™t create a referencing row.
  • 46. Ā© Ā 2011 Ā ā€“ Ā 2013 Ā PERCONA Ā  Query for Recent Users SELECT u.*ā€Ø FROM Title_EpisodeOf AS eā€Ø JOIN Title AS t USING (id)ā€Ø WHERE e.episode_of_id = '1291895';! This is a primary key lookup. It matches only titles that have such a field, and whose value matches the condition
  • 47. Ā© Ā 2011 Ā ā€“ Ā 2013 Ā PERCONA Ā  Pros and Cons ā€¢ā€Æ Good solution: ā€“ā€Æ Preserves the advantage of Serialized LOB. ā€“ā€Æ Adds support for SQL data types, and UNIQUE and FOREIGN KEY constraints. ā€“ā€Æ You can index any custom fieldā€”without locking the master table.
  • 48. Ā© Ā 2011 Ā ā€“ Ā 2013 Ā PERCONA Ā  Pros and Cons ā€¢ā€Æ Bad solution: ā€“ā€Æ Redundant storage. ā€“ā€Æ Itā€™s up to you to keep attribute tables in sync manually (or with triggers). ā€“ā€Æ Requires JOIN to fetch the master row. ā€“ā€Æ You must plan which columns you want to be indexed (but this is true of conventional columns too). ā€“ā€Æ Still no support for NOT NULL constraint.
  • 49. Ā© Ā 2011 Ā ā€“ Ā 2013 Ā PERCONA Ā  ONLINE SCHEMA CHANGES
  • 50. Ā© Ā 2011 Ā ā€“ Ā 2013 Ā PERCONA Ā  pt-online-schema-change ā€¢ā€Æ Performs online, non-blocking ALTER TABLE. ā€“ā€Æ Captures concurrent updates to a table while restructuring. ā€“ā€Æ Some risks and caveats exist; please read the manual and test carefully. ā€¢ā€Æ Free toolā€”part of Percona Toolkit. ā€“ā€Æ http://www.percona.com/doc/percona-toolkit/pt-online-schema- change.html
  • 51. Ā© Ā 2011 Ā ā€“ Ā 2013 Ā PERCONA Ā  How MySQL Does ALTER TABLE 1.ā€Æ Lock the table. 2.ā€Æ Make a new, empty the table like the original. 3.ā€Æ Modify the columns of the new empty table. 4.ā€Æ Copy all rows of data from original to new table. 5.ā€Æ Swap the old and new tables. 6.ā€Æ Unlock the tables drop the original.
  • 52. Ā© Ā 2011 Ā ā€“ Ā 2013 Ā PERCONA Ā  How pt-osc Does ALTER TABLE Lock the table. 1.ā€Æ Make a new, empty the table like the original. 2.ā€Æ Modify the columns of the new empty table. 3.ā€Æ Copy all rows of data from original to new table. a.ā€Æ Iterate over the table in chunks, in primary key order. b.ā€Æ Use triggers to capture ongoing changes in the original, and apply them to the new table. 4.ā€Æ Swap the tables, then drop the original. Unlock the tables.
  • 53. Ā© Ā 2011 Ā ā€“ Ā 2013 Ā PERCONA Ā  Visualize This (1) cast_info after trigger cast_info new
  • 54. Ā© Ā 2011 Ā ā€“ Ā 2013 Ā PERCONA Ā  Visualize This (2) cast_info after trigger cast_info new
  • 55. Ā© Ā 2011 Ā ā€“ Ā 2013 Ā PERCONA Ā  Visualize This (3) cast_info after trigger cast_info new
  • 56. Ā© Ā 2011 Ā ā€“ Ā 2013 Ā PERCONA Ā  Visualize This (4) cast_info after trigger cast_info new
  • 57. Ā© Ā 2011 Ā ā€“ Ā 2013 Ā PERCONA Ā  Visualize This (5) cast_info after trigger cast_info new
  • 58. Ā© Ā 2011 Ā ā€“ Ā 2013 Ā PERCONA Ā  Visualize This (6) cast_info cast_info old DROP
  • 59. Ā© Ā 2011 Ā ā€“ Ā 2013 Ā PERCONA Ā  Adding a New Attribute ā€¢ā€Æ Design the ALTER TABLE statement, but donā€™t execute it yet. mysql ALTER TABLE cast_info ā€Ø ADD COLUMN source INT NOT NULL;! ā€¢ā€Æ Equivalent pt-online-schema-change command: $ pt-online-schema-change ā€Ø h=localhost,D=imdb,t=cast_info ā€Ø --alter ADD COLUMN source INT NOT NULL!
  • 60. Ā© Ā 2011 Ā ā€“ Ā 2013 Ā PERCONA Ā  Execute $ pt-online-schema-change h=localhost,D=imdb,t=cast_info ā€Ø --alter ADD COLUMN source INT NOT NULL --execute! ! Altering `imdb`.`cast_info`...! Creating new table...! Created new table imdb._cast_info_new OK.! Altering new table...! Altered `imdb`.`_cast_info_new` OK.! Creating triggers...! Created triggers OK.! Copying approximately 22545051 rows...! Copying `imdb`.`cast_info`: 10% 04:05 remain! Copying `imdb`.`cast_info`: 19% 04:07 remain! Copying `imdb`.`cast_info`: 28% 03:44 remain! Copying `imdb`.`cast_info`: 37% 03:16 remain! Copying `imdb`.`cast_info`: 47% 02:47 remain! Copying `imdb`.`cast_info`: 56% 02:18 remain! Copying `imdb`.`cast_info`: 64% 01:53 remain! Copying `imdb`.`cast_info`: 73% 01:28 remain! Copying `imdb`.`cast_info`: 82% 00:55 remain! Copying `imdb`.`cast_info`: 91% 00:26 remain! Copied rows OK.! Swapping tables...! Swapped original and new tables OK.! Dropping old table...! Dropped old table `imdb`.`_cast_info_old` OK.! Dropping triggers...! Dropped triggers OK.! Successfully altered `imdb`.`cast_info`.! ! !
  • 61. Ā© Ā 2011 Ā ā€“ Ā 2013 Ā PERCONA Ā  Self-Adjusting ā€¢ā€Æ Copies rows in chunks which the tool sizes dynamically. ā€¢ā€Æ The tool throttles back if it increases load too much or if it causes any replication slaves to lag. ā€¢ā€Æ The tool tries to set its lock timeouts to let applications be more likely to succeed.
  • 62. Ā© Ā 2011 Ā ā€“ Ā 2013 Ā PERCONA Ā  Why Shouldnā€™t I Use This? ā€¢ā€Æ Is your table small enough that ALTER is already quick enough? ā€¢ā€Æ Is your change already very quick, for example DROP KEY in InnoDB? ā€¢ā€Æ Will pt-online-schema-change take too long or increase the load too much?
  • 63. Ā© Ā 2011 Ā ā€“ Ā 2013 Ā PERCONA Ā  Pros and Cons ā€¢ā€Æ Good solution: ā€“ā€Æ ALTER TABLE to add conventional columns without the pain of locking.
  • 64. Ā© Ā 2011 Ā ā€“ Ā 2013 Ā PERCONA Ā  Pros and Cons ā€¢ā€Æ Bad solution: ā€“ā€Æ Can take up to 4Ɨ more time than ALTER TABLE. ā€“ā€Æ Table must have a PRIMARY key. ā€“ā€Æ Table must not have triggers. ā€“ā€Æ No need if your table is small and ALTER TABLE already runs quickly enough. ā€“ā€Æ No need for some ALTER TABLE operations that donā€™t restructure the table (e.g. dropping indexes, adding comments).
  • 65. Ā© Ā 2011 Ā ā€“ Ā 2013 Ā PERCONA Ā  NON-RELATIONAL DATABASES
  • 66. Ā© Ā 2011 Ā ā€“ Ā 2013 Ā PERCONA Ā  No Rules to Break ā€¢ā€Æ To be relational, a table must have a fixed set of columns on every row. ā€¢ā€Æ No such rule exists in a non-relational model; you can store a distinct set of fields per record. ā€¢ā€Æ No schema makes NoSQL more flexible.
  • 67. Ā© Ā 2011 Ā ā€“ Ā 2013 Ā PERCONA Ā  Adding a New Attribute ā€¢ā€Æ Document-oriented databases are designed to support defining distinct attributes per document. ā€¢ā€Æ But you lose advantages of relational databases: ā€“ā€Æ Data types ā€“ā€Æ Constraints ā€“ā€Æ Uniform structure of records
  • 68. Ā© Ā 2011 Ā ā€“ Ā 2013 Ā PERCONA Ā  SUMMARY
  • 69. Ā© Ā 2011 Ā ā€“ Ā 2013 Ā PERCONA Ā  Summary Solu%on Ā  Lock-Ā­ā€free Ā  Flexible Ā  Select Ā  Filter Ā  Indexed Ā  Data Ā Types Ā  Constraints Ā  Extra Ā  Columns Ā  no* Ā  no Ā  yes Ā  yes Ā  yes* Ā  no Ā  no Ā  EAV Ā  yes Ā  yes Ā  yes* Ā  yes Ā  yes* Ā  no* Ā  no Ā  CTI Ā  no* Ā  no Ā  yes Ā  yes Ā  yes Ā  yes Ā  yes Ā  LOB Ā  yes Ā  yes Ā  no Ā  no Ā  no Ā  no Ā  no Ā  Inverted Ā  Index Ā  yes Ā  yes Ā  yes Ā  yes Ā  yes Ā  yes Ā  yes Ā  OSC Ā  yes Ā  no Ā  yes Ā  yes Ā  yes Ā  yes Ā  yes Ā  NoSQL Ā  yes Ā  yes Ā  yes Ā  yes Ā  yes Ā  no* Ā  no Ā  * conditions or exceptions apply.
  • 70. Ā© Ā 2011 Ā ā€“ Ā 2013 Ā PERCONA Ā  Senior Industry Experts In-Person and Online Classes Custom Onsite Training http://percona.com/training
  • 71. Ā© Ā 2011 Ā ā€“ Ā 2013 Ā PERCONA Ā  ā€œRate This Sessionā€ http://www.percona.com/live/mysql-conference-2013/ sessions/extensible-data-modeling-mysql
  • 72. Ā© Ā 2011 Ā ā€“ Ā 2013 Ā PERCONA Ā  SQL Antipatterns: Avoiding the Pitfalls of Database Programming by Bill Karwin Available in print, epub, mobi, pdf. Delivery options for Kindle or Dropbox. http://pragprog.com/book/bksqla/