SlideShare a Scribd company logo
Materialized Views for MySQL using Flexviews
FOSDEM 2015
Brussels, Belgium
Justin Swanhart (@jswanhart)http://flexvie.ws
Introduction
● Who am I?
● What do I do?
● What is this talk about?
What is Swanhart-Tools?
● Github repo containing multiple tools
○ Flexviews - Materialized Views for MySQL
○ Shard-Query - Sharding and parallel query (MPP)
○ utils - small utilities for MySQL
○ bcmath UDF - Arbitrary precision math UDFs
What is Flexviews?
A Materialized View toolkit with two parts:
● FlexCDC - pluggable change data capture
● Flexviews SQL API - stored routines for
managing materialized views
materialize [məˈtÉȘərÉȘəˌlaÉȘz] vb
1. (intr) to become fact; actually happen our hopes never materialized
2. to invest or become invested with a physical shape or form
3. to cause (a spirit, as of a dead person) to appear in material form (intr)
4. to take shape; become tangible after hours of discussion, the project finally began
5. Physics - to form (material particles) from energy, as in pair production
Collins English Dictionary – Complete and Unabridged © HarperCollins Publishers 1991, 1994, 1998,
2000, 2003
What are Materialized Views?
● A materialized view is similar to a regular
view
● Regular views are computed each time they
are accessed
● Materialized views are computed periodically
and the results are stored in a table
A rose by any other name
● DB2 calls them “materialized query tables”
● Microsoft SQL Server calls them “indexed
views”
● Oracle calls them “snapshots” or
“materialized views”, depending on the
version
● Vertica calls them “projections”
MySQL does not have native MVs
● Closest thing is:
CREATE TABLE 
 AS SELECT
