SlideShare a Scribd company logo
common_schema2.2
D B A ' s f r a m e w o r k f o r M y S Q L
S h l o m i N o a c h
h t t p : / / o p e n a r k . o r g
Copyright © 2014, Shlomi Noach
common_schema
D B A ' s f r a m e w o r k f o r M y S Q L
common_schema2.2
D B A ' s f r a m e w o r k f o r M y S Q L
 About
 Views & eval()
 Routines
 QueryScript
 rdebug
D B A ' s f r a m e w o r k f o r M y S Q L
common_schema
Copyright © 2013, Shlomi Noach
About myself
 Shlomi Noach, geek
 Software engineer, DBA
 Author of open source tools
common_schema, openark kit,
propagator, others.
 Blog on all things MySQL
http://openark.org
 Employed by Outbrain Inc.
D B A ' s f r a m e w o r k f o r M y S Q L
common_schema
Copyright © 2013, Shlomi Noach
About common_schema
 common_schema is an open source
project, licensed under the GPL License.
 Authored by Shlomi Noach
http://openark.org
 Major contributions by Roland Bouman
http://rpbouman.blogspot.com
 Several contributions & suggestions by the
community
(Thanks!)
D B A ' s f r a m e w o r k f o r M y S Q L
common_schema
Copyright © 2013, Shlomi Noach
About common_schema
 common_schema is a framework which
includes:
– Views: analytics, security, action-taking, ...
– Routines: text, temporal, process, security, ...
– QueryScript: an SQL oriented scripting
language & interpreter
– rdebug: debugger and debugging API for
MySQL stored routines (alpha).
D B A ' s f r a m e w o r k f o r M y S Q L
common_schema
Copyright © 2013, Shlomi Noach
About common_schema
 It is a schema that lies next to
INFORMATION_SCHEMA.
 It lies completely within the MySQL server,
and does not require external packages or
dependencies. No Perl scripts nor UDFs or
plugins.
D B A ' s f r a m e w o r k f o r M y S Q L
common_schema
Copyright © 2013, Shlomi Noach
Getting & Installing
 common_schema distribution is a SQL file.
 Currently hosted on Google Code:
http://code.google.com/p/common-schema/
 Install by importing SQL file into MySQL:
bash$ mysql < /tmp/common_schema-2.2.sql
complete
- Base components: installed
- InnoDB Plugin components: installed
- Percona Server components: not installed
- TokuDB components: partial install: 1/2
Installation complete. Thank you for using
common_schema!
D B A ' s f r a m e w o r k f o r M y S Q L
common_schema
Copyright © 2013, Shlomi Noach
Views
 Views providing non-trivial information
about your table design, keys, grants,
processes, transactions, locks, ...
 While INFORMATION_SCHEMA provides
with complete info, it is ofter difficult to
aggregate. It is sometimes too normalized,
and at other times too de-normalized.
 Some metadata is simply not available in
INFORMATION_SCHEMA.
D B A ' s f r a m e w o r k f o r M y S Q L
common_schema
Copyright © 2013, Shlomi Noach
Views
 common_schema's views will present with
valuable knowledge
 And will typically offer recommendations
or possible actions based on that
knowledge.
 common_schema also provides the
mechanism to apply those
recommendations.
D B A ' s f r a m e w o r k f o r M y S Q L
common_schema
Copyright © 2013, Shlomi Noach
Views: redundant_keys
 Find duplicate/redundant keys in your
schema. Recommend DROP statements.
http://common-schema.googlecode.com/svn/trunk/common_schema/doc/html/redundant_keys.html
– Similar to pt-duplicate-key-checker
mysql> select * from redundant_keys
where table_schema='sakila'G
table_schema: sakila
table_name: rental
redundant_index_name: rental_date_2
redundant_index_columns: rental_date
redundant_index_non_unique: 1
dominant_index_name: rental_date
dominant_index_columns: rental_date, inventory_id,
customer_id
dominant_index_non_unique: 0
sql_drop_index: ALTER TABLE `sakila`.`rental`
DROP INDEX `rental_date_2`
D B A ' s f r a m e w o r k f o r M y S Q L
common_schema
Copyright © 2013, Shlomi Noach
Views: sql_range_partitions
 Analyze your range partitioned tables.
Offers the ADD/REORGANIZE PARTITION
statements to roll partitions:
mysql> create table test.report … partition by range (…) ;
mysql> select * from sql_range_partitions where
table_name='report' G
table_schema: test
table_name: report
count_partitions: 7
sql_drop_first_partition: alter table `test`.`report`
drop partition `p0`
sql_add_next_partition: alter table `test`.`report`
reorganize partition `p6` into (
partition `p_20090701000000` values less than
(1246395600) /* 2009-07-01 00:00:00 */ ,
partition p_maxvalue values less than MAXVALUE
)
D B A ' s f r a m e w o r k f o r M y S Q L
common_schema
Copyright © 2013, Shlomi Noach
Views: sql grants
 Provide SQL based access to user accounts
and their grants.
 Offers the relevant GRANT, REVOKE and
DROP statements for such grants:
GRANTEE: 'world_user'@'localhost'
user: world_user
host: localhost
priv_level: `world`.*
priv_level_name: schema
current_privileges: INSERT, SELECT, UPDATE
sql_grant: GRANT INSERT, SELECT, UPDATE ON
`world`.* TO 'world_user'@'%'
sql_revoke: REVOKE INSERT, SELECT, UPDATE ON
`world`.* FROM 'world_user'@'%'
D B A ' s f r a m e w o r k f o r M y S Q L
common_schema
Copyright © 2013, Shlomi Noach
SQL generation & eval()
 Views above present with “SQL columns”,
offering a statement to execute.
 This is at the heart of common_schema's
views, and is part of the server-side
mechanism the framework strongly
supports.
 The eval() routine accepts such SQL
columns and executes (evaluates) them.
D B A ' s f r a m e w o r k f o r M y S Q L
common_schema
Copyright © 2013, Shlomi Noach
eval()
 Accepts a query returning a text column.
