Professional Documents
Culture Documents
Migration From Oracle To MySQL - An NPR Case Study Presentation
Migration From Oracle To MySQL - An NPR Case Study Presentation
npr.org
Overview
Background
Database Architecture
SQL Differences
Concurrency Issues
Useful MySQL Tools
Encoding Gotchas
Background
NPR (National Public Radio)
Leading producer and distributor of radio programming
All Things Considered, Morning Edition, Fresh Air, Wait, Wait,
Don’t Tell Me, etc.
Broadcasted on over 800 local radio stations nationwide
NPR Digital Media
Website (NPR.org) with audio content from radio programs
Web-Only content including blogs, slideshows, editorial columns
About 250 produced podcasts, with over 600 in directory
Mobile apps and sites
Syndication
High-Level System Architecture
Limitations of the Oracle Architecture
Reached capacity of single system to support our
load
Replication outside our budget
Databases crashes were becoming frequent
Database Architecture Goals
Redundancy
Scalability
Load balancing
Separation of concerns
Better security
High-Level System Architecture
Database Architecture
Content
Mgmt System Main
RO slave
Main Web Servers
• Read and updated
InnoDBonly by our website
• Low resource contention • Updated
Mainby a nightly script
• Small tables or log tables • Read-only
RO slave by our Content
• Short Transactions Management System
AMG • Need fast full text queries
STATIONS PUBLIC
MyISAM (replacing Oracle Text)InnoDB InnoDB
• Large tables
• Isolation
Updatedbyfrom
• Updated bya our
main
Content
website
quarterly Management System
script
• Transaction
Read-onlyfrom
• Read-only byOriented
our
ourwebservers
website
• Horizontally
Resource
• Some Contention
log type scalable
information written Scripts Backup
• Highly
• Low Normalized
resource contention RO slave
• No transactions
Issues When Converting SQL
MySQL is case sensitive
Oracle outer join syntax (+) -> OUTER JOIN clause
Oracle returns a zero to indicate zero rows updated –
MySQL returns TRUE (1) to indicate it successfully
updated 0 rows
MySQL sorts null to the top, Oracle sorts null to the
bottom
Use “order by – colName desc” for sorting asc with nulls at
bottom
MySQL has Limit clause – YAY!
Replacing Oracle Sequences
Initialize a table with a single row:
CREATE TABLE our_seq (
id INT NOT NULL
);
INSERT INTO our_seq (id) VALUES (120000000);
Then use the whole rownum workaround described above to get a unique value for
each row:
INSERT INTO my_table (my_primary_id . . . ) SELECT
@lastSeqId - (@rownum:=@rownum+1), . . . FROM (SELECT
@rownum:=-1) r, . . .
Converting Functions
NVL() -> IFNULL() or COALESCE()
DECODE() -> CASE() or IF()
Concatenating strings || -> CONCAT()
‘test’ || null returns ‘test’ in Oracle
CONCAT(‘test’,null) returns null in MySQL
LTRIM and RTRIM -> TRIM()
INSTR() works differently.
Use LOCATE() for Oracle’s INSTR() with occurrences = 1.
SUBSTRING_INDEX() and REVERSE() might also work.
Converting Dates
sysdate -> now()
Adding or subtracting
In Oracle “– 1” subtracts a day
In MySQL “- 1” subtracts a milisecond – must use
“interval”
TRUNC() -> DATE()
TO_DATE and TO_CHAR -> STR_TO_DATE and
DATE_FORMAT
Update Differences
You can't update a table that is used in the WHERE
clause for the update (usually in an "EXISTS" or a
subselect) in mysql.
UPDATE tableA SET tableA.col1 = NULL
WHERE tableA.col2 IN
(SELECT tableA.col2
FROM tableA A2, tableB
WHERE tableB.col3 = A2.col3 AND
tableB.col4 = 123456);
In mysql, use
show engine innodb status\G;
Useful for contention and locking issues
Query Profiling
Try the Query Profiler with Explain Plan when
debugging slow queries
http://dev.mysql.com/tech-resources/articles/using-new-
query-profiler.html
Concurrency Solution
Tuning our SQL and our server configuration helped