● There is no way to automatically update the
resulting table when the original data
changes
● Flexviews fills the gap providing 3rd party
MVs
Why use Materialized Views (MV)?
● Speed!
○ A MV stores the results in a table, which can be
indexed
○ Queries can sometimes be reduced from hours
down to seconds or even milliseconds as a result
○ Great for dashboards, or cacheing important result
sets
An MV is a cache
● The results of the MV are stored in a table,
which is just a cache
● The cache gets out of data when underlying
data changes
● The view must be refreshed periodically
○ This refresh should be as efficient as possible
Two materialized view refresh algos
● COMPLETE refresh
○ Supports all SELECT, including OUTER join
○ Rebuilds whole table from scratch when the view is
refreshed (expensive)
● INCREMENTAL refresh
○ Only INNER join supported
○ Most aggregate functions supported
○ Uses the row changes collected since the last
refresh to incrementally update the table (much
Flexviews Installation
● Download Swanhart-Tools
● Setup FlexCDC
○ Requires PHP 5.3+
○ ROW based binary log (not MIXED or
STATEMENT!)
○ Full binary log images (5.6)
○ READ-COMMITTED tx_isolation (recommended)
● Setup Flexviews with setup.sql
FlexCDC - Change Data Capture
● FlexCDC uses mysqlbinlog to read the
binary log from the server
● mysqlbinlog converts RBR into “pseudo-
SBR” which FlexCDC decodes
● For each insert,update or delete, FlexCDC
writes the change history into a change log
FlexCDC - Why is it needed?
● FlexCDC reads the binary log created by the
database server.
● Why not triggers?
○ Triggers can not capture commit order
○ Triggers add a lot of overhead
○ Triggers can’t be created by stored routines
○ MySQL allows only one trigger per table
○ ...
FlexCDC captures changes
CREATE TABLE `t1` (
`c1` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CALL flexviews.create_mvlog('test','t1');
insert into test.t1 values (10);
select * from mvlog_7a52a7837df7b90fa91d3c0c3c985048;
+----------+--------+--------------+--------+------+
| dml_type | uow_id | fv$server_id | fv$gsn | c1 |
+----------+--------+--------------+--------+------+
| 1 | 7 | 1 | 2 | 10 |
+----------+--------+--------------+--------+------+
select * from flexviews.mvlogs
where table_name='t1'
***************************
table_schema: test
table_name: t1
mvlog_name:
mvlog_7a52a7837df7b90fa91d3c0c3c985048
active_flag: 1
1 row in set (0.00 sec)
FlexCDC captures changes (cont)
+----------+--------+--------------+--------+------+
| dml_type | uow_id | fv$server_id | fv$gsn | c1 |
+----------+--------+--------------+--------+------+
| 1 | 7 | 1 | 2 | 10 |
+----------+--------+--------------+--------+------+
Inserted value
Server ID of server Global Sequence Number
Transaction ID
aka Unit of Work ID
1 = INSERT
-1 = DELETE
SQL API Basics
Creating Materialized Views
● Flexviews includes a set of stored routines
called the Flexviews SQL API
● http://greenlion.github.io/swanhart-
tools/flexviews/manual.html
● SQL API is used to “build” the SQL
statement which is used to create the view
SQL API BASICS - CREATE VIEW
● Every MV has a “materialized view id”
● This ID is created by flexviews.CREATE()
● The ID is used in almost all other API calls
call flexviews.create('test','test_mv','INCREMENTAL');
set @mvid := last_insert_id();
SQL API BASICS - Add tables
Add tables using flexviews.ADD_TABLE()
call flexviews.add_table(@mvid, 'test','t1','alias1',
NULL);
Last parameter is the JOIN clause:
call flexviews.add_table(@mvid, 'test','t2','alias2',’ON
alias1.some_col = alias2.some_col’);
SQL API Basics - Add expressions
SELECT clause and WHERE clause
expressions can be added with flexviews.
ADD_EXPR()
call flexviews.add_expr(@mvid,'GROUP','c1','c1');
call flexviews.add_expr(@mvid,'COUNT','*','cnt');
SQL API BASICS - Build the view
The materialized view doesn’t exist until it is
enabled with flexviews.ENABLE()
call flexviews.enable(@mvid);
select * from test.test_mv;
+----------+------+---------+
| mview$pk | c1 | cnt |
+----------+------+---------+
| 1 | 1 | 1048576 |
| 2 | 10 | 1048576 |
+----------+------+---------+
What happens when data changes?
● The materialized view will become “stale” or
“out of date” with respect to the data in the
table
● Periodically, the MV can be “refreshed”, or
brought up to date with the changes
SQL API - Refreshing the view
Consider the following insertion into the t1
table:
insert into test.t1 values (2);
Now MV is out of date:
+----------+------+---------+
| mview$pk | c1 | cnt |
+----------+------+---------+
| 1 | 1 | 1048576 |
| 2 | 10 | 1048576 |
select c1, count(*) as cnt from t1
group by c1;
+------+---------+
| c1 | cnt |
+------+---------+
| 1 | 1048576 |
| 2 | 1 |
| 10 | 1048576 |
+------+---------+
SQL API Basics - Refresh procedure
MV are refreshed with flexviews.REFRESH()
There are two steps to refreshing a MV
1. COMPUTE changes into delta tables
2. APPLY delta changes into the view
3. BOTH (do both steps at once)
SQL API Basics - Compute Deltas
call flexviews.refresh(@mvid,'COMPUTE',NULL);
select * from test.test_mv_delta;
+----------+--------+---------+------+-----+
| dml_type | uow_id | fv$gsn | c1 | cnt |
+----------+--------+---------+------+-----+
| 1 | 39 | 2097154 | 2 | 1 |
+----------+--------+---------+------+-----+
SQL API Basics - Apply deltas
call flexviews.refresh(@mvid,'APPLY',NULL);
select * from test.test_mv;
+----------+------+---------+
| mview$pk | c1 | cnt |
+----------+------+---------+
| 1 | 1 | 1048576 |
| 2 | 10 | 1048576 |
| 4 | 2 | 1 |
+----------+------+---------+
SQL API Basics - COMPLETE views
You can create views that can’t be refreshed,
but that can use all SQL constructs, including
OUTER join.
CREATE TABLE 
 AS and RENAME TABLE
are used by Flexviews to manage the view
SQL API Basics - COMPLETE (cont)
call flexviews.create('demo','top_customers','COMPLETE');
call flexviews.set_definition(
flexviews.get_id('demo','dashboard_top_customers'),
'select customer_id,
sum(total_price) total_price,
sum(total_lines) total_lines
from demo.dashboard_customer_sales dcs
group by customer_id
order by total_price desc');
call flexviews.enable(flexviews.get_id
('demo','top_customers'));
FlexCDC Plugins
FlexCDC is pluggable
● A PHP interface is provided for FlexCDC
plugins
● Plugins receive each insert, update and
delete
● take action such as writing the changes to a
message queue
Example FlexCDC plugin*
require_once('plugin_interface.php');
class FlexCDC_Plugin implements FlexCDC_Plugin_Interface {
static function begin_trx($uow_id, $gsn,$instance) {
echo "START TRANSACTION: trx_id: $uow_id, Prev GSN: $gsnn";
}
static function insert($row, $db, $table, $trx_id, $gsn,$instance) {
echo "TRX_ID: $trx_id, Schema:$db, Table: $table, DML: INSERT, AT: $gsnn"; print_r($row);
}
static function delete($row, $db, $table, $trx_id, $gsn,$instance) {
echo "TRX_ID: $trx_id, Schema:$db, Table: $table, DML: DELETE, AT: $gsnn"; print_r($row);
}
static function update_before($row, $db, $table, $trx_id, $gsn,$instance) {
echo "TRX_ID: $trx_id, Schema:$db, Table: $table, DML: UPDATE (OLD), AT: $gsnn"; print_r($row);
}
static function update_after($row, $db, $table, $trx_id, $gsn,$instance) {
echo "TRX_ID: $trx_id, Schema:$db, Table: $table, DML: UPDATE (NEW), AT: $gsnn"; print_r($row);
}
}
* Not all functions represented
SQL API QUICK REFERENCE
● flexviews.create($schema, $table, $method);
● flexviews.get_id($schema, $table);
● flexviews.add_table($id, $schema, $table, $alias, $join_condition);
● flexviews.add_expr($id, $expr_type, $expr, $alias);
● flexviews.enable($id);
● flexviews.refresh($id, $method, $to_trx_id);
● flexviews.get_sql($id);
● flexviews.disable($id);

More Related Content

What's hot (20)

PDF
FOSDEM 2022 MySQL Devroom: MySQL 8.0 - Logical Backups, Snapshots and Point-...
Frederic Descamps
 
PDF
MySQL Group Replication
Kenny Gryp
 
PPTX
Why oracle data guard new features in oracle 18c, 19c
Satishbabu Gunukula
 
PDF
MySQL High Availability -- InnoDB Clusters
Matt Lord
 
PDF
Into TSM
Ahmed Selim
 
DOCX
MySQL_SQL_Tunning_v0.1.3.docx
NeoClova
 
PDF
MySQL Administrator 2021 - ë„€ì˜€íŽëĄœë°”
NeoClova
 
PDF
MySQL Performance - Best practices
Ted Wennmark
 
PDF
MySQL Connectors 8.0.19 & DNS SRV
Kenny Gryp
 
PDF
ProxySQL Cluster - Percona Live 2022
RenĂ© CannaĂČ
 
PDF
Advanced backup methods (Postgres@CERN)
Anastasia Lubennikova
 
PDF
Highly efficient backups with percona xtrabackup
Nilnandan Joshi
 
PPTX
Presentation upgrade, migrate & consolidate to oracle database 12c &amp...
solarisyougood
 
PDF
MySQL Database Architectures - MySQL InnoDB ClusterSet 2021-11
Kenny Gryp
 
PDF
MySQL InnoDB Cluster - Advanced Configuration & Operations
Frederic Descamps
 
PPTX
Oracle ASM Training
Vigilant Technologies
 
PDF
Parallel Replication in MySQL and MariaDB
Mydbops
 
PDF
MAA Best Practices for Oracle Database 19c
Markus Michalewicz
 
PPTX
Always on in SQL Server 2012
Fadi Abdulwahab
 
PPTX
Sql 2012 always on
dilip nayak
 
FOSDEM 2022 MySQL Devroom: MySQL 8.0 - Logical Backups, Snapshots and Point-...
Frederic Descamps
 
MySQL Group Replication
Kenny Gryp
 
Why oracle data guard new features in oracle 18c, 19c
Satishbabu Gunukula
 
MySQL High Availability -- InnoDB Clusters
Matt Lord
 
Into TSM
Ahmed Selim
 
MySQL_SQL_Tunning_v0.1.3.docx
NeoClova
 
MySQL Administrator 2021 - ë„€ì˜€íŽëĄœë°”
NeoClova
 
MySQL Performance - Best practices
Ted Wennmark
 
MySQL Connectors 8.0.19 & DNS SRV
Kenny Gryp
 
ProxySQL Cluster - Percona Live 2022
RenĂ© CannaĂČ
 
Advanced backup methods (Postgres@CERN)
Anastasia Lubennikova
 
Highly efficient backups with percona xtrabackup
Nilnandan Joshi
 
Presentation upgrade, migrate & consolidate to oracle database 12c &amp...
solarisyougood
 
MySQL Database Architectures - MySQL InnoDB ClusterSet 2021-11
Kenny Gryp
 
MySQL InnoDB Cluster - Advanced Configuration & Operations
Frederic Descamps
 
Oracle ASM Training
Vigilant Technologies
 
Parallel Replication in MySQL and MariaDB
Mydbops
 
MAA Best Practices for Oracle Database 19c
Markus Michalewicz
 
Always on in SQL Server 2012
Fadi Abdulwahab
 
Sql 2012 always on
dilip nayak
 

Viewers also liked (8)

PDF
Introduction to column oriented databases
ArangoDB Database
 
PDF
Intro to HBase Internals & Schema Design (for HBase users)
alexbaranau
 
PDF
Intro to column stores
Justin Swanhart
 
PDF
Hbase: Introduction to column oriented databases
Luis Cipriani
 
PPTX
Executing Queries on a Sharded Database
Neha Narula
 
PDF
Row or Columnar Database
Biju Nair
 
PDF
Intro to HBase
alexbaranau
 
PDF
What is Artificial Intelligence | Artificial Intelligence Tutorial For Beginn...
Edureka!
 
Introduction to column oriented databases
ArangoDB Database
 
Intro to HBase Internals & Schema Design (for HBase users)
alexbaranau
 
Intro to column stores
Justin Swanhart
 
Hbase: Introduction to column oriented databases
Luis Cipriani
 
Executing Queries on a Sharded Database
Neha Narula
 
Row or Columnar Database
Biju Nair
 
Intro to HBase
alexbaranau
 
What is Artificial Intelligence | Artificial Intelligence Tutorial For Beginn...
Edureka!
 
Ad

Similar to Flexviews materialized views for my sql (20)

PDF
Summary tables with flexviews
Justin Swanhart
 
PPT
materialized view description presentation
dbmanhero
 
PDF
Cassandra and materialized views
Grzegorz Duda
 
PPTX
Materialized Views and Secondary Indexes in Scylla: They Are finally here!
ScyllaDB
 
PDF
Cassandra Materialized Views
Carl Yeksigian
 
PPTX
Accelerating query processing
DataWorks Summit
 
PDF
OER UNIT 2-- MATERIALIZED VIEW- DATA WAREHOUSING
Girija Muscut
 
PPTX
Accelerating query processing with materialized views in Apache Hive
DataWorks Summit
 
PDF
Accelerating query processing with materialized views in Apache Hive
Sahil Takiar
 
PDF
Cassandra 3 new features 2016
Duyhai Doan
 
PPTX
3-Data_Chjgjjghgjhgjhgjhgjhontrol.pptxghgjg
messagetome133
 
PPTX
Discardable In-Memory Materialized Queries With Hadoop
Julian Hyde
 
PPTX
Discardable In-Memory Materialized Query for Hadoop
DataWorks Summit
 
PPTX
Improve data warehouse performance by preprocessing
Shehla Shoaib
 
PPTX
Cassandra 2.2 & 3.0
Victor Coustenoble
 
PDF
Cassandra 3 new features @ Geecon Krakow 2016
Duyhai Doan
 
PPTX
Ch07_SQL- The PostgreSQL Wkkkkkkkkay.pptx
MhmdMk10
 
PPT
chap 9 dbms.ppt
arjun431527
 
PPT
dbms.ppt
GeorgeSamaan9
 
PPT
dbms.ppt
KRISHNARAJ207
 
Summary tables with flexviews
Justin Swanhart
 
materialized view description presentation
dbmanhero
 
Cassandra and materialized views
Grzegorz Duda
 
Materialized Views and Secondary Indexes in Scylla: They Are finally here!
ScyllaDB
 
Cassandra Materialized Views
Carl Yeksigian
 
Accelerating query processing
DataWorks Summit
 
OER UNIT 2-- MATERIALIZED VIEW- DATA WAREHOUSING
Girija Muscut
 
Accelerating query processing with materialized views in Apache Hive
DataWorks Summit
 
Accelerating query processing with materialized views in Apache Hive
Sahil Takiar
 
Cassandra 3 new features 2016
Duyhai Doan
 
3-Data_Chjgjjghgjhgjhgjhgjhontrol.pptxghgjg
messagetome133
 
Discardable In-Memory Materialized Queries With Hadoop
Julian Hyde
 
Discardable In-Memory Materialized Query for Hadoop
DataWorks Summit
 
Improve data warehouse performance by preprocessing
Shehla Shoaib
 
Cassandra 2.2 & 3.0
Victor Coustenoble
 
Cassandra 3 new features @ Geecon Krakow 2016
Duyhai Doan
 
Ch07_SQL- The PostgreSQL Wkkkkkkkkay.pptx
MhmdMk10
 
chap 9 dbms.ppt
arjun431527
 
dbms.ppt
GeorgeSamaan9
 
dbms.ppt
KRISHNARAJ207
 
Ad

Recently uploaded (20)

PDF
Linux Certificate of Completion - LabEx Certificate
VICTOR MAESTRE RAMIREZ
 
PPTX
Hardware(Central Processing Unit ) CU and ALU
RizwanaKalsoom2
 
PPTX
Empowering Asian Contributions: The Rise of Regional User Groups in Open Sour...
Shane Coughlan
 
PPTX
ChiSquare Procedure in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PPTX
MailsDaddy Outlook OST to PST converter.pptx
abhishekdutt366
 
PDF
iTop VPN With Crack Lifetime Activation Key-CODE
utfefguu
 
PDF
Thread In Android-Mastering Concurrency for Responsive Apps.pdf
Nabin Dhakal
 
PDF
Build It, Buy It, or Already Got It? Make Smarter Martech Decisions
bbedford2
 
PDF
Mobile CMMS Solutions Empowering the Frontline Workforce
CryotosCMMSSoftware
 
PPTX
Java Native Memory Leaks: The Hidden Villain Behind JVM Performance Issues
Tier1 app
 
PDF
Unlock Efficiency with Insurance Policy Administration Systems
Insurance Tech Services
 
PDF
Online Queue Management System for Public Service Offices in Nepal [Focused i...
Rishab Acharya
 
PPTX
Fundamentals_of_Microservices_Architecture.pptx
MuhammadUzair504018
 
PDF
SciPy 2025 - Packaging a Scientific Python Project
Henry Schreiner
 
PPTX
Agentic Automation Journey Session 1/5: Context Grounding and Autopilot for E...
klpathrudu
 
PPTX
Coefficient of Variance in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PDF
Odoo CRM vs Zoho CRM: Honest Comparison 2025
Odiware Technologies Private Limited
 
PDF
SAP Firmaya İade ABAB Kodları - ABAB ile yazılmıl hazır kod örneği
Salih KĂŒĂ§ĂŒk
 
PPTX
Home Care Tools: Benefits, features and more
Third Rock Techkno
 
PPTX
Migrating Millions of Users with Debezium, Apache Kafka, and an Acyclic Synch...
MD Sayem Ahmed
 
Linux Certificate of Completion - LabEx Certificate
VICTOR MAESTRE RAMIREZ
 
Hardware(Central Processing Unit ) CU and ALU
RizwanaKalsoom2
 
Empowering Asian Contributions: The Rise of Regional User Groups in Open Sour...
Shane Coughlan
 
ChiSquare Procedure in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
MailsDaddy Outlook OST to PST converter.pptx
abhishekdutt366
 
iTop VPN With Crack Lifetime Activation Key-CODE
utfefguu
 
Thread In Android-Mastering Concurrency for Responsive Apps.pdf
Nabin Dhakal
 
Build It, Buy It, or Already Got It? Make Smarter Martech Decisions
bbedford2
 
Mobile CMMS Solutions Empowering the Frontline Workforce
CryotosCMMSSoftware
 
Java Native Memory Leaks: The Hidden Villain Behind JVM Performance Issues
Tier1 app
 
Unlock Efficiency with Insurance Policy Administration Systems
Insurance Tech Services
 
Online Queue Management System for Public Service Offices in Nepal [Focused i...
Rishab Acharya
 
Fundamentals_of_Microservices_Architecture.pptx
MuhammadUzair504018
 
SciPy 2025 - Packaging a Scientific Python Project
Henry Schreiner
 
Agentic Automation Journey Session 1/5: Context Grounding and Autopilot for E...
klpathrudu
 
Coefficient of Variance in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
Odoo CRM vs Zoho CRM: Honest Comparison 2025
Odiware Technologies Private Limited
 
SAP Firmaya İade ABAB Kodları - ABAB ile yazılmıl hazır kod örneği
Salih KĂŒĂ§ĂŒk
 
Home Care Tools: Benefits, features and more
Third Rock Techkno
 
Migrating Millions of Users with Debezium, Apache Kafka, and an Acyclic Synch...
MD Sayem Ahmed
 

Flexviews materialized views for my sql

  • 1. Materialized Views for MySQL using Flexviews FOSDEM 2015 Brussels, Belgium Justin Swanhart (@jswanhart)http://flexvie.ws
  • 2. Introduction ● Who am I? ● What do I do? ● What is this talk about?
  • 3. What is Swanhart-Tools? ● Github repo containing multiple tools ○ Flexviews - Materialized Views for MySQL ○ Shard-Query - Sharding and parallel query (MPP) ○ utils - small utilities for MySQL ○ bcmath UDF - Arbitrary precision math UDFs
  • 4. What is Flexviews? A Materialized View toolkit with two parts: ● FlexCDC - pluggable change data capture ● Flexviews SQL API - stored routines for managing materialized views
  • 5. materialize [məˈtÉȘərÉȘəˌlaÉȘz] vb 1. (intr) to become fact; actually happen our hopes never materialized 2. to invest or become invested with a physical shape or form 3. to cause (a spirit, as of a dead person) to appear in material form (intr) 4. to take shape; become tangible after hours of discussion, the project finally began 5. Physics - to form (material particles) from energy, as in pair production Collins English Dictionary – Complete and Unabridged © HarperCollins Publishers 1991, 1994, 1998, 2000, 2003
  • 6. What are Materialized Views? ● A materialized view is similar to a regular view ● Regular views are computed each time they are accessed ● Materialized views are computed periodically and the results are stored in a table
  • 7. A rose by any other name ● DB2 calls them “materialized query tables” ● Microsoft SQL Server calls them “indexed views” ● Oracle calls them “snapshots” or “materialized views”, depending on the version ● Vertica calls them “projections”
  • 8. MySQL does not have native MVs ● Closest thing is: CREATE TABLE 
 AS SELECT ● There is no way to automatically update the resulting table when the original data changes ● Flexviews fills the gap providing 3rd party MVs
  • 9. Why use Materialized Views (MV)? ● Speed! ○ A MV stores the results in a table, which can be indexed ○ Queries can sometimes be reduced from hours down to seconds or even milliseconds as a result ○ Great for dashboards, or cacheing important result sets
  • 10. An MV is a cache ● The results of the MV are stored in a table, which is just a cache ● The cache gets out of data when underlying data changes ● The view must be refreshed periodically ○ This refresh should be as efficient as possible
  • 11. Two materialized view refresh algos ● COMPLETE refresh ○ Supports all SELECT, including OUTER join ○ Rebuilds whole table from scratch when the view is refreshed (expensive) ● INCREMENTAL refresh ○ Only INNER join supported ○ Most aggregate functions supported ○ Uses the row changes collected since the last refresh to incrementally update the table (much
  • 12. Flexviews Installation ● Download Swanhart-Tools ● Setup FlexCDC ○ Requires PHP 5.3+ ○ ROW based binary log (not MIXED or STATEMENT!) ○ Full binary log images (5.6) ○ READ-COMMITTED tx_isolation (recommended) ● Setup Flexviews with setup.sql
  • 13. FlexCDC - Change Data Capture ● FlexCDC uses mysqlbinlog to read the binary log from the server ● mysqlbinlog converts RBR into “pseudo- SBR” which FlexCDC decodes ● For each insert,update or delete, FlexCDC writes the change history into a change log
  • 14. FlexCDC - Why is it needed? ● FlexCDC reads the binary log created by the database server. ● Why not triggers? ○ Triggers can not capture commit order ○ Triggers add a lot of overhead ○ Triggers can’t be created by stored routines ○ MySQL allows only one trigger per table ○ ...
  • 15. FlexCDC captures changes CREATE TABLE `t1` ( `c1` int(11) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1; CALL flexviews.create_mvlog('test','t1'); insert into test.t1 values (10); select * from mvlog_7a52a7837df7b90fa91d3c0c3c985048; +----------+--------+--------------+--------+------+ | dml_type | uow_id | fv$server_id | fv$gsn | c1 | +----------+--------+--------------+--------+------+ | 1 | 7 | 1 | 2 | 10 | +----------+--------+--------------+--------+------+ select * from flexviews.mvlogs where table_name='t1' *************************** table_schema: test table_name: t1 mvlog_name: mvlog_7a52a7837df7b90fa91d3c0c3c985048 active_flag: 1 1 row in set (0.00 sec)
  • 16. FlexCDC captures changes (cont) +----------+--------+--------------+--------+------+ | dml_type | uow_id | fv$server_id | fv$gsn | c1 | +----------+--------+--------------+--------+------+ | 1 | 7 | 1 | 2 | 10 | +----------+--------+--------------+--------+------+ Inserted value Server ID of server Global Sequence Number Transaction ID aka Unit of Work ID 1 = INSERT -1 = DELETE
  • 18. Creating Materialized Views ● Flexviews includes a set of stored routines called the Flexviews SQL API ● http://greenlion.github.io/swanhart- tools/flexviews/manual.html ● SQL API is used to “build” the SQL statement which is used to create the view
  • 19. SQL API BASICS - CREATE VIEW ● Every MV has a “materialized view id” ● This ID is created by flexviews.CREATE() ● The ID is used in almost all other API calls call flexviews.create('test','test_mv','INCREMENTAL'); set @mvid := last_insert_id();
  • 20. SQL API BASICS - Add tables Add tables using flexviews.ADD_TABLE() call flexviews.add_table(@mvid, 'test','t1','alias1', NULL); Last parameter is the JOIN clause: call flexviews.add_table(@mvid, 'test','t2','alias2',’ON alias1.some_col = alias2.some_col’);
  • 21. SQL API Basics - Add expressions SELECT clause and WHERE clause expressions can be added with flexviews. ADD_EXPR() call flexviews.add_expr(@mvid,'GROUP','c1','c1'); call flexviews.add_expr(@mvid,'COUNT','*','cnt');
  • 22. SQL API BASICS - Build the view The materialized view doesn’t exist until it is enabled with flexviews.ENABLE() call flexviews.enable(@mvid); select * from test.test_mv; +----------+------+---------+ | mview$pk | c1 | cnt | +----------+------+---------+ | 1 | 1 | 1048576 | | 2 | 10 | 1048576 | +----------+------+---------+
  • 23. What happens when data changes? ● The materialized view will become “stale” or “out of date” with respect to the data in the table ● Periodically, the MV can be “refreshed”, or brought up to date with the changes
  • 24. SQL API - Refreshing the view Consider the following insertion into the t1 table: insert into test.t1 values (2); Now MV is out of date: +----------+------+---------+ | mview$pk | c1 | cnt | +----------+------+---------+ | 1 | 1 | 1048576 | | 2 | 10 | 1048576 | select c1, count(*) as cnt from t1 group by c1; +------+---------+ | c1 | cnt | +------+---------+ | 1 | 1048576 | | 2 | 1 | | 10 | 1048576 | +------+---------+
  • 25. SQL API Basics - Refresh procedure MV are refreshed with flexviews.REFRESH() There are two steps to refreshing a MV 1. COMPUTE changes into delta tables 2. APPLY delta changes into the view 3. BOTH (do both steps at once)
  • 26. SQL API Basics - Compute Deltas call flexviews.refresh(@mvid,'COMPUTE',NULL); select * from test.test_mv_delta; +----------+--------+---------+------+-----+ | dml_type | uow_id | fv$gsn | c1 | cnt | +----------+--------+---------+------+-----+ | 1 | 39 | 2097154 | 2 | 1 | +----------+--------+---------+------+-----+
  • 27. SQL API Basics - Apply deltas call flexviews.refresh(@mvid,'APPLY',NULL); select * from test.test_mv; +----------+------+---------+ | mview$pk | c1 | cnt | +----------+------+---------+ | 1 | 1 | 1048576 | | 2 | 10 | 1048576 | | 4 | 2 | 1 | +----------+------+---------+
  • 28. SQL API Basics - COMPLETE views You can create views that can’t be refreshed, but that can use all SQL constructs, including OUTER join. CREATE TABLE 
 AS and RENAME TABLE are used by Flexviews to manage the view
  • 29. SQL API Basics - COMPLETE (cont) call flexviews.create('demo','top_customers','COMPLETE'); call flexviews.set_definition( flexviews.get_id('demo','dashboard_top_customers'), 'select customer_id, sum(total_price) total_price, sum(total_lines) total_lines from demo.dashboard_customer_sales dcs group by customer_id order by total_price desc'); call flexviews.enable(flexviews.get_id ('demo','top_customers'));
  • 31. FlexCDC is pluggable ● A PHP interface is provided for FlexCDC plugins ● Plugins receive each insert, update and delete ● take action such as writing the changes to a message queue
  • 32. Example FlexCDC plugin* require_once('plugin_interface.php'); class FlexCDC_Plugin implements FlexCDC_Plugin_Interface { static function begin_trx($uow_id, $gsn,$instance) { echo "START TRANSACTION: trx_id: $uow_id, Prev GSN: $gsnn"; } static function insert($row, $db, $table, $trx_id, $gsn,$instance) { echo "TRX_ID: $trx_id, Schema:$db, Table: $table, DML: INSERT, AT: $gsnn"; print_r($row); } static function delete($row, $db, $table, $trx_id, $gsn,$instance) { echo "TRX_ID: $trx_id, Schema:$db, Table: $table, DML: DELETE, AT: $gsnn"; print_r($row); } static function update_before($row, $db, $table, $trx_id, $gsn,$instance) { echo "TRX_ID: $trx_id, Schema:$db, Table: $table, DML: UPDATE (OLD), AT: $gsnn"; print_r($row); } static function update_after($row, $db, $table, $trx_id, $gsn,$instance) { echo "TRX_ID: $trx_id, Schema:$db, Table: $table, DML: UPDATE (NEW), AT: $gsnn"; print_r($row); } } * Not all functions represented
  • 33. SQL API QUICK REFERENCE ● flexviews.create($schema, $table, $method); ● flexviews.get_id($schema, $table); ● flexviews.add_table($id, $schema, $table, $alias, $join_condition); ● flexviews.add_expr($id, $expr_type, $expr, $alias); ● flexviews.enable($id); ● flexviews.refresh($id, $method, $to_trx_id); ● flexviews.get_sql($id); ● flexviews.disable($id);