Column data is expected to be SQL
statements to be executed.
mysql> call eval("
SELECT sql_add_next_partition
FROM sql_range_partitions
WHERE table_schema='webprod'");
Query OK, 0 rows affected
-- A new partition has just been added on
-- all range-partitioned tables in `webprod`
D B A ' s f r a m e w o r k f o r M y S Q L
common_schema
Copyright © 2013, Shlomi Noach
eval()
KILL QUERY 123
KILL QUERY 129
KILL QUERY 598
KILL QUERY 620
KILL QUERY 702
Query
eval()
Execute query
Execute statements
D B A ' s f r a m e w o r k f o r M y S Q L
common_schema
Copyright © 2013, Shlomi Noach
Views: innodb_transactions
 Which transactions are running?
– For how long?
– How long are they being idle? Locked?
– What queries are they issuing?
 Recommend KILL, apply with eval()
mysql> call eval("
SELECT sql_kill_query
FROM innodb_transactions
WHERE trx_idle_seconds >= 30
");
-- All idle transactions have just been killed
D B A ' s f r a m e w o r k f o r M y S Q L
common_schema
Copyright © 2013, Shlomi Noach
Views: others
 processlist_grantees: identifies the user
connection with its associated account
(missing info in MySQL)
 auto_increment_columns: find “free space”
or “usage” for AUTO_INCREMENT values
 slave_status: get Seconds_behind_master
via real SQL query
 last_query_profiling :aggregated profile for
last executed query
D B A ' s f r a m e w o r k f o r M y S Q L
common_schema
Copyright © 2013, Shlomi Noach
Views: processlist_grantees
mysql> select * from processlist_granteesG
ID: 41165491
USER: dbuser
HOST: web00.myapp:40049
DB: profile
COMMAND: Query
TIME: 0
STATE: updating
INFO: DELETE FROM locks WHERE id =
'helper' AND dt < '2013-04-17 15:09:50'
GRANTEE: 'dbuser'@'%.myapp'
grantee_user: dbuser
grantee_host: %.myapp
is_super: 0
is_repl: 0
is_current: 0
sql_kill_query: KILL QUERY 41165491
sql_kill_connection: KILL 41165491
D B A ' s f r a m e w o r k f o r M y S Q L
common_schema
Copyright © 2013, Shlomi Noach
Views: others
 sql_accounts: block/release accounts
 similar_grants: detect security roles
 global_status_diff_nonzero: detect status
change
 innodb_locked_transactions: who locks
who, on which query? Recommend KILL
 more...
http://common-schema.googlecode.com/svn/trunk/common_schema/doc/html/process_views.html
http://common-schema.googlecode.com/svn/trunk/common_schema/doc/html/innodb_plugin_views.html
http://common-schema.googlecode.com/svn/trunk/common_schema/doc/html/security_views.html
http://common-schema.googlecode.com/svn/trunk/common_schema/doc/html/schema_analysis_views.html
D B A ' s f r a m e w o r k f o r M y S Q L
common_schema
Copyright © 2013, Shlomi Noach
Routines
 common_schema offers more than 60
useful stored routines, part of the
framework's function library.
 From text parsing & manipulation,
through process diagnostics, query
analysis & dynamic execution, to security
routines, the function library extends and
complements MySQL's own functions.
http://common-schema.googlecode.com/svn/trunk/common_schema/doc/html/execution_routines.html
http://common-schema.googlecode.com/svn/trunk/common_schema/doc/html/text_routines.html
http://common-schema.googlecode.com/svn/trunk/common_schema/doc/html/security_routines.html
http://common-schema.googlecode.com/svn/trunk/common_schema/doc/html/process_routines.html
D B A ' s f r a m e w o r k f o r M y S Q L
common_schema
Copyright © 2013, Shlomi Noach
Routines: security
 killall() kills connections by matching text
with user, host or grantee.
 security_audit() audits server's privileges
tables and configuration to detect such
threats as empty or duplicate passwords,
excessive privileges, permissive hosts etc.
 duplicate_grantee(): duplicates an account
mysql> call killall('analytics');
mysql> call killall('localhost');
D B A ' s f r a m e w o r k f o r M y S Q L
common_schema
Copyright © 2013, Shlomi Noach
Routines: text
 extract_json_value(): Extract value from
JSON notation via Xpath.
 replace_sections(): Search and replace text
appearing between section.
 get_num_tokens(): Return number of
tokens in delimited text.
mysql> select common_schema.get_num_tokens(
'the quick brown fox', ' ');
=> 4
mysql> select common_schema.split_token(
'the quick brown fox', ' ', 3);
=> 'brown'
D B A ' s f r a m e w o r k f o r M y S Q L
common_schema
Copyright © 2013, Shlomi Noach
Routines: text
mysql> set @json := '{
"menu": {
"id": "file",
"value": "File",
"popup": {
"menuitem": [
{"value": "New", "onclick": "create()"},
{"value": "Open", "onclick": "open()"},
{"value": "Close", "onclick": "close()"}
]
}
}
}';
mysql> select extract_json_value(@json, '//id') AS result;
+--------+
| result |
+--------+
| file |
+--------+
D B A ' s f r a m e w o r k f o r M y S Q L
common_schema
Copyright © 2013, Shlomi Noach
Routines: general
 query_checksum(): Checksum the result
set of a query.
 crc64(): Return a 64 bit CRC of given input,
as unsigned big integer.
 random_hash(): Return a 64 bit CRC of
given input, as unsigned big integer.
mysql> call query_checksum(
'select code, name from world.Country order by code');
+----------------------------------+
| checksum |
+----------------------------------+
| 79221e8d040e33cd3262f1680b4c8e54 |
+----------------------------------+
D B A ' s f r a m e w o r k f o r M y S Q L
common_schema
Copyright © 2013, Shlomi Noach
Routines: SQL & execution
 eval() evaluates the queries generated by a
given query.
 exec(), exec_file() dynamically executes a
given query or semicolon delimited list of
queries.
 run(), run_file() execute QueryScript code.
mysql> call exec('
CREATE TABLE test.t(id INT);
INSERT INTO test.t VALUES (2),(3),(5);
');
D B A ' s f r a m e w o r k f o r M y S Q L
common_schema
Copyright © 2013, Shlomi Noach
QueryScript
 A SQL oriented scripting language, offering
tight integration with SQL commands, easy
and familiar control flow syntax and high
level abstraction of complex tasks.
 common_schema implements QueryScript
via interpreter, based on stored routines.
 This makes QueryScript suitable for
administration and bulk tasks, not for
OLTP tasks.
http://common-schema.googlecode.com/svn/trunk/common_schema/doc/html/query_script.html
D B A ' s f r a m e w o r k f o r M y S Q L
common_schema
Copyright © 2013, Shlomi Noach
Why QueryScript?
 Stored routine programming is a pain:
– Requires one to actually store the routine
within the schema: can't just run something.
– Syntax is cumbersome (ANSI:SQL).
– Does not offer deeper insight into MySQL's
limitations and bottlenecks.
– Does not provide with syntax for oh-so-
common tasks
– Verbose. Can't see the forest for the trees.
D B A ' s f r a m e w o r k f o r M y S Q L
common_schema
Copyright © 2013, Shlomi Noach
Compare: stored routine
DELIMITER $$
DROP PROCEDURE IF EXISTS some_proc $$
CREATE PROCEDURE some_proc()
READS SQL DATA
SQL SECURITY INVOKER
begin
declare some_id bigint unsigned default null;
declare done tinyint default 0;
declare my_cursor cursor for SELECT some_id FROM some_table;
declare continue handler for NOT FOUND set done = 1;
open my_cursor;
read_loop: loop
fetch my_cursor into some_id;
if done then
leave read_loop;
end if;
-- do something with some_id
end loop;
close my_cursor;
end $$
DELIMITER ;
D B A ' s f r a m e w o r k f o r M y S Q L
common_schema
Copyright © 2013, Shlomi Noach
Compare: QueryScript
set @script := '
foreach ($some_id: select some_id from some_table)
{
-- do something with $some_id
}
';
call run(@script);
 Significantly less overhead
 Familiar C-family syntax
 No need to store the code in schema
 Can execute script directly from file
call run('/path/to/script.qs');
D B A ' s f r a m e w o r k f o r M y S Q L
common_schema
Copyright © 2013, Shlomi Noach
QueryScript: variables
 Create variables which are known to
initialize as NULL and are known to clean
up as they exit scope.
 Can be expanded and used where MySQL
does not allow variables.
var $count := 20;
var $pop := 1000000;
var $tbl := 'City';
select * from world.:${tbl}
where population >= $pop limit :${count};
D B A ' s f r a m e w o r k f o r M y S Q L
common_schema
Copyright © 2013, Shlomi Noach
QueryScript: conditions
 Familiar if, while or loop-while statements.
 But conditions are also tightly integrated
with SQL, and so queries make for valid
conditions.
if (@val > 3) {
pass;
}
while (delete from world.Country
where Continent = 'Asia' limit 10)
{
throttle 2;
}
D B A ' s f r a m e w o r k f o r M y S Q L
common_schema
Copyright © 2013, Shlomi Noach
QueryScript: foreach
 A sophisticated looping device, allowing
iteration over queries, sets, numbers
range, databases, tables...
 But also acknowledges MySQL limitations
and provides with a safer access method.
 Table iteration uses INFORMATION_SCHEMA
optimizations to avoid excessive locks:
foreach($tbl, $scm: table like wp_posts)
alter table :${scm}.:${tbl} add column
post_geo_location VARCHAR(128);
D B A ' s f r a m e w o r k f o r M y S Q L
common_schema
Copyright © 2013, Shlomi Noach
QueryScript: foreach
foreach($year: 2001:2009)
delete from sakila.rental where
rental_date >= CONCAT($year, '-07-01')
and rental_date < CONCAT($year, '-09-01');
foreach($shard: {US, GB, Japan, FRA})
create database dbshard_:${shard};
foreach($scm: schema like wp%) {
create table :$scm.wp_likes(
id int, data VARCHAR(128));
}
foreach ($some_id: select some_id
from some_table) {...
}
D B A ' s f r a m e w o r k f o r M y S Q L
common_schema
Copyright © 2013, Shlomi Noach
QueryScript: split
 Automagically breaks a bulk operation into
smaller chunks.
 Supports DELETE, UPDATE,
INSERT...SELECT, REPLACE...SELECT
 Supports single and multi table statements
split (delete from sakila.rental where
rental_date < NOW() - interval 5 year);
split (insert into world.City_duplicate
select * from world.City) {
throttle 1;
}
D B A ' s f r a m e w o r k f o r M y S Q L
common_schema
Copyright © 2013, Shlomi Noach
QueryScript: split
split(update sakila.film_actor
set last_update=now() where actor_id != 4)
{ select $split_columns as columns,
$split_range_start as range_start,
$split_range_end as range_end,
$split_total_rowcount as total;
}
+----------------------+-------------+------------+-------+
| columns | range_start | range_end | total |
+----------------------+-------------+------------+-------+
| `actor_id`,`film_id` | '1','1' | '39','293' | 978 |
+----------------------+-------------+------------+-------+
+----------------------+-------------+------------+-------+
| columns | range_start | range_end | total |
+----------------------+-------------+------------+-------+
| `actor_id`,`film_id` | '39','293' | '76','234' | 1978 |
+----------------------+-------------+------------+-------+
+----------------------+-------------+-------------+-------+
| columns | range_start | range_end | total |
+----------------------+-------------+-------------+-------+
| `actor_id`,`film_id` | '76','234' | '110','513' | 2978 |
+----------------------+-------------+-------------+-------+
D B A ' s f r a m e w o r k f o r M y S Q L
common_schema
Copyright © 2013, Shlomi Noach
QueryScript: split, example
 Real world use case: need to copy
120,000,000 rows belonging to a specific
partition from range-partitioned table
2013-02
2013-03
2013-04
2013-05
2013-06
2013-05
D B A ' s f r a m e w o r k f o r M y S Q L
common_schema
Copyright © 2013, Shlomi Noach
QueryScript: split, example
split (
{ index: PRIMARY, start: '2013-05-01',
step: 20000 } :
insert into one_month_sample
select * from
table_with_many_month_partitions
where entry_date < DATE('2013-06-01')
) {
if ($split_range_start >= DATE('2013-06-01'))
Break;
throttle 1;
}
 We provide hints to the split statement:
choosing index, start position, chunk size.
 Terminate when out of range.
D B A ' s f r a m e w o r k f o r M y S Q L
common_schema
Copyright © 2013, Shlomi Noach
QueryScript: example
 Create shards & tables, and copy row data:
-- iterate shards
foreach ($shard: {US, GB, Japan, FRA}) {
create database dbshard_:${shard};
-- iterate tables
foreach ($tbl: table in original_shard) {
create table dbshard_:${shard}.:${tbl}
like original_shard.:$tbl;
-- copy table data
split (insert into dbshard_:${shard}.:${tbl}
select * from original_shard.:$tbl) {
-- show progress
select $shard, $tbl, $split_total_rowcount;
throttle 1;
}
}
}
D B A ' s f r a m e w o r k f o r M y S Q L
common_schema
Copyright © 2013, Shlomi Noach
QueryScript: example
 Protect against malfunctioning 3rd party
locking code:
call common_schema.run("
while( 1 ) {
sleep 5;
try {
eval select
min(sql_kill_blocking_connection)
from
common_schema.innodb_locked_transactions
group by
locking_trx_mysql_thread_id
having count(*) > 10;
} catch {pass;}
}
");
D B A ' s f r a m e w o r k f o r M y S Q L
common_schema
Copyright © 2013, Shlomi Noach
QueryScript: example
 Create 10 years worth of partitions:
CREATE TABLE my_document (
my_document_id int unsigned NOT NULL AUTO_INCREMENT,
creation_date DATETIME NOT NULL,
data VARCHAR(127),
PRIMARY KEY (my_document_id, creation_date)
)
PARTITION BY RANGE (to_days(creation_date))
(PARTITION p_2006_01_01 VALUES LESS THAN (732677),
PARTITION p_2006_02_01 VALUES LESS THAN (732708),
PARTITION p_2006_03_01 VALUES LESS THAN (732736),
PARTITION p_2006_04_01 VALUES LESS THAN (732767)
);
call common_schema.run("
foreach($i : 1:240) {
eval select sql_add_next_partition
from sql_range_partitions
where table_name='my_document';
}
");
D B A ' s f r a m e w o r k f o r M y S Q L
common_schema
Copyright © 2013, Shlomi Noach
QueryScript: more goodies
 throttle statement controls script
execution time, reduces server load
 Local variables auto-cleanup, can be used
(expanded) in statements where variables
not allowed (table names, LIMIT value etc.)
 try-catch statement is available: easy error
handling mechanism
 echo, eval, throw statements make for
easy development
D B A ' s f r a m e w o r k f o r M y S Q L
common_schema
Copyright © 2013, Shlomi Noach
rdebug
 common_schema introduces rdebug:
debugger and debugging API for MySQL
stored routines.
 A server side solution, based in itself on
stored routines.
 Provides routine API calls to manage
debugging sessions of other routines.
 At this time (April 2013) in alpha stage
http://common-schema.googlecode.com/svn/trunk/common_schema/doc/html/rdebug.html
D B A ' s f r a m e w o r k f o r M y S Q L
common_schema
Copyright © 2013, Shlomi Noach
rdebug
 Uses code injection; one must “compile”
her routine with/out debug code.
– Does not emulate code; retains logic of one's
routines, with some limitations.
 Supports:
– Step into/over/out
– Watch variables, modify variables
– Set breakpoint, run to breakpoint
– Watch stack state, statements
D B A ' s f r a m e w o r k f o r M y S Q L
common_schema
Copyright © 2013, Shlomi Noach
rdebug
 common_schema provides with stored
routine API.
– One may debug on command line using
mysql client
– Or one may wrap with GUI tool to call upon
routine API
 Awaiting GUI client implementations!
D B A ' s f r a m e w o r k f o r M y S Q L
common_schema
Copyright © 2013, Shlomi Noach
rdebug
mysql [debugger]> call rdebug_step_into();
+-------------+----------------+---------------+--------------+---------------------+
| stack_level | routine_schema | routine_name | statement_id | entry_time |
+-------------+----------------+---------------+--------------+---------------------+
| 1 | test | review_colors | 145 | 2013-04-08 15:41:28 |
+-------------+----------------+---------------+--------------+---------------------+
+----------------+---------------+---------------+---------------+----------------+
| routine_schema | routine_name | variable_name | variable_type | variable_value |
+----------------+---------------+---------------+---------------+----------------+
| test | review_colors | current_color | local | white |
| test | review_colors | current_count | local | 10 |
| test | review_colors | done | local | 0 |
+----------------+---------------+---------------+---------------+----------------+
+----------------+---------------+--------------+-----------------------------------------+
| routine_schema | routine_name | statement_id | statement |
+----------------+---------------+--------------+-----------------------------------------+
| test | review_colors | 145 | call review_single_color(current_color) |
+----------------+---------------+--------------+-----------------------------------------+
mysql [debugger]> call rdebug_set_variable('current_color', 'orange');
mysql [debugger]> call rdebug_step_over();
D B A ' s f r a m e w o r k f o r M y S Q L
common_schema
Copyright © 2013, Shlomi Noach
call for help()
 common_schema documentation is
available in these formats:
– Online documentation (directly in code
repository)
– Bundled HTML download
– Inline: Just call for help():
call help('split');
+--------------------------------------------------------------------------------+
| help |
+--------------------------------------------------------------------------------+
| QueryScript Flow Control: split statement |
| |
| SYNOPSIS |
| |
| Single table operations, autodetect mode:
D B A ' s f r a m e w o r k f o r M y S Q L
common_schema
Copyright © 2013, Shlomi Noach
Roadmap & future plans
 Everything is developed by demand. There
are a lot of pending ideas.
 Priority given to solutions I need today.
 Nevertheless:
– QueryScript will evolve. Already pending
release are functions()
– QueryScript debugging on roadmap
– Schema snapshots and comparisons
– Other ideas
D B A ' s f r a m e w o r k f o r M y S Q L
common_schema
Copyright © 2013, Shlomi Noach
Support common_schema
 Download and try it out
 Report issues
http://code.google.com/p/common-schema/issues/list
 Happy to receive ideas and contributions
http://bit.ly/UPC3vh
http://code.google.com/p/common-schema/issues/entry?
template=Request%20for%20new%20component
 Above all else: spread the word!
D B A ' s f r a m e w o r k f o r M y S Q L
common_schema
Copyright © 2013, Shlomi Noach
Thank you!
 Visit http://openark.org for news &
updates.
 Other open source projects:
– openark kit
http://openarkkit.googlecode.com/svn/trunk/openarkkit/doc/html/introduction.html
– propagator
https://github.com/outbrain/propagator

More Related Content

What's hot (20)

PDF
MySQL's NoSQL -- Texas Linuxfest August 22nd 2015
Dave Stokes
 
PDF
Undelete (and more) rows from the binary log
Frederic Descamps
 
PDF
Second Step to the NoSQL Side: MySQL JSON Functions
Sveta Smirnova
 
PDF
MySQL Group Replication - an Overview
Matt Lord
 
PDF
MySQL Group Replication
Kenny Gryp
 
PDF
Php &amp; my sql - how do pdo, mysq-li, and x devapi do what they do
Dave Stokes
 
PDF
What you wanted to know about MySQL, but could not find using inernal instrum...
Sveta Smirnova
 
PPTX
PHP, The X DevAPI, and the MySQL Document Store Presented January 23rd, 20...
Dave Stokes
 
PPTX
Database 12c is ready for you... Are you ready for 12c?
Performance Tuning Corporation
 
PDF
PHP Oracle Web Applications by Kuassi Mensah
PHP Barcelona Conference
 
PDF
MySQL Server Defaults
Morgan Tocker
 
PDF
Perl Stored Procedures for MySQL (2009)
Antony T Curtis
 
PDF
Production Readiness Strategies in an Automated World
Sean Chittenden
 
PDF
Summary tables with flexviews
Justin Swanhart
 
PDF
MySQL Group Replication
Manish Kumar
 
PDF
Dynamic Database Credentials: Security Contingency Planning
Sean Chittenden
 
PDF
MySQL Database Architectures - 2020-10
Kenny Gryp
 
PDF
Mysql 56-experiences-bugs-solutions-50mins
Valeriy Kravchuk
 
PPSX
Oracle Cloud As Services
Özgür Umut Vurgun
 
PDF
MySQL Group Replication - Ready For Production? (2018-04)
Kenny Gryp
 
MySQL's NoSQL -- Texas Linuxfest August 22nd 2015
Dave Stokes
 
Undelete (and more) rows from the binary log
Frederic Descamps
 
Second Step to the NoSQL Side: MySQL JSON Functions
Sveta Smirnova
 
MySQL Group Replication - an Overview
Matt Lord
 
MySQL Group Replication
Kenny Gryp
 
Php &amp; my sql - how do pdo, mysq-li, and x devapi do what they do
Dave Stokes
 
What you wanted to know about MySQL, but could not find using inernal instrum...
Sveta Smirnova
 
PHP, The X DevAPI, and the MySQL Document Store Presented January 23rd, 20...
Dave Stokes
 
Database 12c is ready for you... Are you ready for 12c?
Performance Tuning Corporation
 
PHP Oracle Web Applications by Kuassi Mensah
PHP Barcelona Conference
 
MySQL Server Defaults
Morgan Tocker
 
Perl Stored Procedures for MySQL (2009)
Antony T Curtis
 
Production Readiness Strategies in an Automated World
Sean Chittenden
 
Summary tables with flexviews
Justin Swanhart
 
MySQL Group Replication
Manish Kumar
 
Dynamic Database Credentials: Security Contingency Planning
Sean Chittenden
 
MySQL Database Architectures - 2020-10
Kenny Gryp
 
Mysql 56-experiences-bugs-solutions-50mins
Valeriy Kravchuk
 
Oracle Cloud As Services
Özgür Umut Vurgun
 
MySQL Group Replication - Ready For Production? (2018-04)
Kenny Gryp
 

Similar to common_schema 2.2: DBA's framework for MySQL (April 2014) (20)

PDF
common_schema, DBA's framework for MySQL
Shlomi Noach
 
PDF
Southeast Linuxfest -- MySQL User Admin Tips & Tricks
Dave Stokes
 
PDF
Drupal campleuven: Secure Drupal Development
Steven Van den Hout
 
ODP
Knolx session
Knoldus Inc.
 
PPTX
MySQL Quick Dive
Sudipta Kumar Sahoo
 
ODP
Performance schema and_ps_helper
Mark Leith
 
PDF
Drupal Security from Drupalcamp Bratislava
Gábor Hojtsy
 
PDF
Development Setup of B-Translator
Dashamir Hoxha
 
PDF
Drupal Security Basics for the DrupalJax January Meetup
Chris Hales
 
PPTX
DBSAT-–-Oracle-Database-Security-Assessment-Tool.pptx
upendra429505
 
ODP
MySQL Administration and Monitoring
Mark Leith
 
PPTX
Derbycon - Passing the Torch
Will Schroeder
 
PPTX
Ultimate Free SQL Server Toolkit
Kevin Kline
 
PPT
Integris Security - Hacking With Glue ℠
Integris Security LLC
 
ODP
Developing Drizzle Replication Plugins
Padraig O'Sullivan
 
PPTX
2014 OpenSuse Conf: Protect your MySQL Server
Georgi Kodinov
 
PPTX
Go Hack Yourself - 10 Pen Test Tactics for Blue Teamers
jasonjfrank
 
PDF
Real-Time Query for Data Guard
Uwe Hesse
 
PDF
Oracle ORAchk & EXAchk overview
Gareth Chapman
 
PDF
Exachk Customer Presentation
Sandesh Rao
 
common_schema, DBA's framework for MySQL
Shlomi Noach
 
Southeast Linuxfest -- MySQL User Admin Tips & Tricks
Dave Stokes
 
Drupal campleuven: Secure Drupal Development
Steven Van den Hout
 
Knolx session
Knoldus Inc.
 
MySQL Quick Dive
Sudipta Kumar Sahoo
 
Performance schema and_ps_helper
Mark Leith
 
Drupal Security from Drupalcamp Bratislava
Gábor Hojtsy
 
Development Setup of B-Translator
Dashamir Hoxha
 
Drupal Security Basics for the DrupalJax January Meetup
Chris Hales
 
DBSAT-–-Oracle-Database-Security-Assessment-Tool.pptx
upendra429505
 
MySQL Administration and Monitoring
Mark Leith
 
Derbycon - Passing the Torch
Will Schroeder
 
Ultimate Free SQL Server Toolkit
Kevin Kline
 
Integris Security - Hacking With Glue ℠
Integris Security LLC
 
Developing Drizzle Replication Plugins
Padraig O'Sullivan
 
2014 OpenSuse Conf: Protect your MySQL Server
Georgi Kodinov
 
Go Hack Yourself - 10 Pen Test Tactics for Blue Teamers
jasonjfrank
 
Real-Time Query for Data Guard
Uwe Hesse
 
Oracle ORAchk & EXAchk overview
Gareth Chapman
 
Exachk Customer Presentation
Sandesh Rao
 
Ad

More from Shlomi Noach (6)

PDF
Un-split brain MySQL
Shlomi Noach
 
PDF
Managing and Visualizing your Replication Topologies with Orchestrator
Shlomi Noach
 
PDF
Pseudo GTID and Easy MySQL Replication Topology Management
Shlomi Noach
 
PDF
Pseudo gtid & easy replication topology management
Shlomi Noach
 
PDF
MySQL DevOps at Outbrain
Shlomi Noach
 
PDF
What's new in MySQL 5.6
Shlomi Noach
 
Un-split brain MySQL
Shlomi Noach
 
Managing and Visualizing your Replication Topologies with Orchestrator
Shlomi Noach
 
Pseudo GTID and Easy MySQL Replication Topology Management
Shlomi Noach
 
Pseudo gtid & easy replication topology management
Shlomi Noach
 
MySQL DevOps at Outbrain
Shlomi Noach
 
What's new in MySQL 5.6
Shlomi Noach
 
Ad

Recently uploaded (20)

PPTX
In From the Cold: Open Source as Part of Mainstream Software Asset Management
Shane Coughlan
 
PDF
TheFutureIsDynamic-BoxLang witch Luis Majano.pdf
Ortus Solutions, Corp
 
PPTX
Agentic Automation Journey Series Day 2 – Prompt Engineering for UiPath Agents
klpathrudu
 
PDF
AOMEI Partition Assistant Crack 10.8.2 + WinPE Free Downlaod New Version 2025
bashirkhan333g
 
PPTX
Empowering Asian Contributions: The Rise of Regional User Groups in Open Sour...
Shane Coughlan
 
PDF
Technical-Careers-Roadmap-in-Software-Market.pdf
Hussein Ali
 
PDF
Driver Easy Pro 6.1.1 Crack Licensce key 2025 FREE
utfefguu
 
PDF
Empower Your Tech Vision- Why Businesses Prefer to Hire Remote Developers fro...
logixshapers59
 
PPTX
AEM User Group: India Chapter Kickoff Meeting
jennaf3
 
PDF
IDM Crack with Internet Download Manager 6.42 Build 43 with Patch Latest 2025
bashirkhan333g
 
PDF
Wondershare PDFelement Pro Crack for MacOS New Version Latest 2025
bashirkhan333g
 
PDF
SAP Firmaya İade ABAB Kodları - ABAB ile yazılmıl hazır kod örneği
Salih Küçük
 
PDF
Odoo CRM vs Zoho CRM: Honest Comparison 2025
Odiware Technologies Private Limited
 
PDF
Build It, Buy It, or Already Got It? Make Smarter Martech Decisions
bbedford2
 
PPTX
Help for Correlations in IBM SPSS Statistics.pptx
Version 1 Analytics
 
PDF
Automate Cybersecurity Tasks with Python
VICTOR MAESTRE RAMIREZ
 
PPTX
Home Care Tools: Benefits, features and more
Third Rock Techkno
 
PDF
How to Hire AI Developers_ Step-by-Step Guide in 2025.pdf
DianApps Technologies
 
PDF
SciPy 2025 - Packaging a Scientific Python Project
Henry Schreiner
 
PDF
Open Chain Q2 Steering Committee Meeting - 2025-06-25
Shane Coughlan
 
In From the Cold: Open Source as Part of Mainstream Software Asset Management
Shane Coughlan
 
TheFutureIsDynamic-BoxLang witch Luis Majano.pdf
Ortus Solutions, Corp
 
Agentic Automation Journey Series Day 2 – Prompt Engineering for UiPath Agents
klpathrudu
 
AOMEI Partition Assistant Crack 10.8.2 + WinPE Free Downlaod New Version 2025
bashirkhan333g
 
Empowering Asian Contributions: The Rise of Regional User Groups in Open Sour...
Shane Coughlan
 
Technical-Careers-Roadmap-in-Software-Market.pdf
Hussein Ali
 
Driver Easy Pro 6.1.1 Crack Licensce key 2025 FREE
utfefguu
 
Empower Your Tech Vision- Why Businesses Prefer to Hire Remote Developers fro...
logixshapers59
 
AEM User Group: India Chapter Kickoff Meeting
jennaf3
 
IDM Crack with Internet Download Manager 6.42 Build 43 with Patch Latest 2025
bashirkhan333g
 
Wondershare PDFelement Pro Crack for MacOS New Version Latest 2025
bashirkhan333g
 
SAP Firmaya İade ABAB Kodları - ABAB ile yazılmıl hazır kod örneği
Salih Küçük
 
Odoo CRM vs Zoho CRM: Honest Comparison 2025
Odiware Technologies Private Limited
 
Build It, Buy It, or Already Got It? Make Smarter Martech Decisions
bbedford2
 
Help for Correlations in IBM SPSS Statistics.pptx
Version 1 Analytics
 
Automate Cybersecurity Tasks with Python
VICTOR MAESTRE RAMIREZ
 
Home Care Tools: Benefits, features and more
Third Rock Techkno
 
How to Hire AI Developers_ Step-by-Step Guide in 2025.pdf
DianApps Technologies
 
SciPy 2025 - Packaging a Scientific Python Project
Henry Schreiner
 
Open Chain Q2 Steering Committee Meeting - 2025-06-25
Shane Coughlan
 

common_schema 2.2: DBA's framework for MySQL (April 2014)

  • 1. common_schema2.2 D B A ' s f r a m e w o r k f o r M y S Q L S h l o m i N o a c h h t t p : / / o p e n a r k . o r g
  • 2. Copyright © 2014, Shlomi Noach common_schema D B A ' s f r a m e w o r k f o r M y S Q L common_schema2.2 D B A ' s f r a m e w o r k f o r M y S Q L  About  Views & eval()  Routines  QueryScript  rdebug
  • 3. D B A ' s f r a m e w o r k f o r M y S Q L common_schema Copyright © 2013, Shlomi Noach About myself  Shlomi Noach, geek  Software engineer, DBA  Author of open source tools common_schema, openark kit, propagator, others.  Blog on all things MySQL http://openark.org  Employed by Outbrain Inc.
  • 4. D B A ' s f r a m e w o r k f o r M y S Q L common_schema Copyright © 2013, Shlomi Noach About common_schema  common_schema is an open source project, licensed under the GPL License.  Authored by Shlomi Noach http://openark.org  Major contributions by Roland Bouman http://rpbouman.blogspot.com  Several contributions & suggestions by the community (Thanks!)
  • 5. D B A ' s f r a m e w o r k f o r M y S Q L common_schema Copyright © 2013, Shlomi Noach About common_schema  common_schema is a framework which includes: – Views: analytics, security, action-taking, ... – Routines: text, temporal, process, security, ... – QueryScript: an SQL oriented scripting language & interpreter – rdebug: debugger and debugging API for MySQL stored routines (alpha).
  • 6. D B A ' s f r a m e w o r k f o r M y S Q L common_schema Copyright © 2013, Shlomi Noach About common_schema  It is a schema that lies next to INFORMATION_SCHEMA.  It lies completely within the MySQL server, and does not require external packages or dependencies. No Perl scripts nor UDFs or plugins.
  • 7. D B A ' s f r a m e w o r k f o r M y S Q L common_schema Copyright © 2013, Shlomi Noach Getting & Installing  common_schema distribution is a SQL file.  Currently hosted on Google Code: http://code.google.com/p/common-schema/  Install by importing SQL file into MySQL: bash$ mysql < /tmp/common_schema-2.2.sql complete - Base components: installed - InnoDB Plugin components: installed - Percona Server components: not installed - TokuDB components: partial install: 1/2 Installation complete. Thank you for using common_schema!
  • 8. D B A ' s f r a m e w o r k f o r M y S Q L common_schema Copyright © 2013, Shlomi Noach Views  Views providing non-trivial information about your table design, keys, grants, processes, transactions, locks, ...  While INFORMATION_SCHEMA provides with complete info, it is ofter difficult to aggregate. It is sometimes too normalized, and at other times too de-normalized.  Some metadata is simply not available in INFORMATION_SCHEMA.
  • 9. D B A ' s f r a m e w o r k f o r M y S Q L common_schema Copyright © 2013, Shlomi Noach Views  common_schema's views will present with valuable knowledge  And will typically offer recommendations or possible actions based on that knowledge.  common_schema also provides the mechanism to apply those recommendations.
  • 10. D B A ' s f r a m e w o r k f o r M y S Q L common_schema Copyright © 2013, Shlomi Noach Views: redundant_keys  Find duplicate/redundant keys in your schema. Recommend DROP statements. http://common-schema.googlecode.com/svn/trunk/common_schema/doc/html/redundant_keys.html – Similar to pt-duplicate-key-checker mysql> select * from redundant_keys where table_schema='sakila'G table_schema: sakila table_name: rental redundant_index_name: rental_date_2 redundant_index_columns: rental_date redundant_index_non_unique: 1 dominant_index_name: rental_date dominant_index_columns: rental_date, inventory_id, customer_id dominant_index_non_unique: 0 sql_drop_index: ALTER TABLE `sakila`.`rental` DROP INDEX `rental_date_2`
  • 11. D B A ' s f r a m e w o r k f o r M y S Q L common_schema Copyright © 2013, Shlomi Noach Views: sql_range_partitions  Analyze your range partitioned tables. Offers the ADD/REORGANIZE PARTITION statements to roll partitions: mysql> create table test.report … partition by range (…) ; mysql> select * from sql_range_partitions where table_name='report' G table_schema: test table_name: report count_partitions: 7 sql_drop_first_partition: alter table `test`.`report` drop partition `p0` sql_add_next_partition: alter table `test`.`report` reorganize partition `p6` into ( partition `p_20090701000000` values less than (1246395600) /* 2009-07-01 00:00:00 */ , partition p_maxvalue values less than MAXVALUE )
  • 12. D B A ' s f r a m e w o r k f o r M y S Q L common_schema Copyright © 2013, Shlomi Noach Views: sql grants  Provide SQL based access to user accounts and their grants.  Offers the relevant GRANT, REVOKE and DROP statements for such grants: GRANTEE: 'world_user'@'localhost' user: world_user host: localhost priv_level: `world`.* priv_level_name: schema current_privileges: INSERT, SELECT, UPDATE sql_grant: GRANT INSERT, SELECT, UPDATE ON `world`.* TO 'world_user'@'%' sql_revoke: REVOKE INSERT, SELECT, UPDATE ON `world`.* FROM 'world_user'@'%'
  • 13. D B A ' s f r a m e w o r k f o r M y S Q L common_schema Copyright © 2013, Shlomi Noach SQL generation & eval()  Views above present with “SQL columns”, offering a statement to execute.  This is at the heart of common_schema's views, and is part of the server-side mechanism the framework strongly supports.  The eval() routine accepts such SQL columns and executes (evaluates) them.
  • 14. D B A ' s f r a m e w o r k f o r M y S Q L common_schema Copyright © 2013, Shlomi Noach eval()  Accepts a query returning a text column. Column data is expected to be SQL statements to be executed. mysql> call eval(" SELECT sql_add_next_partition FROM sql_range_partitions WHERE table_schema='webprod'"); Query OK, 0 rows affected -- A new partition has just been added on -- all range-partitioned tables in `webprod`
  • 15. D B A ' s f r a m e w o r k f o r M y S Q L common_schema Copyright © 2013, Shlomi Noach eval() KILL QUERY 123 KILL QUERY 129 KILL QUERY 598 KILL QUERY 620 KILL QUERY 702 Query eval() Execute query Execute statements
  • 16. D B A ' s f r a m e w o r k f o r M y S Q L common_schema Copyright © 2013, Shlomi Noach Views: innodb_transactions  Which transactions are running? – For how long? – How long are they being idle? Locked? – What queries are they issuing?  Recommend KILL, apply with eval() mysql> call eval(" SELECT sql_kill_query FROM innodb_transactions WHERE trx_idle_seconds >= 30 "); -- All idle transactions have just been killed
  • 17. D B A ' s f r a m e w o r k f o r M y S Q L common_schema Copyright © 2013, Shlomi Noach Views: others  processlist_grantees: identifies the user connection with its associated account (missing info in MySQL)  auto_increment_columns: find “free space” or “usage” for AUTO_INCREMENT values  slave_status: get Seconds_behind_master via real SQL query  last_query_profiling :aggregated profile for last executed query
  • 18. D B A ' s f r a m e w o r k f o r M y S Q L common_schema Copyright © 2013, Shlomi Noach Views: processlist_grantees mysql> select * from processlist_granteesG ID: 41165491 USER: dbuser HOST: web00.myapp:40049 DB: profile COMMAND: Query TIME: 0 STATE: updating INFO: DELETE FROM locks WHERE id = 'helper' AND dt < '2013-04-17 15:09:50' GRANTEE: 'dbuser'@'%.myapp' grantee_user: dbuser grantee_host: %.myapp is_super: 0 is_repl: 0 is_current: 0 sql_kill_query: KILL QUERY 41165491 sql_kill_connection: KILL 41165491
  • 19. D B A ' s f r a m e w o r k f o r M y S Q L common_schema Copyright © 2013, Shlomi Noach Views: others  sql_accounts: block/release accounts  similar_grants: detect security roles  global_status_diff_nonzero: detect status change  innodb_locked_transactions: who locks who, on which query? Recommend KILL  more... http://common-schema.googlecode.com/svn/trunk/common_schema/doc/html/process_views.html http://common-schema.googlecode.com/svn/trunk/common_schema/doc/html/innodb_plugin_views.html http://common-schema.googlecode.com/svn/trunk/common_schema/doc/html/security_views.html http://common-schema.googlecode.com/svn/trunk/common_schema/doc/html/schema_analysis_views.html
  • 20. D B A ' s f r a m e w o r k f o r M y S Q L common_schema Copyright © 2013, Shlomi Noach Routines  common_schema offers more than 60 useful stored routines, part of the framework's function library.  From text parsing & manipulation, through process diagnostics, query analysis & dynamic execution, to security routines, the function library extends and complements MySQL's own functions. http://common-schema.googlecode.com/svn/trunk/common_schema/doc/html/execution_routines.html http://common-schema.googlecode.com/svn/trunk/common_schema/doc/html/text_routines.html http://common-schema.googlecode.com/svn/trunk/common_schema/doc/html/security_routines.html http://common-schema.googlecode.com/svn/trunk/common_schema/doc/html/process_routines.html
  • 21. D B A ' s f r a m e w o r k f o r M y S Q L common_schema Copyright © 2013, Shlomi Noach Routines: security  killall() kills connections by matching text with user, host or grantee.  security_audit() audits server's privileges tables and configuration to detect such threats as empty or duplicate passwords, excessive privileges, permissive hosts etc.  duplicate_grantee(): duplicates an account mysql> call killall('analytics'); mysql> call killall('localhost');
  • 22. D B A ' s f r a m e w o r k f o r M y S Q L common_schema Copyright © 2013, Shlomi Noach Routines: text  extract_json_value(): Extract value from JSON notation via Xpath.  replace_sections(): Search and replace text appearing between section.  get_num_tokens(): Return number of tokens in delimited text. mysql> select common_schema.get_num_tokens( 'the quick brown fox', ' '); => 4 mysql> select common_schema.split_token( 'the quick brown fox', ' ', 3); => 'brown'
  • 23. D B A ' s f r a m e w o r k f o r M y S Q L common_schema Copyright © 2013, Shlomi Noach Routines: text mysql> set @json := '{ "menu": { "id": "file", "value": "File", "popup": { "menuitem": [ {"value": "New", "onclick": "create()"}, {"value": "Open", "onclick": "open()"}, {"value": "Close", "onclick": "close()"} ] } } }'; mysql> select extract_json_value(@json, '//id') AS result; +--------+ | result | +--------+ | file | +--------+
  • 24. D B A ' s f r a m e w o r k f o r M y S Q L common_schema Copyright © 2013, Shlomi Noach Routines: general  query_checksum(): Checksum the result set of a query.  crc64(): Return a 64 bit CRC of given input, as unsigned big integer.  random_hash(): Return a 64 bit CRC of given input, as unsigned big integer. mysql> call query_checksum( 'select code, name from world.Country order by code'); +----------------------------------+ | checksum | +----------------------------------+ | 79221e8d040e33cd3262f1680b4c8e54 | +----------------------------------+
  • 25. D B A ' s f r a m e w o r k f o r M y S Q L common_schema Copyright © 2013, Shlomi Noach Routines: SQL & execution  eval() evaluates the queries generated by a given query.  exec(), exec_file() dynamically executes a given query or semicolon delimited list of queries.  run(), run_file() execute QueryScript code. mysql> call exec(' CREATE TABLE test.t(id INT); INSERT INTO test.t VALUES (2),(3),(5); ');
  • 26. D B A ' s f r a m e w o r k f o r M y S Q L common_schema Copyright © 2013, Shlomi Noach QueryScript  A SQL oriented scripting language, offering tight integration with SQL commands, easy and familiar control flow syntax and high level abstraction of complex tasks.  common_schema implements QueryScript via interpreter, based on stored routines.  This makes QueryScript suitable for administration and bulk tasks, not for OLTP tasks. http://common-schema.googlecode.com/svn/trunk/common_schema/doc/html/query_script.html
  • 27. D B A ' s f r a m e w o r k f o r M y S Q L common_schema Copyright © 2013, Shlomi Noach Why QueryScript?  Stored routine programming is a pain: – Requires one to actually store the routine within the schema: can't just run something. – Syntax is cumbersome (ANSI:SQL). – Does not offer deeper insight into MySQL's limitations and bottlenecks. – Does not provide with syntax for oh-so- common tasks – Verbose. Can't see the forest for the trees.
  • 28. D B A ' s f r a m e w o r k f o r M y S Q L common_schema Copyright © 2013, Shlomi Noach Compare: stored routine DELIMITER $$ DROP PROCEDURE IF EXISTS some_proc $$ CREATE PROCEDURE some_proc() READS SQL DATA SQL SECURITY INVOKER begin declare some_id bigint unsigned default null; declare done tinyint default 0; declare my_cursor cursor for SELECT some_id FROM some_table; declare continue handler for NOT FOUND set done = 1; open my_cursor; read_loop: loop fetch my_cursor into some_id; if done then leave read_loop; end if; -- do something with some_id end loop; close my_cursor; end $$ DELIMITER ;
  • 29. D B A ' s f r a m e w o r k f o r M y S Q L common_schema Copyright © 2013, Shlomi Noach Compare: QueryScript set @script := ' foreach ($some_id: select some_id from some_table) { -- do something with $some_id } '; call run(@script);  Significantly less overhead  Familiar C-family syntax  No need to store the code in schema  Can execute script directly from file call run('/path/to/script.qs');
  • 30. D B A ' s f r a m e w o r k f o r M y S Q L common_schema Copyright © 2013, Shlomi Noach QueryScript: variables  Create variables which are known to initialize as NULL and are known to clean up as they exit scope.  Can be expanded and used where MySQL does not allow variables. var $count := 20; var $pop := 1000000; var $tbl := 'City'; select * from world.:${tbl} where population >= $pop limit :${count};
  • 31. D B A ' s f r a m e w o r k f o r M y S Q L common_schema Copyright © 2013, Shlomi Noach QueryScript: conditions  Familiar if, while or loop-while statements.  But conditions are also tightly integrated with SQL, and so queries make for valid conditions. if (@val > 3) { pass; } while (delete from world.Country where Continent = 'Asia' limit 10) { throttle 2; }
  • 32. D B A ' s f r a m e w o r k f o r M y S Q L common_schema Copyright © 2013, Shlomi Noach QueryScript: foreach  A sophisticated looping device, allowing iteration over queries, sets, numbers range, databases, tables...  But also acknowledges MySQL limitations and provides with a safer access method.  Table iteration uses INFORMATION_SCHEMA optimizations to avoid excessive locks: foreach($tbl, $scm: table like wp_posts) alter table :${scm}.:${tbl} add column post_geo_location VARCHAR(128);
  • 33. D B A ' s f r a m e w o r k f o r M y S Q L common_schema Copyright © 2013, Shlomi Noach QueryScript: foreach foreach($year: 2001:2009) delete from sakila.rental where rental_date >= CONCAT($year, '-07-01') and rental_date < CONCAT($year, '-09-01'); foreach($shard: {US, GB, Japan, FRA}) create database dbshard_:${shard}; foreach($scm: schema like wp%) { create table :$scm.wp_likes( id int, data VARCHAR(128)); } foreach ($some_id: select some_id from some_table) {... }
  • 34. D B A ' s f r a m e w o r k f o r M y S Q L common_schema Copyright © 2013, Shlomi Noach QueryScript: split  Automagically breaks a bulk operation into smaller chunks.  Supports DELETE, UPDATE, INSERT...SELECT, REPLACE...SELECT  Supports single and multi table statements split (delete from sakila.rental where rental_date < NOW() - interval 5 year); split (insert into world.City_duplicate select * from world.City) { throttle 1; }
  • 35. D B A ' s f r a m e w o r k f o r M y S Q L common_schema Copyright © 2013, Shlomi Noach QueryScript: split split(update sakila.film_actor set last_update=now() where actor_id != 4) { select $split_columns as columns, $split_range_start as range_start, $split_range_end as range_end, $split_total_rowcount as total; } +----------------------+-------------+------------+-------+ | columns | range_start | range_end | total | +----------------------+-------------+------------+-------+ | `actor_id`,`film_id` | '1','1' | '39','293' | 978 | +----------------------+-------------+------------+-------+ +----------------------+-------------+------------+-------+ | columns | range_start | range_end | total | +----------------------+-------------+------------+-------+ | `actor_id`,`film_id` | '39','293' | '76','234' | 1978 | +----------------------+-------------+------------+-------+ +----------------------+-------------+-------------+-------+ | columns | range_start | range_end | total | +----------------------+-------------+-------------+-------+ | `actor_id`,`film_id` | '76','234' | '110','513' | 2978 | +----------------------+-------------+-------------+-------+
  • 36. D B A ' s f r a m e w o r k f o r M y S Q L common_schema Copyright © 2013, Shlomi Noach QueryScript: split, example  Real world use case: need to copy 120,000,000 rows belonging to a specific partition from range-partitioned table 2013-02 2013-03 2013-04 2013-05 2013-06 2013-05
  • 37. D B A ' s f r a m e w o r k f o r M y S Q L common_schema Copyright © 2013, Shlomi Noach QueryScript: split, example split ( { index: PRIMARY, start: '2013-05-01', step: 20000 } : insert into one_month_sample select * from table_with_many_month_partitions where entry_date < DATE('2013-06-01') ) { if ($split_range_start >= DATE('2013-06-01')) Break; throttle 1; }  We provide hints to the split statement: choosing index, start position, chunk size.  Terminate when out of range.
  • 38. D B A ' s f r a m e w o r k f o r M y S Q L common_schema Copyright © 2013, Shlomi Noach QueryScript: example  Create shards & tables, and copy row data: -- iterate shards foreach ($shard: {US, GB, Japan, FRA}) { create database dbshard_:${shard}; -- iterate tables foreach ($tbl: table in original_shard) { create table dbshard_:${shard}.:${tbl} like original_shard.:$tbl; -- copy table data split (insert into dbshard_:${shard}.:${tbl} select * from original_shard.:$tbl) { -- show progress select $shard, $tbl, $split_total_rowcount; throttle 1; } } }
  • 39. D B A ' s f r a m e w o r k f o r M y S Q L common_schema Copyright © 2013, Shlomi Noach QueryScript: example  Protect against malfunctioning 3rd party locking code: call common_schema.run(" while( 1 ) { sleep 5; try { eval select min(sql_kill_blocking_connection) from common_schema.innodb_locked_transactions group by locking_trx_mysql_thread_id having count(*) > 10; } catch {pass;} } ");
  • 40. D B A ' s f r a m e w o r k f o r M y S Q L common_schema Copyright © 2013, Shlomi Noach QueryScript: example  Create 10 years worth of partitions: CREATE TABLE my_document ( my_document_id int unsigned NOT NULL AUTO_INCREMENT, creation_date DATETIME NOT NULL, data VARCHAR(127), PRIMARY KEY (my_document_id, creation_date) ) PARTITION BY RANGE (to_days(creation_date)) (PARTITION p_2006_01_01 VALUES LESS THAN (732677), PARTITION p_2006_02_01 VALUES LESS THAN (732708), PARTITION p_2006_03_01 VALUES LESS THAN (732736), PARTITION p_2006_04_01 VALUES LESS THAN (732767) ); call common_schema.run(" foreach($i : 1:240) { eval select sql_add_next_partition from sql_range_partitions where table_name='my_document'; } ");
  • 41. D B A ' s f r a m e w o r k f o r M y S Q L common_schema Copyright © 2013, Shlomi Noach QueryScript: more goodies  throttle statement controls script execution time, reduces server load  Local variables auto-cleanup, can be used (expanded) in statements where variables not allowed (table names, LIMIT value etc.)  try-catch statement is available: easy error handling mechanism  echo, eval, throw statements make for easy development
  • 42. D B A ' s f r a m e w o r k f o r M y S Q L common_schema Copyright © 2013, Shlomi Noach rdebug  common_schema introduces rdebug: debugger and debugging API for MySQL stored routines.  A server side solution, based in itself on stored routines.  Provides routine API calls to manage debugging sessions of other routines.  At this time (April 2013) in alpha stage http://common-schema.googlecode.com/svn/trunk/common_schema/doc/html/rdebug.html
  • 43. D B A ' s f r a m e w o r k f o r M y S Q L common_schema Copyright © 2013, Shlomi Noach rdebug  Uses code injection; one must “compile” her routine with/out debug code. – Does not emulate code; retains logic of one's routines, with some limitations.  Supports: – Step into/over/out – Watch variables, modify variables – Set breakpoint, run to breakpoint – Watch stack state, statements
  • 44. D B A ' s f r a m e w o r k f o r M y S Q L common_schema Copyright © 2013, Shlomi Noach rdebug  common_schema provides with stored routine API. – One may debug on command line using mysql client – Or one may wrap with GUI tool to call upon routine API  Awaiting GUI client implementations!
  • 45. D B A ' s f r a m e w o r k f o r M y S Q L common_schema Copyright © 2013, Shlomi Noach rdebug mysql [debugger]> call rdebug_step_into(); +-------------+----------------+---------------+--------------+---------------------+ | stack_level | routine_schema | routine_name | statement_id | entry_time | +-------------+----------------+---------------+--------------+---------------------+ | 1 | test | review_colors | 145 | 2013-04-08 15:41:28 | +-------------+----------------+---------------+--------------+---------------------+ +----------------+---------------+---------------+---------------+----------------+ | routine_schema | routine_name | variable_name | variable_type | variable_value | +----------------+---------------+---------------+---------------+----------------+ | test | review_colors | current_color | local | white | | test | review_colors | current_count | local | 10 | | test | review_colors | done | local | 0 | +----------------+---------------+---------------+---------------+----------------+ +----------------+---------------+--------------+-----------------------------------------+ | routine_schema | routine_name | statement_id | statement | +----------------+---------------+--------------+-----------------------------------------+ | test | review_colors | 145 | call review_single_color(current_color) | +----------------+---------------+--------------+-----------------------------------------+ mysql [debugger]> call rdebug_set_variable('current_color', 'orange'); mysql [debugger]> call rdebug_step_over();
  • 46. D B A ' s f r a m e w o r k f o r M y S Q L common_schema Copyright © 2013, Shlomi Noach call for help()  common_schema documentation is available in these formats: – Online documentation (directly in code repository) – Bundled HTML download – Inline: Just call for help(): call help('split'); +--------------------------------------------------------------------------------+ | help | +--------------------------------------------------------------------------------+ | QueryScript Flow Control: split statement | | | | SYNOPSIS | | | | Single table operations, autodetect mode:
  • 47. D B A ' s f r a m e w o r k f o r M y S Q L common_schema Copyright © 2013, Shlomi Noach Roadmap & future plans  Everything is developed by demand. There are a lot of pending ideas.  Priority given to solutions I need today.  Nevertheless: – QueryScript will evolve. Already pending release are functions() – QueryScript debugging on roadmap – Schema snapshots and comparisons – Other ideas
  • 48. D B A ' s f r a m e w o r k f o r M y S Q L common_schema Copyright © 2013, Shlomi Noach Support common_schema  Download and try it out  Report issues http://code.google.com/p/common-schema/issues/list  Happy to receive ideas and contributions http://bit.ly/UPC3vh http://code.google.com/p/common-schema/issues/entry? template=Request%20for%20new%20component  Above all else: spread the word!
  • 49. D B A ' s f r a m e w o r k f o r M y S Q L common_schema Copyright © 2013, Shlomi Noach Thank you!  Visit http://openark.org for news & updates.  Other open source projects: – openark kit http://openarkkit.googlecode.com/svn/trunk/openarkkit/doc/html/introduction.html – propagator https://github.com/outbrain/propagator