Upload
giuseppe-maxia
View
1.769
Download
0
Embed Size (px)
Citation preview
Creative programming with MySQL
Giuseppe Maxia, MySQL Community Team Lead
Database Group, Sun Microsystems Inc
[email protected] The Data Charmer
2008 CommunityOne Conference | developers.sun.com/events/communityone | 2
Creative programming?
WTF?
2008 CommunityOne Conference | developers.sun.com/events/communityone | 2
Creative programming?
WTF?
bright ideas
2008 CommunityOne Conference | developers.sun.com/events/communityone | 2
Creative programming?
WTF?
hacking!
bright ideas
2008 CommunityOne Conference | developers.sun.com/events/communityone | 3
About me
22 years in ITconsultantMySQL communityQA developerMySQL Community Team Lead
2008 CommunityOne Conference | developers.sun.com/events/communityone | 4
About me
database hackercreative programmer
2008 CommunityOne Conference | developers.sun.com/events/communityone | 5
Does it look familiar?
SELECT * FROM table_name;
2008 CommunityOne Conference | developers.sun.com/events/communityone | 5
Does it look familiar?
SELECT * FROM table_name;UPDATE table_name SET x=1 WHERE y=10;
2008 CommunityOne Conference | developers.sun.com/events/communityone | 5
Does it look familiar?
SELECT * FROM table_name;UPDATE table_name SET x=1 WHERE y=10;
DELETE FROM tab_name WHERE z=1;
2008 CommunityOne Conference | developers.sun.com/events/communityone | 5
Does it look familiar?
SELECT * FROM table_name;UPDATE table_name SET x=1 WHERE y=10;
DELETE FROM tab_name WHERE z=1;make coffee;
2008 CommunityOne Conference | developers.sun.com/events/communityone | 5
Does it look familiar?
SELECT * FROM table_name;UPDATE table_name SET x=1 WHERE y=10;
DELETE FROM tab_name WHERE z=1;make coffee;play movie;
2008 CommunityOne Conference | developers.sun.com/events/communityone | 5
Does it look familiar?
SELECT * FROM table_name;UPDATE table_name SET x=1 WHERE y=10;
DELETE FROM tab_name WHERE z=1;make coffee;play movie; ??
2008 CommunityOne Conference | developers.sun.com/events/communityone | 6
Agenda
Database programming blues Stored routinesTriggersViewsEventsFederatedBlackholeCreative loopsMySQL Proxy
2008 CommunityOne Conference | developers.sun.com/events/communityone | 6
Agenda
Database programming blues Stored routinesTriggersViewsEventsFederatedBlackholeCreative loopsMySQL Proxy
2008 CommunityOne Conference | developers.sun.com/events/communityone | 6
Agenda
Database programming blues Stored routinesTriggersViewsEventsFederatedBlackholeCreative loopsMySQL Proxy
2008 CommunityOne Conference | developers.sun.com/events/communityone | 7
Agenda
Database programming blues Stored routinesTriggersViewsEventsFederatedBlackholeCreative loopsRevisiting TriggersMySQL Proxy
2008 CommunityOne Conference | developers.sun.com/events/communityone | 8
What average programmers dislike
regular expressions
2008 CommunityOne Conference | developers.sun.com/events/communityone | 8
What average programmers dislike
regular expressions
"I (love|hate)\s*([Ff]\w*)\s*REGEXP"
2008 CommunityOne Conference | developers.sun.com/events/communityone | 9
What average programmers dislike
regular expressionsdatabase backend
2008 CommunityOne Conference | developers.sun.com/events/communityone | 9
What average programmers dislike
regular expressionsdatabase backend
SELECT t.id FROM (SELECT id2 FROM other WHERE b = 2) AS tWHERE a > (SELECT MAX(x) FROM z);
2008 CommunityOne Conference | developers.sun.com/events/communityone | 10
Why many programmers hate database handling?
SQL != JavaSQL != PerlSQL != PHPSQL != C++I want to write in my language of choice
2008 CommunityOne Conference | developers.sun.com/events/communityone | 11
database handling
bring the logic at application level
2008 CommunityOne Conference | developers.sun.com/events/communityone | 12
database handling
database
C/C++Java
Perl
PHP
.NET
2008 CommunityOne Conference | developers.sun.com/events/communityone | 13
database handlingtoo many languages
database
C/C++Java
Perl
PHP
.NET
Perl library
Java class
PHP library
2008 CommunityOne Conference | developers.sun.com/events/communityone | 14
database handling
set the logic at server level (stored routines)
2008 CommunityOne Conference | developers.sun.com/events/communityone | 15
database handling
C/C++Java
Perl
PHP
.NET
database
stored routineslibrary
2008 CommunityOne Conference | developers.sun.com/events/communityone | 16
database handling
set the logic at protocol level (proxy)
2008 CommunityOne Conference | developers.sun.com/events/communityone | 17
database handling
C/C++Java
Perl
PHP
.NET
database
Proxylibrary
2008 CommunityOne Conference | developers.sun.com/events/communityone | 18
Agenda
Database programming blues Stored routinesTriggersViewsEventsFederatedBlackholeCreative loopsRevisiting TriggersMySQL Proxy
2008 CommunityOne Conference | developers.sun.com/events/communityone | 19
Stored routines - the boring stuff
SQL standardheavy syntaxtricky performanceno debug - limited exception handlingprocedures • can return multiple record sets• can execute dynamic code
functions• must return one value• no dynamic code
2008 CommunityOne Conference | developers.sun.com/events/communityone | 20
Stored routines - the boring stuff
SQL statementsIF THEN ELSE .. END IFWHILE .. DO .. END WHILELOOP .. END LOOPCURSORCONDITIONHANDLER
2008 CommunityOne Conference | developers.sun.com/events/communityone | 21
Stored routines - some hacks
enhance the language• arrays• easy loops• global variables• see MySQL general purpose routine library
create routines on-the-fly• undocumented hack!• see http://datacharmer.org
2008 CommunityOne Conference | developers.sun.com/events/communityone | 22
Agenda
Database programming blues Stored routinesTriggersViewsEventsFederatedBlackholeCreative loopsRevisiting TriggersMySQL Proxy
2008 CommunityOne Conference | developers.sun.com/events/communityone | 23
Triggers - the boring stuff
BEFORE or AFTER eventBEFORE INSERT/UPDATE/DELETEAFTER INSERT/UPDATE/DELETENo dynamic codeNo operations on the same tableNo multiple triggers per event
2008 CommunityOne Conference | developers.sun.com/events/communityone | 24
Triggers - what for
dedicated loggingdata aggregationtransfer of data to other tables/databasescalculated fields
2008 CommunityOne Conference | developers.sun.com/events/communityone | 25
Triggers - HACKS!
wait.Strong combination with FEDERATED and BLACKHOLE
2008 CommunityOne Conference | developers.sun.com/events/communityone | 26
Agenda
Database programming blues Stored routinesTriggersViewsEventsFederatedBlackholeCreative loopsRevisiting TriggersMySQL Proxy
2008 CommunityOne Conference | developers.sun.com/events/communityone | 27
Views
no storagelook like tablescan aggregate datacan insert datacan save timebut can also become horribly inefficient
2008 CommunityOne Conference | developers.sun.com/events/communityone | 28
Views - the cool stuff
can run functionscan create sort of triggers on select
2008 CommunityOne Conference | developers.sun.com/events/communityone | 29
Views with functions
CREATE FUNCTION f(V VARCHAR(20))RETURNS INTBEGIN INSERT INTO log_table VALUES (V, NOW()); RETURN 1;END
2008 CommunityOne Conference | developers.sun.com/events/communityone | 30
Views with functions
CREATE VIEW v1 ASSELECT x FROM table_nameWHERE f('v1') = 1;
2008 CommunityOne Conference | developers.sun.com/events/communityone | 31
Agenda
Database programming blues Stored routinesTriggersViewsEventsFederatedBlackholeCreative loopsRevisiting TriggersMySQL Proxy
2008 CommunityOne Conference | developers.sun.com/events/communityone | 32
Events - the not-so-boring facts
Temporal triggersOperating System independentexecutes SQL command or routine• AT timestamp• EVERY X time_interval
can execute dynamic SQL
2008 CommunityOne Conference | developers.sun.com/events/communityone | 33
Events
CREATE EVENT e1ON SCHEDULE AT NOW() + INTERVAL 10 minuteDO CALL clean_employee_recs();
2008 CommunityOne Conference | developers.sun.com/events/communityone | 34
Events
CREATE EVENT e1ON SCHEDULE EVERY 5 minuteDO CALL import_recs_from_file();
2008 CommunityOne Conference | developers.sun.com/events/communityone | 35
Agenda
Database programming blues Stored routinesTriggersEventsFederatedBlackholeCreative loopsRevisiting TriggersMySQL Proxy
2008 CommunityOne Conference | developers.sun.com/events/communityone | 36
Federated storage engine
no storageconnects to a table on a remote serversince version 5.1.22 can connect to a table on the same server
table t1ffederated
table t1MyISAM
2008 CommunityOne Conference | developers.sun.com/events/communityone | 37
Federated storage engine - caveats
no drop-in replacement for regular tablesinefficient aggregateslimited push-down conditions
2008 CommunityOne Conference | developers.sun.com/events/communityone | 38
Federated storage engine limited pushdown conditions
base table
federated table
SELECT ID FROM table_nameWHERE x = 10 SELECT ID
FROM table_name
expected~ 100 records returns
1 million recordsfilter
2008 CommunityOne Conference | developers.sun.com/events/communityone | 39
Federated storage engine workaround pushdown conditions
base table
federated table
SELECT ID FROM table_nameWHERE x = 10 SELECT ID
FROM table_nameWHERE x = 10
expected~ 100 records returns
100 records
add fake indexon column xto Federated table
2008 CommunityOne Conference | developers.sun.com/events/communityone | 40
Agenda
Database programming blues Stored routinesTriggersViewsEventsFederatedBlackholeCreative loopsRevisiting TriggersMySQL Proxy
2008 CommunityOne Conference | developers.sun.com/events/communityone | 41
Blackhole
The NON-STORAGE enginelike /dev/null in Unixused for triggers and replication relay
recordrecordrecordrecord binary log
triggers
2008 CommunityOne Conference | developers.sun.com/events/communityone | 42
Agenda
Database programming blues Stored routinesTriggersViewsEventsFederatedBlackholeCreative loopsRevisiting TriggersMySQL Proxy
2008 CommunityOne Conference | developers.sun.com/events/communityone | 43
More exciting triggers
Put them together• triggers• Federated engine• Blackhole engine
2008 CommunityOne Conference | developers.sun.com/events/communityone | 44
cascade triggers
BASE TABLEbefore insert
after insertbefore updateafter updatebefore deleteafter delete
t1
2008 CommunityOne Conference | developers.sun.com/events/communityone | 44
cascade triggers
BASE TABLEbefore insert
after insertbefore updateafter updatebefore deleteafter delete
t1
Federated TABLE
before insert
after insertbefore updateafter update
before deleteafter delete
t1_f1
2008 CommunityOne Conference | developers.sun.com/events/communityone | 45
triggers on request
BASE TABLE
Federated TABLE
t1
t1_f1
Federated TABLE
before insert
after update
after delete
before insert
after insert
before update
before deletet1_f2
either
or
2008 CommunityOne Conference | developers.sun.com/events/communityone | 46
blackhole triggersBASE TABLE
blackhole TABLE
before insert
t1_b1
BASE TABLE
t1
t2
2008 CommunityOne Conference | developers.sun.com/events/communityone | 47
remote executionblackhole
tableFederated
TABLE
t1 t1_f1
before insert
2008 CommunityOne Conference | developers.sun.com/events/communityone | 48
remote executionVIEW Federated
TABLE
v1 t1_f1
function
2008 CommunityOne Conference | developers.sun.com/events/communityone | 49
Agenda
Database programming blues Stored routinesTriggersViewsEventsFederatedBlackholeCreative loopsRevisiting TriggersMySQL Proxy
2008 CommunityOne Conference | developers.sun.com/events/communityone | 50
loops or why do we have to endure this?
loops with CURSORS• dreadful syntax• INEFFICIENT (temporary table created)
loops alternatives ... coming soon
2008 CommunityOne Conference | developers.sun.com/events/communityone | 51
loops with cursors :(
temporary table
data to process
crc
crc
loop
handler
flagcursor
2008 CommunityOne Conference | developers.sun.com/events/communityone | 52
loops with cursors (I) :(CREATE PROCEDURE p(j INT)
BEGIN DECLARE done BOOLEAN default 'false'; DECLARE crc VARCHAR(42) default ''; DECLARE cid INT; DECLARE x CURSOR FOR SELECT id FROM table_name WHERE b < j; -- to be continued
2008 CommunityOne Conference | developers.sun.com/events/communityone | 53
loops with cursors (II) :(( DECLARE CONTINUE HANDLER
FOR NOT FOUND set done = true; OPEN CURSOR x; search: LOOP FETCH x into cid; IF done THEN LEAVE search; END IF; -- do something with cid SET crc = MD5(CONCAT(crc,cid))END LOOP;SELECT crc;
2008 CommunityOne Conference | developers.sun.com/events/communityone | 54
loops with blackhole :)
blackhole table
data to process crc
crc
2008 CommunityOne Conference | developers.sun.com/events/communityone | 55
loops with blackhole (I) :)
CREATE TABLE t1 ( id VARCHAR(200)) ENGINE = BLACKHOLE;
SET @crc = '';SET @j = 10;
2008 CommunityOne Conference | developers.sun.com/events/communityone | 56
loops with blackhole (II) :)
INSERT INTO t1SELECT @crc := MD5(CONCAT(@crc, id)) FROM table_name WHERE b < @j;
SELECT @crc;
IT'S FASTER!!!
2008 CommunityOne Conference | developers.sun.com/events/communityone | 57
more loops alternatives
use the General Purpose Routine Library• https://sourceforge.net/projects/mysql-sr-lib/
use events! (ON SCHEDULE EVERY 1 SECOND ENDS NOW() + INTERVAL 10 SECOND)
use MySQL Proxy (later)
2008 CommunityOne Conference | developers.sun.com/events/communityone | 58
loops with MySQL stored routines librarycall for_each_table_value_complete(
'db_name', 'table_name', 'id', null, null, '', 'set @crc=MD5(concat(@crc,$I1))', 'set @crc=""', 'select @crc','', 'once');
2008 CommunityOne Conference | developers.sun.com/events/communityone | 59
Agenda
Database programming blues Stored routinesTriggersViewsEventsFederatedBlackholeCreative loopsRevisiting TriggersMySQL Proxy
2008 CommunityOne Conference | developers.sun.com/events/communityone | 60
MySQL ProxySo, what about "make coffee" ?
2008 CommunityOne Conference | developers.sun.com/events/communityone | 65
MySQL Proxy principles - hooks
2008 CommunityOne Conference | developers.sun.com/events/communityone | 65
MySQL Proxy principles - hooks
PROXY CORE
connection hook
read query hook
read result hook
2008 CommunityOne Conference | developers.sun.com/events/communityone | 65
MySQL Proxy principles - hooks
PROXY CORE
connection hook
read query hook
read result hook
function
Lua script
function
functionfunctionfunction
2008 CommunityOne Conference | developers.sun.com/events/communityone | 65
MySQL Proxy principles - hooks
PROXY CORE
connection hook
read query hook
read result hook
function
Lua script
function
functionfunctionfunction
2008 CommunityOne Conference | developers.sun.com/events/communityone | 66
MySQL Proxy principles - Lua
??Why not ...{Perl ?
PHP?Javascript?[whatever]?
2008 CommunityOne Conference | developers.sun.com/events/communityone | 67
MySQL Proxy principles - Lua
2008 CommunityOne Conference | developers.sun.com/events/communityone | 67
MySQL Proxy principles - Lua
• SMALL ( < 200 KB)• DESIGNED for
EMBEDDED systems• Widely used (lighttpd)
2008 CommunityOne Conference | developers.sun.com/events/communityone | 67
MySQL Proxy principles - Lua
• SMALL ( < 200 KB)• DESIGNED for
EMBEDDED systems• Widely used (lighttpd)
lighttpd, like MySQL Proxy, was created by Jan Kneschke
2008 CommunityOne Conference | developers.sun.com/events/communityone | 68
MySQL Proxy principles - Lua
Very popular among game writers
2008 CommunityOne Conference | developers.sun.com/events/communityone | 68
MySQL Proxy principles - Lua
Very popular among game writers
2008 CommunityOne Conference | developers.sun.com/events/communityone | 68
MySQL Proxy principles - Lua
Very popular among game writers
2008 CommunityOne Conference | developers.sun.com/events/communityone | 71
Debugging
server
client
proxy
diagnosticstext
2008 CommunityOne Conference | developers.sun.com/events/communityone | 72
Debugging scripts
server
client
proxy
diagnosticstext
proxy
diagnosticstext
2008 CommunityOne Conference | developers.sun.com/events/communityone | 73
Chained proxies - double features
server
proxy
pivot tablesproxy
loopsclient
2008 CommunityOne Conference | developers.sun.com/events/communityone | 74
Testing - customized packages
server
client
proxy
fake packetse.g.
connectors
2008 CommunityOne Conference | developers.sun.com/events/communityone | 75
MySQL Proxy - loops revisited
# get the script from MySQL Forge# http://forge.mysql.com/tools/tool.php?id=96
mysql-proxy \ --proxy-lua-script=loop.lua
2008 CommunityOne Conference | developers.sun.com/events/communityone | 76
MySQL Proxy - loops revisited
mysql -h 127.0.0.1 -P 4040
FOR VAR 1 3 CREATE TABLE t_$VAR (id int);
# creates t_1, t_2, t_3
2008 CommunityOne Conference | developers.sun.com/events/communityone | 77
MySQL Proxy - loops revisitedmysql -h 127.0.0.1 -P 4040
FOR VAR ( aa, bb cc) CREATE TABLE t_$VAR (id int);
# creates t_aa, t_bb, t_cc
2008 CommunityOne Conference | developers.sun.com/events/communityone | 78
Summary
MySQL is open to development creativityKnow your bricks• stored routines, triggers, views, events• dynamic engines (Federated, Blackhole)• MySQL Proxy
Put them together : be creative
2008 CommunityOne Conference | developers.sun.com/events/communityone | 79
For More Information
MySQL Forge ( http://forge.mysql.com )my blog ( http://datacharmer.blogspot.com )MySQL blogs ( http://planetmysql.org )