You are on page 1of 437

Inside MySQ L: InnoDB Storage Engine , Second Edition

yS L

China Machine Press


‘,...'1.". .
••

Inside MySQL: InnoDB Storage Engine, Second Edition

\'\
._.

ISBN 978-7-111-42206-8

I. M… lV. TP31 1.1 38

100037)

ISBN

(010) 88378991 88361066 (010) 88379604


(010) 68326294 88379649 68995259 hzjsj@hzbook.com
..ttl.CA
..'- -,.

It' s fair to say that MySQL is the most popular open source database. It has a very large
installed base and number of users. Let' s see what are the reasons MySQL is so popular, where
it stands currently, and maybe touch on some of its future (although predicting the future is rarely
successful).
Looking at the customer area of MySQL, which includes Facebook , Flickr, Adobe (in
Creative Suite Drupal , Linkedln, Wikipedia, eBay, YouTu be, Google AdSense (source
http://mysq l. com/customers/andpublicresources) , it' s obvious that MySQL is everywhere.
When you log in to your popular forum (powered by Bulleting) or blog (powered by WordPress),
most likely it has MySQL as its backend database. Traditionally, two MySQL' s characteristics,
simplicity of use and performance, were what allowed it to gain such popularity. In addition to that,
availability on a range of platforms (including Windows) and built-in replication, which
provides an easy scale-out solution for clients, gave more user attractions and production
deployments. There is simple evidence of MySQL' s simplicity: In 15 minutes or less, you really
can get installed, have a working database, and start running queries and store data. From its early
stages MySQL had a good interface to most popular languages for Web development - PHP and
Perl, and also Java and ODBC connectors.
There are two best known storage engines in MySQL: MyISAM and InnoDB (1 don' t cover
NDB cluster here; it' s a totally different story). MylSAM comes as the default storage engine
and historically it is the oldest, but InnoDB is ACID compliant and provides transactions, row-
level locking , MVCC , automatic recovery and data corruption detection. This makes it the
storage engine you want to choose for your application. Also , there is the third-party transaction
storage engine PBXT, with characteristics similar to InnoDB, which is included in the MariaDB
distribution.
MySQL ' s simplicity has its own drawback. Just as it is to working with it, it is
very easy to start into trouble with it. As soon as your website or forum gets popular, you
may fìgure out that the database is a bottleneck, and that you need special skills and tools to fìx it.
IV

of this
highly recommend new oflnnoDB well who already have well-tuned
InnoDB-based applications but need to get intemal out of them.

Vadim Tkachenko
C...

C:::::J

Different. Think

Different ,
VI

DBA

MySQL
w

buffer
C...
VIII

Peter Vadim
C...

2.5.2 InnoDB
Master Thread.................... ..

2.5.3 Thread
2.6 ……………… 0
1.1 2.6.1 · …………… 46
1.2 …3 2.6.2
1.3
2.6.3
1.3.1 .. ..6
2.6.4 …
.....57
1.3.2 …… ..7
2.6 .5 ………… ........58
1.3 .3
2.7 .. … … …..........58
1.3 .4 … … ...8
2.8
1.3.5
1.3 .6
1.3 .7 · ….................... ..9 3.1

1.3 .8 … u …… .....9 3. 1.1


1.4 3. 1.2
1. 5 … ............… … ............… 13 3.2 … … ................………… …........65
1.5.1 TCP/I P.. …… .....……………"…13 3.2. 1
1.5 .2 ……15 3.2.2
1.5 .3
3.2.3
……………… .............. ..15
3.2 .4
3.3 …… ….........………… ……….-83
2.1 .... 17
3 .4
2.2
2.3 …............,....……......…19 3.5 ……·……………....…… 84

2.3. 1 3.6

2.3.2 3.6.1 …… ........... ……85


2 .4 3.6.2
2.5 Master 3.7
2.5.1 InnoDB
Master Thread..................................................36 4.1 ......91
11·
-
X 1


4.2 4.8.1
4.2. 1 4.8.2
4.8.3
4.8.4
4.8.5
4.8.6
4.3
4.3.1
4.3.2 5.1
4.3.3 …………..… 110 5.2
4.3.4 5.2.1
5.2.2 5
4.3.5 B+ ..……………
..
4 .4 5.3. 1
4 .4.1 Fi1e Header……………………… 121 5.3.2 ……
.... O
4 .4.2 Page Header…........…·…................ ................122 5 .4
4.4.3 Supremum Records........123 5.4.1
4 .4.4 User Space......… ......123 5.4.2
4 .4.5 Page Directory ……………… ..............124 5.4.3
4 .4.6 File Trailer"…………....... …..………....… ..124 5.4.4
4 .4.7 5.5
4.5 Named Fi1e 5 .5 .1
4.6 .. ..……………
....…………
..………
.... .. .. .. …
.... 5 .5 .2

4.6.2 - …….135 5.6 ……...215


4.6.3 5.6.1 ..215
4.6.4 5.6.2 ……·… 215
4.6.5 … 139 5.6.3 ..218
4.6.6 ....139 5.6 .4 ...219
4.6.7 5.6.5 l
4.7 Multi-Range .. ..223
4.7.1 5.6.7 Index Condition Pushdown <ICP )
4.7.2 …… ..147
4.8 ..………
……
..…....……………"…..…………
.. .. .. .... 2 5.7
C...

5.7.1 7. 1.1
5.7.2 7. 1. 2 ..
5.7 .3 7.2
5.8 7.2.1 redO'.............….........................…… ... .294

5.8.1 … 231 7.2.2 undo..... .......…………..................... … ..305

5.8.2 7.2.3 purge- … … … ………… ....………317


5.8.3 7.2.4 group commit............................................... ..319
5.8.4 · ………….............".....… 240 7.3
5.9 7.4
.... …… ……...........………… ..249
e 7 .5
6.1 7.6
6.2 7.7
6.3 7.7.1 5
6.3.1 7
6.3.2 ………258 7.8 …………
....………
..……
….... .. ….
.............................….......-3 41

6.3.3 …… ....261 7.8.1


6.3.4 … 262 7.8.2
6.3.5 7.8.3
6 .4 7.9

6.4 .1 ………·…"…265 7.10 … ........ .349

6 .4.2 Problem' …….....269


6 .5 8 .1
6.5 .1 8.2

6.5.2 8.3 …… ......….........................353


6.5.3 …… … … e … … ....274 8.3.1 mysqldump'....…….
……
..... ....
6.6 8.3.2 SELECT.. .lNTO OUTFILE- ................360
6.7 8.3.3 ..…………
…………
....……
..……
.. ..

6.7.1 8.3 .4 LOAD DATA INFILE..…………...362


6.7.2 8.3.5 mysqlimport............. ……… ....................364
6.7.3 8.4
6.8 8.5
6.9 8.5. 1 ibbackup' ………...............………..………-367
8.5.2 XtraBackup '……........................… …368 h

7.1 8.5.3
XII

8.6
8.7 9.7

8.7.1 ……….-3 76 9.7.1 sysbench ………… .........…........……....... ….399

8.7.2 9.7.2 mysql-tpc c- … ..….....................…… 405


8.8 ......... …..…...-3 82 9.8

9.1 ………....….-383
9.2 10.1
9 .3 10.2
9.3.1 10 .3 MySQL
9.3.2 … …… …....,.........………......…415
9.4 10.3 .1
9.4 .1 10.3.2
9.4 .2 RAID Write 10.4
9.4 .3 ….....................…........……… · ….423
9.5 10.5
Linux ,

1.1

C
C

(root@xen-server binJf ./mysqld_safe&

(root@xen-server binJf ps -ef I grep mysqld


2

root 3441 3258 0 10:23 pts!3 00 : 00 : 00 !bin!sh


mysql 3578 3 44 1 0 10:23 pts!3 00 :00:0 0
!usr!local!mysq l!libexec!mysqld --basedir=!usr!local!mys q l
--da tadir-!us r! local!mysql !var --use r=mysql
--log-error- !usr!local!mysql!var! xen-server.err

--socketa !tmp!mysql .sock --port=3306


root 3 616 3258 0 10:27 pts!3 00:00:00 grep mysqld

[root@xen- serve r bin)' mysql --help I grep my.cnf


order of preference , my.cnf , $MYSQL_TCP_PORT ,
!et c!my.cnf !etc!mysql!my .cnf ! usr! local!mysq1 !etc!my. cnf

- l etc/mysql/my.cnf • lusr/locallmysqll
etc/my.cnf • ,...,

Default options are read from the following files in th e g ive n order:
C : \Windows\my.ini C:\Windows\my.cnf C:\my. ini C:\my.cnf C:\program Files\

\ MySQL Server 5.1 \my.cnf


1.2

mysql>SHOW VARIABLES LIKE 'datadir'\G;


1. row ..

Variable name: datadir


Value: /usr/local/mysql/data/
1 row in set (0.00 sec)1 row in set (0.00 sec)

mysql>system ls- lh /usr/local/mysql/data


total 32K
drwxr-xr-x 2 root mysql 4.0K Aug 6 16:23 bin
drwxr-xr-x 2 root mysql 4.0K Aug 6 16:23 docs
drwxr-xr-x 3 root mysql 4.0K Aug 6 16:04 include
drwxr-xr-x 3 root mysql 4.0K Aug 6 16:04 lib
drwxr-xr-x 2 root mysql 4.0K Aug 6 16:23 libexec
drwxr-xr-x 10 root mysql 4.0K Aug 6 16:23 mysql-test
drwxr-xr-x 5 root mysql 4.0K Aug 6 16:04 share
drwxr-xr-x 5 root mysql 4.0K Aug 6 16:23 sql-bench
lrwxrwxrwx 1 root mysql 16 Aug 6 16:05 data -> /opt/mysql_data/

1.2

o
... ...
4 -."

Native C API. JDBC. ODBC. NET. PHP, PerI, Python. Ruby, Cobol

Management Conncction Pool


Service & Authentication. Thead Reuse. Connection Limits. Chcck Mcmory, Cacbcs
Utillties
SQL Interface Parser Optimizer Cathes & Buffers
DML, DDL. Stored Quary Translation Access Paths , Olobal and Engine
Proccdurcs Vicws, Objcct Privilcge Statistics Specífie Caches &
Triggers, etc Buffers

Zj @

File system
ufs,
,_;;;.-- NFS. SAN, NAS
1.3 5 4b

1.3

dynamìc-rowsl
C...
6

COnline Transaction

1.3.1

4.1

insert double hash


read

_ _ buffer_pool
..{)I.CA

3
Yahoo

1.3.2

MySQL

1.3.3
C...
8 ‘'
share

key

MySQL NDB
Grade

1.3 .4

(
..'i I.CA
I.3

1.3.5

1.3.6

1.3.7

1.3.8

(
o
10
._.

1.4
mysql>SHOW ENGINES\G;
1. row
Engine : InnoDB
Support: YES
Comment: Supports transactions , row-level locking , and foreign keys
Trar>sactions: YES
XA: YES
Savepoints: YES
***** ******* ******* ******* * 2. row
Engine: MRG_MYISAM
Support: YES
COmment: Col lection of identica l MyISAM tables
Transact ions: NO
XA: NO
Savepoints: NO
*************************** 3. row
Engine: BLACKHOLE
Support: YES
Comment: /dev/null storage engine (anything you write to it disappears)
Transactions: NO
XA: NO
Savepoints : NO
4. row
Engine: CSV
Support : YES
Comment: CSV storage engine
Transactions: NO
XA: NO
Savepoints: NO
5. row
Engine: MEMORY
Support : YES
12

Comment: Hash based , stored in memory , useful for temporary


Transactions: NO
XA: NO
Savepoints: NO
******************* ******** 6. row
Engine: FEDERATED
Support: NO
Commen t: Federated MySQL storage engine
Transactions: NULL
XA: NULL
Savepoints: NULL
7. row

Engine: ARCHIVE
Support: YES
Comment: Archive storage engine
Transactions: NO
XA: NO
Savepoints: NO
8 . row ***...………...*********

Engine: MyISAM
Support: DEFAULT
Comment: Default engine as of MySQL 3.23 with great performance
Transactions: NO
XA: NO
Savepoints: NO
8 rows in set (0.00 sec)

mysql>CREATE TABLE mytest Engine=MyISAM


->AS SELECT * FROM salaries ;
Query OK , 2844047 rows affected (4.37 sec)
Records: 2844047 Dup1icates: 0 Warnings: 0

mysq1>ALTER TABLE mytest Engine=InnoDB;


Qu ery OK , 2844047 rows affected (15.86 sec)
Records : 2844047 Duplicates: 0 Warnings: 0

mysq1>ALTER TABLE mytest Engine=ARCHIVE;


Query OK , 2844047 rows affected (16.03 sec )
Records : 2844047 Dupl icates : 0 Warnings: 0
..{,I. CA
13- .

1.5

1.5.1 TCP/IP

C:\>mysql -h192. 168.0.101 -u david -p


Enter password:
Welcome to the MySQL monitor. Commands end wi th ; or \g.
Your MySQL connection id is 18358
.{) I. CA
14 --.

Server version: 5.0.77-10g MySQL Community Server (GPL)


Y‘ UJ
Type 'he1p;' or '\h' for he1p.Type '\c' to c1ear the current input statement.

mysq1>

mysq1>USE mysq1;
Database changed
mysq1>SELECT host , user , password FROM user;
1. row
host :
u ser: root
password : *75DBD4FA548120B54FE693006C41AA9A16DE8FBE
2. row
host: nineyouO-43
user: root
password : *75DBD4FA548120B54FE693006C41AA9A16DE8FBE
**************************** 3. row
host: 127.0.0.1
user: root
password: *75DBD4FA548120B54FE693006C41AA9A16DE8FBE
4. row
host: 192 . 168 .0.100
user: zlm
password: *DAE0939275CC7CD8E0293812A3 1 735DA9CF0953C
**************************** 5. row

user: david
password:
5 rows in set (0.00 sec)
.ttl.CA

1.5.2

XP , Windows
Microsoft

SQL

1.5.3

mysq l >SHOW VARIABLES LI KE 'soc ket';


1 . row
Variable name: socket
Va lue: /tmp/mysql.sock
1 row in set (0.00 s ec)

(root@stargazer -]t mysq l -udavi d - S /tmp/mysql.sock


Welcome to the MySQL mon itor. Commands end with ; or \g.
Your MySQL connect ion id is 20333
Server version: 5.0.77- 1og MySQL Community Server (GPL)

Type 'help;' or '\h' fo r hel p. Type '\c' t o clear t h e buffer.

mysql>

1.6
16
., I.C...

2.1

Heikki Tuuri

e
.{, I.C...
18

2.2

JnnoDB I.O.x
InnoDB I.l.x
JnnoDB 1.2.x

InnoDB

Native
InnoDB
lnnoDB
2.3

2.3

…...

2.3 .1
C...
20 fIa' --.

1 . Master Thread
Master

2.10 Thread
(Async
back)

10 10
read
io
write io
mysql>SHOW VARIABLES LIKE ' innodb_version'\G ;
*************************** 1. row
Variable name: innodb version
Value: 1. 0.6
1 row in set (0.00 sec )

mysql>SHOW VARIABLES LIKE threads'\G;


1. row ***************************
Variable name: innodb read io threads
Va l ue: 4
2. row
Variable name: in nodb write io threads
Value: 4
2 rows in set (0.00 sec)

Thread:
mysql>SHOW ENGINE INNODB STATUS\G;
1. row
Type: InnoDB
Name:
Status:
=====================================
100719 21:55:26 INNODB MONITOR OUTPUT
=====================================
ð. \I , CA
2.3 21"'.

per second averages calculated from the last 36 seconds


••

F1LE 1/0

1/0 thread 0 state: waiting for i/o request (insert buffer thread)
1/0 thread 1 state: waiting for i/o request (10g thread)
1/0 thread 2 state: waiting for i/o request (read thread)
1/0 thread 3 state: wa iting for i/o request (read thread)
1/0 thread 4 state: wai ting for i/o request (read thread)
1/0 thread 5 state: waiting for i/o request (read thread)
1/0 thread 6 state: wa iting for i/o request (write thread)
I/O thread 7 state: waiting for i/o request (write thread)
1/0 thread 8 state: waiting for i/o request (wri te thread)
I/O threa d 9 state: waiting for i/o request (write thread)

END OF 1NNODB MON1TOR OUTPUT

1 row in set (0.01 sec)

Thread buffer threado 10 Thread

3. Purge Thread

Thread:
yn
mn lp
sod-
u r qe h r ea dqu -
_p urge_

12052922:54:16 [Warning) option 'innodb-purge-threads ': unsigned value 4 adjusted to 1


.{)I.C...
22 #I:a" -.

Thread:
mysql> SELECT VERSION()\G;
1 . row
VERSION(): 5.6.6
1 row in set (0 . 00 sec )

mysql> S HOW VARIABLES LIKE 'innodb_p urge_threads'\G;


1.
Variable_name : i nnodb_ purge_ threads
Valu e : 4
1 row i n set (0.00 sec)

4. Page Cleaner Thread


Page Cleaner

2.3.2

.
_ buffer_p ool_

mysq1>SHOW VARIABLES LIKE 'innodb_ buffer_poo 1_size'\G;


1. row
Variab1e_name: innodb_buffer_poo1 _ size
Va1ue : 16106127360
1 row in set (0.00 sec)

(insert hash

_ buffer_pool)
(redo log_buffcr)
page)
( insert buffer) info)

index page)
(innodb additional
24 ‘'
_ buffer_p ool_ ••
mysql> SHOW VARIABLES LIKE ' innodb_buffer_pool _ instances ' \G;
1. row
Variable_name: innodb_buffer_pool_ i nstances
Value: 1
1 row in set (0.00 sec)

_ buffer_pool_

mysql> SHOW ENGINE INNODB STATUS\G;


1. row ******************* ********
Type: InnoDB

INDIVIOUAL BUFFER POOL INFO

… BUFFER POOL 0
Bu ffer pool size 65535
Free buffers 65451
Database pages 84
01d database pages 0
Modified db pages 0
Pending reads 0
Pending writes: LRU 0 , flush list 0 single page 0
Pages made young 0 , not young 0
0.00 youngs/s , 0.00 non-youngs/s
Pages read 84 , created 0 , written 1
9.33 reads/s , 0.00 creates/s , 0.11 writes /s
Buffer pool hit rate 764 1 1000 , young-making rate 0 1 1000 not 0 / 1000
Pages read ahead O.OO/s , evicted without access O.OO/s , Random read ahead O.OO/s
LRU len: 84 , unzip_LRU len : 0
1/0 sum[O] :cu r [O ] , unzip sum (O]:cur(O]
---BUFFER POOL 1
Buffer pool size 65536
Free buffers 65473
Database pages 63
Old database pages 0
Modified db pages 0
Pending reads 0
pending writes: LRU 0 , flush list 0 s ingle page 0
Pages made young 0 , not young 0
0.00 youngs/s , 0.00 non-youngs/s
..{,I. CA
2.3 25--'

Pages read 63 , created 0 , written 0


7.00 reads/s , 0.00 creates/s , 0.00 writes/s
Buffer pool hit rate 500 / 1000 , young-making rate 0 / 1000 not 0 / 1000
Pages read ahead O.OO/s , evicted without acceS5 O.OO/s , Random read ahead 0.00/5
LRU len: 63 , unzip_LRU 1en: 0
1/0 5um[O):cur(O) , unzip 5um[OJ :cur(O)

_ buffer_pool_
ENGINE INNODB
POOL

BUFFER POOL

my5q1> SELECT POOL_ID , POOL_SIZE ,


-> FREE BUFFERS , DATABASE PAGES
-> FROM 1NNODB BUFFER POOL STATS\G;
1 . row ***************************
o-FP
OLU
POBE DESS
-ZEG
O-S ......
554
EJauz

EB
RA
FT
DA

2. row

POOL 10 : 1
POOL SIZE: 65536
FREE BUFFERS: 65473
DATABASE PAGES: 63

2. LRU List

(Latest Recent
26 ._'- --.
..{)I.CA

insertion
_old_ blocks
mysql> SHOW VARIABLES LIKE 'innodb_ o ld_blocks_pct'\G;
1 . row
Variable_ name: innodb_ old_ blocks_ pct
Va lue: 37
1 row in set (0.00 sec)

_ old_ blocks

mysql> SET GLOBAL i nnodb old blocks time=1000;


Query OK , 0 rows affected (0.00 sec)

• data or index scan operation

mysq l > SET GLOBAL innodb old blocks t ime= O;


..\I , CA
2.3

Query OK , 0 rows af f ecte d (0.00 sec) ••

mysql> SET GLOBAL innodb_01d _b locks _p ct=20 ;


Query OK , 0 rows affec ted (0.00 sec)

made

page not made ENGINE INNOOB

mysql> SHOW ENGINE I NNODB STATUS\G;


1. row
Type: lnnoOB
Name:
Status:

120725 22:04:25 lNNOOB MON ITOR OUTPUT

Per second averages calculated from the last 24 seconds

Buffer pool size 327679


Free buffers 0
Oatabase pages 3077 1 7
Old database p ages 113570
Modified db pages 24673
Pending reads 0
Pending wr ites: LRU 0 , flush list 0 , single page 0
Pages made young 64 4 8526 , not young 0
48.75 youngs/s , 0.00 non-youn gs / s
Pages read 535 44 20 , created 239625 , written 3 48 6063
55.68 re ads/s , 81.7 4 creates /s , 955.88 writes/s
Buffer poo l h i t rate 1000 / 1000 , young-making rate 0 I 1 000 not 0 I 1000
28 4- ..,.

ENGINE INNODB

Database
Database pool

pages made

pool hit

pool hit

Per
second averages calculated the last 24

mysql> SELECT POOL_ ID , HIT_RATE ,


-> PAGES MADE YOUNG , PAGES NOT MADE YOUNG
-> FROM information schema.INNODB BUFFER POOL STATS\G;
*************************** 1. row
P&T O L-M nu

E
& y< ao

PAGES MADE YOUNG: 450


PAGES NOT MADE YOUNG: 0

mysql> SELECT TABLE_NAME , SPACE , PAGE_NUMBER , PAGE_TYPE


-> FROM INNODB BUFFER PAGE LRU WHERE SPACE = 1;
C...
2.3 29--'

+------------+-------+…----------+------------……-+
I I
TABLE NAME SPACEI I
PAGE NUMBER PAGE TYPE
+------------+--- ----+-------------+-------------------+
NULL 1 FILE SPACE HEADER
NULL 1 l IBUF BITMAP
NULL 1 2 INODE
test/tl 1 3 INDEX
+------------+-------+-------------+-------------------+
4 rows in set (0.00 secl

ENGINE

mysql> SHOW ENGINE INNODB STATUS\G;

Buffer pool hit rate 999 / 1000 , young-making rate 0 / 1000 not 0 / 1000
Page5 read ahead 0.00/5 , evicted without acce55 0.00/5 , Random read ahead 0.00/5
LRU 1en: 1539 , unzip_LRU 1en: 156
1/0 5um(O) :cur(O) , unzip 5um(0] :cur[Ol

1
C...
30

mysql> SELECT
-> TABLE_NAME , SPACE , PAGE_ NUMBER, COMPRESSED_ SIZE
- > FROM INNODB BUFFER PAGE LRU
-> WHERE COMPRESSED SIZE <> 0;

I TABLE_ NAME I SPACE I PAGE_NUMBER I COMPRESSED_SIZE I


+- -------- ---+-------+----------- --+--------- --------+
sbtest/t 9 134 8192
sbtest/t 9 135 8192
sbtest/t 9 96 8192
sbtest/t 9 136 8192
sbtest/t 9 32 81 92
sbtest/t 9 97 8192
sbtest/t 9 137 8192
sbtest / t 9 98 8192

ENGINE INNODB STATUS


db pages

mysql> SELECT TABLE NAME , SPACE , PAGE NUMBER , PAGE T YPE


-> FROM INNODB BUFFER PAGE LRU
-> WHERE OLDEST MODIFICATION> 0 ;

I TABLE NAME I SPACE I PAGE NUMBER I PAGE TYPE

+-…----------+-------+
I NUL L
---…-------+-------------------+
-
56 I SYSTEM
---
I NULL o I FILE SPACE HEADER I
test/t 3 I INDEX
NULL 320 I INODE
23

I NULL o I 325 I UNDO LOG


+-------- - ---+- - - ---- +---- --------- +-- -------------- ---+
5 rows i n se t (0.00 s e c)

log buffer) 0

_log_ buffer_
8MB:
mysq l > SHOW VARIABLES LIKE 'innodb_ log_ buffer_si ze'\G;
1. row
Variable_ name: i n nodb_ l o9_buffer_s ize
Value: 8388608
1 r ow in set (0.00 sec)

o Master

buffer
(buffer control
32

2 .4

Ahead

Oracle
...

( Log Sequence

ENGINE INNODB
mysql> SHOW ENGINE INNOOB STATUS\G;

LOG

Log sequence number 92561351052


Log flushed up to 92 561351 052
Last checkpoint at 92561351052
..{,I. CA
34

o Sharp Checkpoint
o Fuzzy Checkpoint
Sharp

Checkpoint:
o Master Thread Checkpoint
o FLUSH_LRU_LIST Checkpoint
o Async/Sync Flush Checkpoint
Page too much Checkpoint
Thread

FLUSH_ LRU_LIST

LRU _LIST

_lru_ scan_
...

mysql> SHOW VARIABLES LIKE 'innodb_ lru_scan_ depth'\G;


1. row
Variable_name: innodb_lru_scan_depth
Value: 1024
1 row in set (0 . 00 sec)

Async/Sync Flush

checkpoi nt_ age = redo_ lsn - checkpoint _ lsn

async_water_ma r k = 75% .. total_ redo_log_ fi l e _ size


sync_ water_mar k - total_ redo_ l og_file_size

_ water_mark= 1.5GB , sync_

age<sync_water_
age<async_ water_
mark:
o checkpoint_ age>sync_water_

checkpoint_age<async_
Async/Sync Flush
InnoDB Async Flush
Sync Flush

Page Cleaner
36 . -.
6.() I. c...

mysql> SHOW ENG1 NE INNOOB STATUS\G;


1.
Type: 1nnoOB

LRU len : 112902 , unzip_LRU len : 0


1/0 sum[Oj :cur[Oj , unzip sum[O):cur[Oj
A.ync Flu.h : 0 , Sync Flu.h : 0 , LRU Li.t Flu.h : 0 , Fluah Li.t Flu.h : 111736

1 r ow in set (0.01 sec)

too

_ max_ dirty_p ages

mysql>SHOW VAR1ABLES LIKE 'innodb_ max_dirty_pages_pct ' \G;


1. row
Var i able_ name: innodb_max_dirty_p ages _p ct
Va l ue : 75
1 row in set (0 . 00 sec)

innodb_ max_ dirty_p ages

2.5 Master

2.5.1 InnoDB Thread

Master
(
Master
2'.5 Master
., I.C...

void master thread() {


loop:
for (int i = 0; i<10; i++) (
do thing once per second
sleep 1 second if necessary

do things once per ten seconds


goto 1oop;

modified_ratio _ max_
dirty_p ages
C...
38

void master thread() {


goto loop;
lOOp:
for (int i = 0; i<10; i++ ) {
thread_sleep(l) // sleep 1 second
do log buffer flush to disk
if (last one second ios < 5 )
do merge at most 5 insert buffer
if ( buf_get_modified_ratio_pct > innodb_max_dirty_pages_pct )
do buffer pool flush 100 dirty page
if ( no user activity )
goto backgroud loop

db
oahqoo
--Et
tkdqquo eon
oto
noso
snml
nlhpe E --Wen s econAUq
p--- >

c deoP

qd
....

modified_ratio_p ct ) .

void master thread () {


goto 100p ;
100p:
for ( int i a 0 ; i<10 ; i++ ) (
thread_ s1eep ( 1) 11 s1eep 1 second
do 10g bu ffer fiush to dis k
if (1ast one second ios < 5 )
do merge at most 5 inse rt b uffer
if ( buf_ get_modified_ ra ti o _ pct > innodb_ ma x_dir ty_ p ages_ pct
do buffer po01 flu sh 100 dir t y page
if ( no user activity )
goto bac kg r oud 100p

if ( 1ast_ten_second_ios < 200 )


do buffer po01 flush 1 00 dirty page
do merge at most 5 inse rt buffer
do 10g buffer flush to disk
do full purge
if ( buf_ get_ modified_ratio_ pct >
d o buffer po01 flush 100 dirty page
e1se
buffer po01 flush 10 d i rty p a ge
go to 100p
bac kground 100p:
do something
goto 100p :

( shutdown
I. CII _
40

flush r

Master

void master thread() I


goto 100p;
100p:
for(int i =
0; i<10; i ++ ) I
thread_s1eep( 1 ) // s 1 eep 1 second
do 10g buffer flush to d1sk
if ( 1ast one second ios < 5 )
do merge at most 5 insert buffer
i f ( bUf_ get_modified_ ratio_pct > innodb_ max_dirty_pag es _ pct
do buffer po01 flush 100 dirty page
if ( no user act i v ity )
goto backgro ud 100p

if ( 1ast ten second 105 < 200 )


do buffer po01 flush 100 dirty page
do merg e at most 5 inser t buffer
do 10g bu ffer flush t o disk
do full purge
i f I bUf_ get_ modified_ ratio_ pct >
do buffer po01 flush 100 dirty page
e l se
buffer po01 flu s h 10 dirty page
goto 100p
background loop :
do full purge
do merge 20 inser t buffer
if not i d 1e:
goto loop:
else:
go to f1 ush loop
flush 1 00p:
do buffer po01 flush 100 dirty page
if ( buf_get_ modified_ra tio_p ct>innodb_max_ dirty_p ages_pct
goto flush loop
goto suspend 100p
.'i I.CA
2 5 Master

ddqo
ltep
ee--
nnni
sswqd
uuao
D-P ore
--at
p-en
ohVJ
AU{
‘ -
to
o

2.5.2 Thread

Master

coding. Master

Plugin
_ io_ capacity
_io_
_ io_
_io_ capacity 0

_ io_

_pages 1.0.x
42

innodb_ max_dirty_p ages

_ max _dirty_p ages

innodb_ max_ dirty_pages

InnoDB _adaptive_flushing

innodb_ max_dirty_p ages _ max_ dirty_p ages


_ adaptive_
_ flush _get_desired_ ßush
_flush_get_desired_flush
_ max_dirty_pages _

InnoDB _purge_ batch_

mysql> SHOW VARIABLES LIKE 'innodb_ purge_batch_ size'\G;


1. row ***************************
Variable_name: innodb_ purge_ ba tch_size
Value: 20

mysql> SET GLOBAL innOdb_ purge_ batch_ size=50;


Query OK , 0 rows affec ted (0.00 sec)
2.5

Master

void master thread() {


goto loop;
loop:
for (int i = 0; i<10; i++) (
thread_sleep(l) // sleep 1 second
do 10g buffer flush to disk
if ( 1ast_one_second_ios < )
do merge insert buffer
if ( bUf_get_modified_ ratio_ pct > innodb_max_ dirty_pages_pct
do buffer poo1 flush dirty page
e1se if enab1e adaptive f1 ush
do buffer poo1 flush desired amount dirty page
if ( no user activity )
goto backgroud loop

if ( la st_ten_ second_ios <innodb_io_ capacity)


do buffer pool flush dirty page
do merge insert buffer
do 10g buffer flush to disk
do full purge
i f ( bUf_get_modified_ratio_p ct > 70% )
do buffer po01 flush dirty page
e lse
dobuffer pool flush 10% innodb_io_capacity dirty page
goto 100p
background loop:
do full purge
do merge insert buffer
i f not i d1e:
goto loop:
e1se:
goto flush loop
flush loop:
do buffer pool flush 100% innodb_i o_capacity dirty page
if (
go to flush loop
goto suspend 100p
suspend loop:
suspend thread ()
wa i ting event
goto 100p;
.{,I. CA
44

InnoDB

ENGINE INNODB

mysq1>SHOW ENGINE INNODB STATUS\G;


1. row *.****‘********************
ees
pmu
yat
TNa vi n n o nuRM
--····

na

=====================================
090921 14:24:56 INNODB MONITOR OUTPUT
=====================================
Per second averages ca1cu1ated from the 1ast 6 seconds

BACKGROUND THREAD

srv_master_thread 1oops: 45 1_second , 45 s1eeps , 4 10_second , 6 background , 6 flush


srv_ master_ thread 10g flush and writes: 45 1 0g writes on1y: 69

: 100 background
ftush

mysq1> show engine innodb status\G;


1. row
Type: InnoDB
Name:
Status:
=====================================
091009 10:14:34 INNODB MONITOR OUTPUT
---------------- - - ----------- --------
Per second averages ca1cu1ated from the 1ast 42 seconds

BACKGROUND THREAD

srv_master_thread 1oops: 2188 1_second , 1537 sleeps , 2 18 10_second , 2


background , 2 flush
.() I. C...

srv_master_thread 10g flush and writes: 1777 og ".,0._.4.-'

2.5.3 InnoDB Thread

Master

if InnoDB is id1e
srv_master_do_id1e_tasks() ;
e1se
srv_mast er_do_active_tasks();

_ srv master do
active_ Thread
Cleaner

2.6

Insert Buffer)
Double Write)
Hash Index )
( Async 10 )
Neighbor Page )
46
._'- -.
.ttl.CA

2.6.1

1.lnsert Buffer
Insert

Buffer

CREATE TABLE t (
a I NT AUTO INCREMENT ,
b VARCHAR (30) ,
PRlMARY KEY(a)

ATR
PM TNA
RPM ATab LAM
BC
FHHURH

YAHV TR N}
FUPURLMFUNT
,
'

PRI MARY KEY(a) ,


key (b)
2.6

Buffer

mysql>SHOW ENGINE INNODB STATUS\G;


1. row
.{,I. CA
48

Type: InnoDB
Name:
Status:

100727 22:21:48 INNODB MONITOR OUT PUT


=====================================
Per second averages ca1culated from the 1ast 44 seconds

INSERT BUFFER AND ADAPTIVE HASH INDEX

Ibuf: size 7545 , free 1ist 1en 3790 , seq size 11336 ,
8075308 inserts , 7540969 merqed recs , 2246304 merqes

END OF INNODB MONITOR OUTPUT


============================

1 row in set (0.00 sec)

seg free list

merged
:

innodb_ buffer_pool)

1** Buffer pool size per the maximum insert bu ffe r size *1
IIdefine IBUF POOL SIZE PER MAX SIZE 2
ibuf->max_size = buf_pool_get_curr_size() 1 UNIV_ PAGE_ SIZE
/ IBUF POOL SIZE PER MAX SIZE;

IBUF POOL SIZE PER MAX


2.6 49 9

2. Change Buffer

Insert
Change

_change_

_ change_ buffer_ max_


Change

mysql> SHOW VARIABLES LIKE 'innodb_change_buffer_max_size'\G;


1. row
Variab l e_name: innodb_change_buffer_max_size
Value: 25
1 row in set (0.00 sec)

innodb_change_buffer_max_

mysql> SHOW ENGINE INNODB STATUS\G;


* 1. row ..... * ...
Type: InnoDB

INSERT BUFFER AND ADAPTIVE


50

------------------------
-------------------
Ibuf: size 1 , f ree list len 34397 , seg size 34399 , 1087 5 merges
merged operations:
insert 20462 , delete mark 20158 , del ete 421 5
discarded operations :
insert 0 , delete mark 0 , delete 0

Buffer; delete
Buffer; Buffer; discarded

Insert
MySQL Buffer
Buffer

REPAIR
Insert
key

space marker offset

Insert key

search

Buffer
. 6.{, I.C...
2 .6

Buffer
Buffer

| I I
\
secondary index record

lnsert

IBUF REC OFFSET COUNT 2

IBUF REC OFFSET TYPE

IBUF REC OFFSET FLAGS

IBUF
Insert

Insert Buffer
space , page_
Insert
space , page_ 00
Buffer
Buffer
Buffer

Buffer
C...
52

JBUF BJTMAP FREE 2

JBUF BITMAP BUFFERED Buffer


JBUF BITMAP JBUF Buffer

4. Merge Insert Buffer

Insert

o Insert Buffer
o Master

Buffer
Buffer Buffer

Insert Buffer

Buffer

Insert
2.6

srv

BufTer

BufTer

Change

2.6.2

page
54

doublewrite buffer
(2MB)

wntc

doublewrite doublcwrite
( 1MB) ( 1MB)

mysq l >SHOW GLOBAL STATUS LIKE \G;


1 . row
Variable_name: I nnodb_dblwr _pages_written
Value: 6325194
*************************** 2. row
Variable name: Innodb dblwr writes
Value: 100399
2 rows in set (0.00 sec)

: _ dblwr_p ages_ written:lnnodb_ dblwr_

090924 11:36:32 mysqld restarted


090924 11:36:33 InnoDB : Database was not shut down normally!
InnoDB: Starting crash recovery.
InnoDB: Reading tab1espace information from the .ibd files...
I nnoDB: Crash recovery may have fail ed for some . ibd files!
InnoDB: half-written data pages

GLOBAL
_pages_

_dblwr_pages_
Innodb_ buffer_p ool_pages_ _dblwr_p ages_

_dblwr_p ages_

_ innodb_

2.6.3

0(1) ,

Hash Index ,
..ttl.CA
56 ..'- -,.

o WHERE a=xxx and b=xxx

ENGINE INNODB
mysq1>5HOW ENGINE INNODB 5TATU5\G;
1. row
5tatus:

090922 11:52:51 INNODB MONITOR OUTPUT

Per second averages ca1cu1ated from the 1ast 15 seconds

IN5ERT BUFFER AND ADAPTIVE HA5H INDEX

----------------…------------------
Ibuf: size 2249 , free 1ist 1en 334 6 , seg size 5596 ,
374650 inserts , 51897 merged recs , 14300 merges
Hash tab1e size 4980499 , node heap has 1246 buffer(s)
1640.60 ha.h ..arch../. , 3709.46 non-ha.h ..arch../a

table
WHERE index
searches:non-hash
2.6

2.6 .4

(Asynchronous 10 ,

page_
(8 , 7) , (8 , 8)

space , page_ no

4.70 0.00 1.60 13.20 0.00 80.50

Device: rrqm/ s wrqm/s r/s w/s rMB/s wMB/s avgrq-sz avgqu-sz


await
sdc 3905.67 172.00 6910.33 466.67 168.81 18.15 5 1. 91 19.17
2.59 0.13 97.73

InnoDB

/usr/local/mysql/bin/mysqld: error while loading shared libraries: libaio. so.1:


cannot open shared object file: No such file or directory

Native
..ttl.CA
58

ffií Mac

mysql> SHOW VARIABLES LIKE ' i nnodb_use_ native_aio'\G;


1. row
Variable name: innodb use nat i ve aio
Value: ON
1 row in se t (0.00 sec)

read

2.6.5

Neighbor Page

_flush_

2.7
insert

merge insert
insert

_force_

_ force 1""'"

(SRV

_FORCE_NO
.{,I. CA
60
s
4(SRV_ FORCE_NO_IBUF_ MERGE):

Log) , InnoDB

_ force _

mysql>START TRANSACTION ;
Query OK , 0 rows affected (0.00 sec)

mysql>UPDATE Profile SET password= " ;


Que r y OK , 9587770 rows affected (7 min 55.73 sec)
Rows matched : 9999248 Changed: 9587770 Warnings: 0

START

[root@nineyouO-43 ps - ef I grep mysqld


root 28007 1 0 13:40 pts/1 00:00:00 Ibin/sh./bin/mysqld_safe --datadir =/
usr/1oca1/mysql/data --pid-file=/usr/loca1/mysql/data/nineyouO-43 . pid
mysql 28045 28007 42 13:40 pts/1 00:04:23 /usr/local/mysql/bin/mysql d -- basedir
=/usr/local/mysql --datadir=/usr/loca1/mysql/data --user=mysql --pid-f ile=/usr/
local/mysql/data/nineyouO-43.pid --skip-external-locking --port=3306 --socket=/tmp/
mysql.sock
root 28110 26963 0 13 : 50 pts/11 00:00:00 grep mysq1d
[root@nineyouO-43 - ] # k i ll -9 28007
[root@nineyouO-43 k il l -9 28045

090922 13 : 40 : 20 I nno DB : Started; log sequence number 6 2530474 6 15


InnoDB: Starting in background the ro11back of uncommitted transactio ns
090922 13 : 40:20 back trx with id 0 5281035 , 8867280 rOW8 to undo

InnoDB : Progress in percents: 1090922 13 :40 : 20


090922 13:40:20 [Note) for
Version: ' 5.0.45-109 ' socket : '/tmp/mysql. sock ' port : 3306 MySQL
Server (GPL)
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
59 60 61 62 63 64 65 66 67 68 - 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
87 88 89 90 91 92 93 94 95 96 97 98 99 100
1nnoDB: Rolling back o f trx id 0 5281035 completed
090922 13:49: 21 1nnoOB: Rol1back of non-prepared transactions completed

_ for ce_

force

090922 14:26:23 InnoDB: Started; log sequence number 7 2253251193


InnoDB: !!!innodb_ f o rce_recovery is set to 3 !!!
090922 14:26:23 [Note) /usr/local/mysql/bin /mysqld: ready for connections.
Version: ' 5 . 0.45-109 ' socket: ' /tmp/mysq l . sock ' port: 3306 MySQL Communi ty
Server (GPL)

_ force _

2.8
C...

3.1

I grep
090922 16:25:52 mysq1d started
090922 16:25:53 InnoDB: Started; 10g sequence number 8 2801063211
InnoDB: !!! innodb force recovery is set to 1 !!!
090922 1 6:25:53 [ERROR) Fata1 error: Can't open and 10ck privi1ege tab1es:
Tab1e 'mysq1.host' doesn't exist
090922 16:25 :53 mysq1d ended

MySQL

3.1.1

_ buffer_p ool_

_ buffer_p ool_ size= 1 _ buffer_pool_

information

mysq1> SELECT * FROM


-> GLOBAL VARIABLES
-> WHERE VARIABLE NAME LIKE ' innodb
*************************** 1. row
VARIABLE NAME: INNODB BUFFER POOL SIZE
VARIABLE VALUE: 1073741824
1 row in set (0.00 sec)

mysq1>SHOW VARIABLES LIKE 'innodb


1. row
Variab1e_name : innodb_buffer_poo1_size
Va1ue: 1073741824
1 row in set (0.00 sec)
rst-e%

VAR1 ABLES.

SQL

3.1.2

SET
I (global I session) system_var_name= expr
I (@@global. I @@session. I expr

mysql>SET read buffer size=52 4288 ;


Query OK , 0 rows affected (0 .00 sec)

mysql>SELECT @@session . read buffer size\G;


1. row * * * * * * * * * … * . . . * * * . . . * * * * *
@@session.read buffer size: 524288
1 row i n set (0.00 sec)

mysql>SELECT
1.
C...

@@global.read_ buffer_ size: 2093056


1 row in set (0.00 sec)

mysql>SET @@global.read_buffer_size=1048576;
Query OK , 0 rows affected (0.00 sec)

mysql>SELECT @@session.read_buffer_size\G;
1. ro w
@@session.read buffer size: 524288
1 row in set (0.00 sec)

mysql>SELECT @@global.read_buffer_ size\G;


*************************** 1. row
@@global.read_buffer_size: 1048576
1 row in set (0.00 sec)

System

mysql>SET GLOBAL
ERROR 1238 (HYOOO): Variable 'datadir' is a read only variable

3.2
log) ••

3.2.1

VARIABLES LIKE 'log_error'

mysql> S HOW VARIABLES LIKE 'log_error'\G ;


1. row
Variable_ name : log_error
Value: /mysql_data_ 2/stargaze r.log
1 row i n set (0.00 sec)

mysql> system hostname


stargazer

[ root@nineyouO -43 data l l tail -n 50 nineyouO-43. err


090924 11:31:18 mysqld started
090924 11:31:18 InnoDB: Started; log sequence number 8 28010 63331
09092411:31 :19 [ERROR l Fata1 error: Can't open and lock privilege tables:
Table 'mysql.host' doesn't exist
090924 11:31:19 mysqld ended
C...
3.2 67-'

log:

090924 11:39:44 InnoDB: ERROR: the age of the 1ast checkpoint is 9433712 ,
InnoDB: which exceeds the 10g group capacity 9433498.
InnoDB: If you are using big BLOB or TEXT rows , you must set the
InnoDB: combined size of 10g fi1es at 1east 10 times bigger than the
InnoDB: 1argest such row.
090924 11:40:00 InnoDB: ERROR: the age of the 1ast checkpoint is 9 433&23 ,
InnoDB: which exceeds the 10g group capacity 9433498.
InnoDB: I f you are using big BLOB or TEXT rows , you must set the
InnoDB: combined size of 10g fi1es at 1east 10 t imes bigger than the
InnoDB: 1argest such row.
090924 11:40:16 InnoDB: ERROR: the age of the 1ast checkpoint is 9433645 ,
InnoDB: which exceeds the 10g group capacity 9433498.
InnoDB: If you are using big BLOB or TEXT rows , you must set the
InnoDB: combined size of 10g fi1es at 1east 10 times bigger than the
InnoDB: 1argest such row.

3.2.2

(slow

query_

mysq1> SHOW VARIABLES LIKE '10ng_query_time '\G;


1. row
Variab1e_ name: 10ng_query_time
Va1ue: 10.000000
1 row in set (0.00 sec)

mysq1> SHOW VARIABLES LIKE


1. row
C...

v1 --o-
evn
b 'AL
aw
a rz aue qo
e·· --N·
met
nLS le
ss .. oo
qu e E .• e s
W}
a nu

( <U

query_
query_

long_query _

queries_ not_using_

queries_ not_using_indexes:
mysql> SHOW VARIABLES LIKE 'log_queries_ not_using_indexes'\G:
1. row
Variable_name: log_queri es_not_ using_indexes
Value: ON
1 row i n set (0.00 sec)

MySQL throttle_ queries_ not_

[root@nh122-190 data)f mysqldumpslow nh122-190-s1ow.log


Reading mysql slow query l og from nh122-190-s1ow.log
Count: 11 Time=10.00s (l10s) Lock=O.OOs (Os) Rows=O.O (0) , dbother(dbother)@
localhost
i nsert into test.DbStatus select now() , (N-com_ select)/(N-uptime) , (N -com_insert )!
(N-uptime) , (N-com_update) / (N-uptime) , (N-com_delete)! (N-uptime) , N- (N/N) , N- (N/N) , N.N/
N , N-N/(N*N) , GetCPULoadlnfo(N) from test.CheckDbStatus order by check_id desc limit N
9
3-
2-
-

R 9 0
Coun t : 653 Time;O.OOs (Os)
SC)@[192 . 168.43.7 )
se1ect custom_name_one from '10w_game_schema'. ' ro1e_detai1s' where r01e_id='S'
rse and summarize the MySQL slow query 10g. Options are

--verbose verbose
--debug debug
--he1p write this text to standard output

-v verbose
-d debug
- s ORDER what to sort by (a1 , at , ar , c , 1 , r , t) , ' at ' is defau1t
a1: average 10ck time
ar: a v erage rows sent
at: average que r y time
c: count
1 : 10ck time
r: rows sent
t: query time
-r reverse the sort order (largest 1ast instead of ßrst)
-t NUM just show the top n queries
-a don't abstract a11 numbers to N and strings to ' S '
-n NUM abstract numbers with at 1east n digits within name s
-g PATTERN grep : on1 y consider stmts that inc1ude this string
-h HOSTNAME hostname of db server for *-slow.1og fi1ename (can be wi1dcard) ,
defau1t is '*', i . e. match a11
-i NAME name of server instance (if using mysq1 . server startup script)
-1 don't subtract 10ck time from tota1 time

[root@nhl19-141 data)' mysq1dumps10w -s a1 -n 10 david.10g


Reading mysq1 slow query 10g from david.10g
Count : 5 Time=O.OOs (Os) Lock=0.20s (ls) Rows=4.4 (22) , Audition[Audition)@
[192.168.30.108)
SELECT OtherSN , State FROM wait friend info WHERE UserSN = N

Count: 1 Time=O.OOs (Os) Lock=O.OOs (Os) Rows;1 . 0 (1) , audition-kr[audition-


kr)@[192.168.30.105J
SELECT COUNT(N) FROM famverifycode WHERE Use rSN=N AND verifycode='S'

MySQL
70 ‘ v

mysq1> SHOW CREATE TABLE mysq1.s1ow_log\G;


1. row

Tab1e: slow_log
Create Tab1e: CREATE TABLE 'slow_log' (
'start_time' timestamp NOT NULL DEFAULT CURRENT_ TIMESTAMP ON UPDATE CURRENT
TIMESTAMP ,
'user_host' mediumtext NOT NULL ,
'query_time' time NOT NULL ,
' lock_time' time NOT NULL ,
'rows_sent ' int(ll) NOT NULL ,
'rows_examined' int(l l ) NOT NULL ,
'db' varchar(512) NOT NULL ,
' last_insert_id' int(ll ) NOT NULL ,
'insert_id' int(ll) NOT NULL ,
'server_id' int(ll) NOT NULL ,
'sq1 text' mediumtext NOT NULL
ENGINE=CSV DEFAULT CHARSET=utf8 COMMENT='Slow log'
1 row in set (0.00 sec)

mysq1>SHOW VARIABLES LIKE 'log_output'\G;

1+
--a-m-e-l+-------+
+--------…----+--
v-
a- a e-n-
-l-
E- hu-

,
e-
a-u-HV'

+
I 1og_output I FILE I

+------------……+- --------+
1 row in set (0.00 sec)

mysql>SET GLOBAL 1og_output='TABLE ';


Query OK , 0 rows affected (0.00 sec)

mysql>SHOW VARIABLES LIKE ' log_output'\G;


+---------------+-- -------+
I Variable name I Va1ue I
+---------------+-- -------+
I 1og_output I TABLE I
+---------------+-- -------+
1 row in set (0.00 sec)

mysq1> se1ect sleep(lO)\G;


+-----------+
I sleep(lO) I
g-
3.2

+----------- +
o I
+------ -----+
1 row in set ( 1 0.01 sec)

mysq1> SELECT * FROM mysq1.s1ow_ 1og\G;


1. row
start time: 2009-09-25 13:44:29
user host: david (david] @ l oca1host ()
q uery_time: 00:00:09
loc k time: 00:00:00
rows sent: 1
rows e xamined: 0
db: mysq1
last insert id: 0
zetrte
srqn
evl
nes-- -- ooso
ddt{
-1·lx
-
····nu
-et
-
eo ce
estc CU e e p ( nu}

z ow s }

mysql>ALTER TABLE mysql.s1ow_1og ENGINE: MyI SM;


ERROR 1580 (HYOOO): You cannot 'ALTER' a 10g tab1e if 10gging is enabled

mysq1>SET GLOBAL s1ow_query_ 1og:0ff;


Quer y OK , 0 rows affected (0.00 sec)

mysq1>ALTER TABLE mysq1.s1ow_ 1og ENGINE=MyISAM;


Query OK , 1 row affected (0.00 sec)
Records: 1 Dup1icates: 0 Warnings: 0
* Time: 111227 23:49:16
* User@Host: root[rootl @ localhos t [127 .0.0.1)
* Query_ time: 6.081214 Lock_time: 0.046800 Rows_sent: 42 Rows_ examined: 727558
Logical-zeada:91584Physical-z-ad-:19
use tpcc;
SET timestamp=1325000956;
SELECT orderid.customerid.employeeid.orderdate
FROM orders
WHERE orderdate IN
( SELECT MAX(orderdate)
FROM orders
GROUP BY (DATE ))

query_
slow
slow

log
slow log
slow log
log

3.2.3
.{) I. CA
3.2 73- .

[root@nineyouO-43 dataJI tail nineyouO-43.1og


090925 11:00:24 44 Connect zlm@192.168.0.100 on
44 Query SET AUTOCOMMIT =O
44 Query set autocommit=O
44 Quit
090925 11:02:37 45 Connect Access denied for user 'root'@ 'localhost' (using
password: NO)
090925 11:03:51 46 Connect Access denied for user 'root' @'localhost' (using
password: NO)
090925 11:04:38 23 Query rollback

3.2 .4

mysql> UPDATE t SET a = 1 WHERE a = 2;


Query OK , 0 rows affected (0.00 sec)
RowS matched: 0 Changed: 0 Warnings: 0

mysql> SHOW MASTER STATUS\G;


1. row
..........
REtoe
enBBt
l----Sm3 q d
V48 nununununu

iDEd
poqdG
oqdnt
s-0·l
-lq
Bot
n
--e
BXncde
14u
-
pu d

1 row in set (0.00 sec)

mysql> SHOW BINLOG EVENTS IN 'mysqld.000008'\G;


1. row
Lee
onz
qdtvapy
mop
ntEesed
m4Flq.m
·-······
SE ld
a oe
- t
os
oc
nu
hUAU oo
--e

Evnb

-l
C...

End_log_poS: 120
Info: Server ver: 5.6.6-m9-1og , Binlog ver: 4
2. row
Lee-
qdtvoeseds
apy-p
onz1&
ntr- qr
vdsoe9
mlQIl d
ly
--········
mop--10 AU
<nuhu oo
hu
--e -U
4UQJ
<

vsd
En

q
Info: BEGIN
3. row ***************************
Lee
apy
onz
ntE
qdtv m1Qld nununununu
VJs9e
esed qar
F
YU
<

D
UM VS
-e

End_log_p os: 303


Info: use 'test'; UPDATE t SET a = 1 WHERE a = 2
4. row * **************************
Lee-
qdtvoyou8
ntr- d nununununuoo
ese.ds
apy-p
S3e3
m3Q13
q.E
-

pun
vnbAu
-eq

PUM

Info: COMMIT
4 rows in set (0.00 sec)

BINLOG
.{)I.C...

(datadir ),
mysq1> show variab1es 1 i ke 'datadir';

+---------------+----------------------…-…--+
I Variab1e name I Value
+------------ ---+------------ ----------------+
I datadir I /usr/1oca1/mysq1/data/ I

+…--------------+----------------------------+
1 row in set (0.00 sec)

mysql> system 1s -1h /usr/loca1/mysq1/data/;


tota l 2 . 1G
- rw - rw---- 1 mysq1 mysq1 6.5M Sep 25 15:13 bin_1og .000001
-rw-rw---- 1 mysql mysql 17 Sep 25 00:32 bin_log.index
-rw-rw- - - - 1 mysql mysql 300M Sep 25 15: 13 ibdata1
-rw-rw---- 1 mysql mysql 256M Sep 25 15:13 ib_logfi1eO
-rw-rw---- 1 mysql mysql 256M Sep 25 15:13 i b_logfi1e1
drwxr-xr- x 2 mysq1 mysql 4.0K May 7 10 : 08 mysql
drwx------ 2 mysql mysq1 4.0K May 7 10 : 0 9 test

replication

o max_ binlog_ size


o binlog_cache_size
o sync_binlog
o binlog-do-db
o binlog-ignore-db
C...

o binlog_format
_ binlog_

073741 G _ binlog_
uncommitted)

cache_

cache_
cache_ size

SHOW GLOBAL cache_ cache_disk_


cache_ cache_
binlog_cache_ disk_

mysql> show variables li ke 'binlog_cache_size';

I Variable name I Value I


+-------------------+- ------+
I binlog cache size I 32768 I

+-------------------+---…-+
1 row in set (0.00 sec)

mysql > show global status like 'binlog_cache%';


+-----------------------+--------------+
I Variable name I Value I
+-----------------------+---------------+
I binlog_cache_disk_use I 0 I
I binlog_cache_use I 33553 I
+-----------------------+---------------+
2 rows i n set (0.00 sec)

cache_
3 .2

_ binlog=
_ binlog=

_ support_ _support_

binlog_

Microsoft SQL

MySQL
C...
‘'
( 1)

temporary

InnoDB Yes Yes


MylSAM Yes Yes
HEAP Yes Yes
MERGE Yes Yes
NDB Ycs No
Archive Yes Yes
CSV Yes Yes
Fcderate Yes Yes
Blockhole No Yes

binlog_

mysql >SET @@session.binlog_format='ROW';


e
3.2
m--
Query OK , 0 rows affected (0 .00 sec)
dv
mysql>SELECT@@ses sion.bi nlog_format;

+-----………-------------------+
I @@session.binlog_ format I

+-------------…---…----------+
I ROW
+------------------ - ------------ +
1 row in set (0.00 sec)

mysql>SET GLOBAL binlog_ format= ' ROW ' ;


Query OK , 0 rows affected (0.00 sec)

mysql>SELECT @@globa l .binlog_format;


+----------------- -------------+
I @@global.binlog_for mat I

+-----…---------------…-----+
I ROW
+------------------ ------------+
1 row in set (0.00 sec)

mysql>SELECT @@session.binlog_fo rmat\G;


1. row
@@session.binlog_fo rmat: STATEMENT
1 row in set (0.00 sec)

mysql>SHOW MASTER STATUS\G;


1. row
i loDDo
enBB- s6e
eostAUc
········nu nunu'J
< nu
U
- }
poqiD--
s-oe
oqns
iq
n--l
BOW
lIn
--{
·-nr Et
<
U

mysql>UPDATE t l SET usernamemUPPER(use rname);


80 ‘tv
Query OK , 89279 rows affected ( 1.83 sec)
Rows matched: 100000 Changed: 89279 Warn ings: 0

mysq1>SHOW MASTER STATUS\G;


1.
loDDo
Ftoe
i---
enBB- eost c
nuU
········AU
-
nuU AU
}
< <

iDEt
poq
s-oe
n--
lIn
iq
BOW

B1
-ln --o -{
E <
U

mysq1>SET SESSION bin1og_fo rmat='ROW ';


Query OK , 0 rows affected (0 .00 sec)

mysq1>SHOW MASTER STATUS\G;


1. row
10DDO
Ftoe
enBB-
tqd t
s6e
eos cnununu
nunu
·······nu
.)
poqiDzt
s-oe
oqns
sow
19lIn
n--1
lo
·-Anr
RM'

<
A U

mysq1>UPDATE t2 SET username=UPPER(use rname);


Query OK , 89279 rows affected (2.42 sec)
Rows matched: 100000 Changed: 89279 Wa rnings: 0

mysq1>SHOW MASTER STATUS\G;


1. row
loDD
Ftoe
LienBB-e3st8
--······
S7 C
AUnu
Unu
UU < < <

poqiDrt
s-oe
oqns ,
Bow
iqlIn
n--l
Bllo
nr ·- --{

O <
u AV e

_ binlog= 1
3 .2

[root@nineyouO -43 datal# mysq1bin1og --start-position;203 test .000004


/*!40019 SET @@session.max_insert_de1ayed_threads;0*/;

'090 927 15:43:11 server id 1 end_ log_pos 376 Query thread_id; 188 exec
time;1 error code=O
SET TIMESTAMP=1254037391/*!*/;
update t2 set username;upper(username) where id=1

, at 376
'090927 15:43:11 server id 1 end_ 10g_p os 403 Xid; 1009
COMMIT/*!*/;
DE LIMITER ;
, End of 10g fi1e
ROLLBACK /* added by mysqlbin10g */;
/*!50003 SET COMPLETION TYPE=@OLD COMPLETION TYPE*/;

t2 SET usemame=UPPER (usemame) WHERE id=

[root@nineyouO-43 datal' mysqlbin10g --sta rt -position=1065 test.000004


/*!40019 SET @@session.max_insert_de1ayed_ threads=0*/;

11 at 1135
, at 1198
11090927 15:53:52 server i d 1 end_ 1og_p os 1198 Tab1e_map: 'member'.'t2' mapped
to number 58
'090927 15:53:52 server id 1 end_ 1og_pos 1378 update_ rows: table id 58 flags:
STMT END F

BINLOG '
tt l. e
82

••
/gJAAAAA
EBq/ShgBAAAAtAAAAGIFAAAQADoAAAAAAAEACv////8A/AEAAAALYWx1eDk50Dh5b3UEOX1vdSA3
Y2JiMzI1 Mm JhNmI3ZT1jNDlyZmFjNTMzNGQyMjAlNAFNLacPAAAAAABjEnpxPBlAAAD8AQAAAAtB

AGMSenE8EgAA
'/*!*/;
1 at 1378
1090927 15:53:52 server id 1 end_10g_p os 1405 Xid = 1110
COMMIT/*!*/;
DELIMITER ;
jj End of 10g lile
ROLLBACK /* added by mysq1bin10g */;
/*!50003 SET COMPLETION TYPE=@OLD COMPLETION TYPE*/;

( root@nineyouO-43 datal' mysq1bin10g -vv --start-position=1065 test.000004

BINLOG '
EBq/ShMBAAAAPwAAAK4EAAAAADoAAAAAAAAABm11bWJ1cgACdDIACgMPDw/+CgsPAQwKJAAoAEAA
/gJAAAAA
EBq/ ShgBAAAAtAAAAG1 FAAAQADoAAAAAAAEACv / / / / 8A/ AEA.A AALYWx1eDk50Dh5b3UEOX1 vdSA3
Y2JiMzI1 MmJhNmI3ZT1jNDlyZmFjNTMzNGQyMjAlNAFNLac PAAAAAABjEnpxPBlAAAD8AQAAAAtB
TEVYOTk40F1PVQQ5eW91IDdjYmlzMjUyYmE2Yjd10WMOMjJmYWM1MzMOZDlyMDUOAUOtpw8AAAAA
AGMSenE8EgAA
'/*!*/;
11111 UPDATE member.t2
#11 11 WHERE
1#' @1=1 /* INT meta=O nu11ab1e=0 is nu11=0 */
111. @2 = 'david' / * VARSTRING(36) meta= 36 nu11ab1e=0 is nu11=0 */
1## @3='fami1y' /* VARSTRING(40) meta=40 nullable=O is_nu11=0 */
11 l1li @4='7cbb3252ba6b7e9c422fac5334d22054' /* VARSTRING(64) meta=64 nu11ab1e=0
is nu11=0 */
HII @5= ' M' /* STRING(2) meta=65026 nu11able= 0 is nu11=O */
#11 11 @6='2009:09:13' /* DATE meta= O nu11ab1e=O is nu11=0 */
IIU @7 = 'OO:00:00' /* TIME meta=O nu11ab1e=0 is nu11=O */
11## @8= " /* VARSTRING(64) meta=64 nu11ab1e=O i s nu11= 0 */
HII @9=O /* TINYINT meta=O nu11ab1e=0 is nu11=0 */
.u @10=2009-08-11 16:32:35 /* DATETlME meta=O nu11able=O is nu11=0 */
'U SET
#., @1=1 /* INT meta=O nu11able=O is null=O */
{,I."

'f'.
'" @2= 'OAVIO' /* VARSTRING(36) meta=36 nu11ab1e=0 is nu11=0
-
@3=fami1y /* VARSTRING(40) meta= 40 nu1 1ab1e=0 is_nu11=0 */
..

'"
is nu1 1=0 ‘
@4='7cbb3252ba6b7e9c422fac5334d22054'
i
/* VARSTRING(64) meta=64 nu11ab1e=0

@5= ' M ' /* STRING(2) meta=65026 nu11ab1e=0 is nu11=0 */


@6= ' 2009:09:13' meta=O nu11ab1e=0 is nu1 1 =0 */
,., @7='OO:00:00 ' /* TIME meta=O nu11ab1e= 0 is nu11=0 */
@8= " /* VARSTRING(64) meta=64 nu11ab1e=0 is nu11=0 */
.11 @9=0 /* TINYINT meta=O nu11ab1e=0 is nu11=0 */

'"
,
@10=2009-08-11 16:32:35 /* OATETIME meta= O nu11ab1e=0 is nu11 a O */
at 1378
'090927 15:53:52 server id 1 end_10g_pos 1405 Xid = 1110
COMMIT/*!*/;
OELIMI TER ;
of 10g file
ROLLBACK /* added by mysq1bin1og */;
/*!50003 SET COMPLETION TYPE=@OLD COMPLETION TYPE* /;

t2
set usemame=upper( usemame )where id=

3.3

mysq1>SHOW VARIABLES LIKE 'socket ' \G;


1. row
Variab1e name: socket
Va1ue: /tmp/mysq1.sock
1 row in set (0.00 sec)

3 .4
mysql> show variables li ke ' pid_ lile'\G;
1. row
variable_name : pid_file
Value : lusr/local/my sql/data/xen- server.pid
1 row in set (0 .00 sec)

3.5

[root@xen- server test)# cat va.frm


TYPE=VIEW
query=select 'test'. 'a'. ' b' AS 'b ' from ' test '. 'a'
md5=4eda7038 7716a4d6c96f3 042dd68b7 42
updatable-l
algori thm- O
deliner user= root
deliner host- localhost
suid=2
with_check_ option=O
timestamp- 2010-08-04 07:23 : 36
create- vers i on=l
source=select * from a
client cs name=utf8
connection_c l_name=utf8 _ general_c i
view_body_ ut f8=select 'tes t'. ' a'. 'b ' AS 'b' from 'test'. ' a'

3.6
3.6
., I. CA

3.6.1

( tablespace _ data_file
i nnodb_ data_ fi 1e _p at h= daeafi1 e_specl [ ; daeafile_spec2 ] . • .

[mysq1d ]
i nnodb_data_fi1e_pat h = /db/ i bdata1:2000M; /dr2/db/ i bda t a2:2000M:autoextend

_ data_fìle
_file_p er_

_ file
mysq1 >SHOW VARI ABLES LIKE • i nnOdb_fi1e _ pe r_tab1 e' \G;
**** *** ******************** 1 . row
Variab1e_ name: innodb_fi1e _per_ tab1e
Va 1ue: ON
1 r ow i n s e t (0. 00 sec)

mys q 1 > system ls -lh /us r /1oca1/mysq1/data/membe r /*


-rw-r- - --- l mysq1 mysq1 8.7K 2009-02 - 24 /usr/ 1 oca1/mysq1/data/member/
Profi1e.frm
-rw- r ----- 1 mysq1 mysq1 1. 7G 25 11 : 1 3 /usr/1oca1 / mysq1/data/memberl
Profi 1 e. i bd
-rw- rw-- -- 1 mysq1 mysq1 8.7K 27 13:38 /usr/ 1 oca1/mysq1/data/member/
t1. fr m
-rw-rw - --- 1 mysq1 mysq1 17M 27 13:40 / usr/ 1 oca1 /mysq1 /da t a/memberl
t 1. i bd
- rw-rw---- l mysq1 mysq1 8.7K 27 1 5:42 lusr/1oca 1 /mysq1 /data/memberl
t 2.frm
86

-rw-rw ---- 1 mysql mysql 17M 4

_ file _p er_

, • ‘ ‘ •
.. ..._'

.'…

1\ InnoDB Tablcs ,,,//·

:
i
:3
;;
InnoDB
share
tablcspace
-ullz ;
i l..·.__
\ .ibd files

3.6.2

log

log
.{) I. c...
3.6 87 --'

ib_logfil eO

ib_logfile2 ib_Iogfile I
(J innodb_ log_fìles_in_group
(J innodb_ mirrored_log_groups
(J innodb_log_group_home_dir

_ log_ files_ in
_ mirrored

_log_group_ home_

mysq1>SHQW VARIABLES LIKE

4. row
Variab1e_ name: innodb_10g_file_si ze
Va1ue: 5242880
5. row ..

Variab1e_name: innodb_109_files_in_group
Va1ue: 2
.
******.******************** 6. row
Variab1e_ name: innodb_10g_group_home_dir
Va1ue: ./
7. row
Variab1e_ name: innodb_ mirrored_10g_9 rou Ps
Va1ue: 1
7 rows i n set (0.00 sec)
090924 11:39:44 1nnoOB: ERROR : the age of the last checkpoint is 9433712 ,
lnnoOB: which exceeds the 10g group capacity 9433498.
1nnoOB: 1f you are using big BLOB or TEXT rows , you must set the
1nnoOB: combined size of 10g tiles at 1 east 1 0 t i mes b i gger than the
1nnoOB: 1argest such row.
090924 11:40:00 1nnoOB: ERROR: the age of the 1as t chec kpo int is 9433823 ,
1nnoDB : which exceeds the 10g group capacity 9433498 .
lnnoDB: If you are using big BLOB or TEXT rows , you must set the
ln noDB: combined size of log files at l east 10 times bigger than the
1nnoDB : 1argest such row.
090924 11:40:16 1nnoOB: ERROR: the age of the 1 as t checkpoint i5 9433645 ,
1 nnoDB: which exceeds the log group capacity 9433498.
lnnoDB : 1f you are u5ing big BLOB or TEXT rows , you must set the
lnnoOB : combined size of 10g fi1es at 1east 10 times bigger than the
1nnoOB: 1argest such row.

age ofthe last checkpoint is 9433645 , InnoDB:which


exceeds the log group capacity
buffer pool )

entry
.{) I. CA
36

redo_log_type space page_no redo_log_body

log

'a d o OohM zu mw

N i b_l ogfilel H ib_l ogfile2 h

_flush_log_ at_ trx_


‘()I. c..
--.
20

_ftush _log_at_trx._

3.7

in

MySQL
C...

4.1

(Primary

NOT

mysql> CREATE TABLE z (


-> a INT NOT NULL ,
-> b INT NULL ,
-> c INT NOT NULL ,
-> d INT NOT NULL ,
-> UNIQUE KEY (b) ,
-> UNIQUE KEY (d) , UNIQUE KEY (c));
Query OK , 0 rows affected (0.02 sec)

mysql> INSERT INTO z SELECT 1 , 2 , 3 , 4;


Query OK , 1 row affected (0.00 sec)
Records: 1 Duplicates: 0 Warnings: 0
•••
mysql> INSERT INTO z SELECT 5 , 6 , 7 , 8;
Query OK , 1 row affected (0.00 sec)
Records: 1 Duplicates : 0 Warnings: 0

mysql> INSERT INTO z SELECT 9 , 10 , 11 , 12;


Query OK , 1 row affected (0.00 sec)
Records: 1 Dup1icates : 0 Warnings: 0

mysq1> SELECT a , b , c , d , rowid FROM z;


+---+-- ----+…- -+----+--- -- ---+
I a I b I c I d rowid I
+---+----- - +----+--…-+-------- +
1 2 3 4 4
5 6 7 8 8
9 10 11 12 12
+---+---- --+----+- …+------ --+
3 rows in se t (0.00 sec)

mysql> CREATE TABLE a


- > a I NT ,
-> b INT ,
- > PRlMARY KEY(a , b )
-> )ENGINE= InnoDB;
Query OK , 0 rows affected (0.0 3 sec)

mysql> INSERT INTO a SELECT 1 , 1;


Query OK , 1 row affected (0.01 sec)
Records: 1 Dupl icates : 0 Warnings: 0

mysql> SELECT a , rowi d FROM a;


ERROR 1054 (42S22): Unknown column ' rowid ' in 'field 1ist '
.ft l. e ....
4.2

4.2

'
, , , '

Leaf node segment


Extent

, ,,
,, R-V t '"nt
,, - A

Page
Row "

4.2.1

_file _per_

Double write
_ fil e_p er_
file
C...

mysq1> SHOW VARIABLES LIKE • innodb_ file _ per _ tab1e' \G;


1 . row
Variab1e_name: i nnodb_fi1e_ per_tab1e
Va1ue: ON
1 row in set (0.00 sec)

mysq1> system 1s -1h


-rw-rw---- 1 mysq1 mysq1 58M Mar 11 13:58 /usr/1oca1/mysq1/data/ibdata1

mysq1> SET autocommit=O;


Query OK , 0 rows affected (0.00 sec)

mysql> UPDATE mytest SET sa1ary=0;


Query OK , 2844047 rows affected (19.47 sec)
Rows ma t ched: 2844047 Changed: 2844047 Warnings: 0

mysql> system 1s -1h /usr/1oca1/mysql/data/ibdata*


-rw-rw---- 1 mysq1 mysq1 114M Mar 11 14:00 /usr/1oca1/mysq1/data/ibdata1

mytest set

mysq1> ROLLBACK;
Query OK , 0 rows affected (0.00 sec)

mysq1> system 1s -1h /usr/1oca1/mysq1/data/ibdata*


-rw-rw---- 1 mysq1 mysq1 114M Mar 11 14:00 /usr/ 1oca1/mysq1/data/ibdata1
4.2 95'9

r
_ innodb _page _

[root@nineyouO-43 pyl# python py_ innodb_page_ infO.py /usr/local/mysql/data/


ibdatal
Total number of page: 83584:
Insert Buffer Free List : 204
Freshly Allocated page: 5467
Undo Log page: 38675
File Segment inode: 4
B-tree Node: 39233
File Space Header: 1

node segment) ,
node
lnnoDB

lnnoDB

_file_per_

mysq1> CREATE TABLE t 1 (


-> co11 INT NOT NULL AUTO I NCREMENT ,
-> co12 VARCHAR(7000) ,
-> PRI MARY KEY (co11)) ENGINE= InnoDB;

mysq1> system 1s -1h lusr/1oca1/m ysq1/data/tes t/t1 . ibd;


- rw - r w- --- 1 mysq1 mysq1 96K 1 4: 59 lusr/1oca1/m ysq1/data/tes t /t 1. i bd

mysq1> INSERT t 1 SELECT NULL , REPEAT ( ' a' , 7000 ) ;


Query OK , 1 row affected (0.04 s ec)
Records: 1 Dup1ica tes: 0 Warnings: 0

mysq1> INSERT in t o t1 SELECT NULL , REPEAT(' a ' , 7000);


Query OK , 1 row affected (0.01 sec )
Records: 1 Dup1icate s : 0 Wa rning s: 0

mysq1> system 1s -1h lusr/1oca1/my sq1/da ta/test/tl . i bd;


-r w-rw---- 1 mysq1 mysq1 96K 1 6: 2 4 lusr /1oca1/my sq1 /data/test/t1 . ibd

_innodb_page_
[ root@nin eyouO - 43 py)l ./py_i n nodb_pag e_in fo.py -v l usr /1oca1/my sq1 /da t a/ t es t l
t l . i bd
.{)I.CA
4.2 97.

page offset 00000000 , page type <File Space Header>


page offset 00000001 , page type <Insert Buffer Bitmap>
page offset 00000002 , page type <File Segment inode>
page offset 00000003 , page type <B-tree Node> , page level <0000>
page offset 00000000 , page type <Freshly Allocated Page>
page offset 00000000 , page type <Freshly Allocated Page>
Total number of page : 6:
Freshly Allocated page: 2
Insert Buffer Bitmap: 1
File Space Header: 1
B-tree Node: 1
File Segment inode: 1

mysql> INSERT into t1 SELECT NUL L, REPEAT('a' , 7000);


Quer y OK , 1 row affected (0 . 01 sec)
Records: 1 Duplicates : 0 Warnings: 0

[root@nineyouO-43 pyl# ./py_innodb_page_info.py -v /usr/local/mysql/data/test/


tl . ibd
page offset 00000000 , page type <File Space Header>
page offset 00000001 , page type <Insert Buffer Bitmap>
page offset 00000002 , page type <File Segment inode>
page offset 00000003 , page type <B-tree Node> , page level <0001>
page offset 00000004 , page type <B- tree Node> , page leve1 <0000>
page offset 00000005 , page type <B-tree Node> , page level <0000>
Total number of page: 6 :
Insert Buffer Bitmap: l
File Space Header: l
B- tree Node: 3
File Segment inode : 1

mysql> DELIMITER/I

mysq l > CREATE PROCEDURE load tl(count INT UNSIGNED)


C...
--.
-> BEGIN
-> OECLARE s INT UNSIGNEO OEFAULT 1;
-> OECLARE c VARCHAR(7000) OEFAULT REPEAT('a' , 7000);
- > WHILE s <= count 00
-> INSERT INTO t1 SELECT NULL , C;
-> SET s = s+l;
-> ENO WHILE;
-> ENO;
-> //
Query OK , 0 rows affected (0.04 sec)

mysq1> OELIMITER ;

mysq1> CALL 1oad_tl(60);


Query OK , 1 row affected (1.59 sec)

mysq1> SELECT COUNT(*) FROM t1\G;


*************************** 1. row
count (*): 63
1 row in set (0.00 sec)l row in set (0.00 sec)

mysq1> system ls -lh /usr/1oca1/mysq1/data/test/t1.ibd;


-rw-rw---- 1 mysq1 mysq1 576K 16:56 /usr/1oca1/mysq1/data/test/t1.ibd

_ innodb_page_

[root@nineyouO-43 pyl# ./py_innodb_page_info.py -v /usr/1oca1/mysq1/data/test/


tl.ibd
page offset 00000000 , page type <Fi1e Space Header>
page offset 00000001 , page type <Insert Buffer Bitmap>
page offset 00000002 , page type <File Segment inode>
page offset 00000003 , page type <B-tree Node> , page 1eve1 <0001>
page offset 00000004 , page type <B-tree Node> , page level <0000>
page offset 00000005 , page t ype <B-tree Node> , page 1eve1 <0000>
page offset 00000006 , page type <B-tree Node> , page 1eve1 <0000>
page offset 00000007 , page type <B-tree Node> , page 1eve1 <0000>
page offset 00000008 , page type <B-tree Node> , page 1eve1 <0000>
page offset 00000009 , page type <B-tree Node> , page l eve1 <0000>
page offset OOOOOOOa , page type <B-tree Node> , page 1eve1 <0000>
page offset OOOOOOOb , page type <B-tree Node> , page 1eve1 <0000>
page offset OOOOOOOc , page type <B-tree Node> , page 1eve1 <0000>
page offset OOOOOOOd , page type <B-tree Node> , page 1eve1 <0000>
4.2

page offset OOOOOOOe , page type <B- tree Node> , page 1eve1 <0000>
page offset OOOOOOOf , page type <B- tree Node> , page 1eve1 <0000>
page offset 00000010 , page type <B-tree Node> , page 1eve1 <0000>
page o f fset 00000011 , page type <B- tree Node> , page 1eve1 <0000>
page offset 00000012 , page type <B-tree Node> , page 1evel <0000>
page offset 00000013 , page type <B-tree Node> , page leve1 <0000>
page offset 00000014 , page type <B-tree Node> , page 1evel <0000>
page offset 00000015 , page type <B-tree Node> , page level <0000>
page offset 00000016 , page type <B-tree Node> , page 1evel <0000>
page offset 00000017 , page type <B-tree Node> , page leve1 <0000>
page offset 00000018 , page type <B-tree Node> , page 1eve1 <0000>
page offset 00000019 , page type <B-tree Node> , page 1eve1 <0000>
page offset 0000001a , page type <B-tree Node> , page 1evel <0000>
page offset 0000001b , page type <B-tree Node> , page level <0000>
page offset 0000001c , page type <B-t ree Node> , page level <0000>
page offset 0000001d , page type <B-tree Node> , page level <0000>
page offset 0000001e , page type <B-tree Node> , page leve1 <0000>
page offset 0000001f , page type <B-tree Node> , page leve1 <0000>
page offset 00000020 , page type <B-tree Node> , page leve1 <0000>
page offset 00000021 , page type <B-tree Node> , page level <0000>
page o ffse t 00000022 , page type <B-tree Node> , page level <0000>
page offset 00000023 , page type <B-tree Node> , page level <0000>
Total number of page: 36:
Insert Buffer Bitmap: 1
File Space Header: 1
B-tree Node: 33
File Segment inode: 1

mysql> CALL load t1(1 ) ;


Query OK , 1 row affected (0 .10 sec)

mysql> system 1s -1h lusr/1ocal/mysq1/data/test/t1.ibd;


-rw-rw---- 1 mysql mysq1 2.0M 17 : 02 /usr/local/mysql/data/test/t1. ibd

_ innodb_page_

Fres hly A llocated

(root@nineyouO-43 test21f -/py/py_innodb_page_ info.py t1.ibd - v


C...

page offset 000 00000 , page type <File Space Header>


p age offset 00000001 , page type <Insert Buffer Bitmap>
page offset 00000002 , page t ype <File Segment inode>
page offset 00000003 , page type <B-tree Node> , page level <0001>
page offset 00000004 , page type <B-tree Node> , page level <0000>
page offset 00000005 , page type <B-tree Node> , page level <0000>
page of f set 00000006 , page type <B-tree Node> , page level <0000>
page offset 00000007 , page type <B-tree Node> , page level <0000>
page of f set 00000008 , page type <B- tree Node> , page level <0000>
page offset 00000009 , page type <B-tree Node> , page level <0000>
page offset OOOOOOOa , page type <B-tree Node> , page le vel <0000>
page offset OOOOOOOb , page type <B-tree Node> , page level <0000>
page offset OOOOOOOc , page type <B-tree Node> , page level <0000>
page offset OOOOOOOd , page type <B-tree Node> , page level <0000>
page offset OOOOOOOe , page type <B-cree Node> , page level <0000>
page offset OOOOOOOf , page type <B-tree Node> , page level <0000>
page offset 00000010 , page type <B-tree Node> , page level <0000>
page offset 00000011 , page type <B-tree Node> , page level <0000>
page offset 00000012 , page type <B-tree Node> , page leve1 <0000>
page offset 00000013 , page type <B-tree Node> , page level <0000>
page offset 00000014 , page type <B-tree Node> , page level <0000>
page offset 00000015 , page type <B-tree Node> , page leve1 <0000>
page offset 00000016 , page type <B-tree Node> , page leve1 <0000>
page offset 00000017 , page type <B-tree Node> , page level <0000>
page offset 00000018 , page type <B-tree Node> , page 1evel <0000>
page offset 00000019 , page type <B-tree Node> , page 1eve1 <0000>
page offset 0000001a , page type <B-tree Node> , page level <0000>
page offset 0000001b , page type <B-tree Node> , page 1evel <0000>
page offset 0000001c , page type <B-tree Node> , page 1eve1 <0000>
page offset 000000 1 d , page type <B-tree Node> , page level <0000>
page offset 0000001e , page type <B-tree Node> , page level <0000>
page offset 0000001f , page type <B-tree Node> , page level <0000>
page of f set 00000020 , page t ype <B-tree Node> , page l evel <0000>
page offset 00000021 , page type <B-tree Node> , page 1evel <0000>
page offset 00000022 , page type <B-tree Node> , page level <0000>
page offset 00000023 , page type <B-tree Node> , page leve1 <0000>
page offset 00000000 , page type <Freshly Al located Page>

page offset 00000000 , page type <Freshly Allocated Page>


page offset 00000000 , page type <Freshly Allocated Page>
page offset 00000000 , page type <Freshly Allocated Page>
Total numbe r of page: 128:
Freshly Al located Page : 91
I nsert Buffer Bitmap: 1
42

File Space Header: 1


B-tree Node: 34
File Segment inode : 1

InnoDB _p age_
_p age_

Node )
Log Page)
Page)
system Page )
Buffer Bitmap )
Insert Buffer Free List)
BLOB Page)
BLOB page)

MySQL
Big
/02

4.3 ••

SQL

RECORD (NEW STYLE)


RECORD ( OLD
TABLE STATUS LIKE 'table

mysql> SHOW TABLE STATUS l i ke


1. row ..

Name: mytest
Engine: InnoDB
Version: 10
Row_forma t : Compact
Rows: 6
Avg_ row_length : 2730
Da ta_length: 16384
Max_da ta_ length: 0
Index_ length: 0
Data free: 0
Auto increment: NULL
Create time: 2009-03-17 13:33 : 50
Update_tim e: NULL
Chec k time: NULL
Collation: latinl swedish ci
Checksum: NULL
Create_op tions:
Comment:
********** ********** ******* 2 . row
Nq een
a--l
mno tn
mwvA SDRUS
nE
Ee
v s -A

Row f OR-
zomat RMH d t
-w
sqq
wnn
·-tt
ot ee ‘ n
AV g ra
-D -
a - -n 6
-
..{t I. CA

Max dnte
-zad -
axee ....nu
'L hh
nn

- .,.
Data free: 0
<
U

Auto increment: NULL


Create time: 2009-03 - 17 13:57:23
Update_time: NULL
Check time: NULL
Co11ation: latin1 swedish ci
Checksum: NULL
Create_options:
Comment:
2 rows in set (0.00 sec)

_innodb_p age_

4.3.1

J. *. ...... .. ..

header)
-.
.ttl , CA
._'-

dclctcd_f1 ag
min_rcc_ f1ag
n owned 4
hcap_no 13
001
record_ typc 3
Suprcmum.
next rccord 16
Total 40

mysql> CREATE TABLE mytest


-> t l VARCHAR(lO) ,
-> t2 VARCHAR(lO) ,
-> t3 CHAR (l O) ,
-> t4 VARCHAR(lO)
->) ENGINE=INNODB CHARSET=LAT I Nl ROW FORMAT=COMPACT;
Que ry OK , 0 rows affected (0.00 sec)

mysql> INSERT INTO mytest


-> VALUES ('a' , 'bb' , ' bb ' , 'ccc');
Query OK , 1 row affected (0.0 1 sec)

mysql> INSERT INTO mytest


-> VALUES (' d' , ' ee ' , 'ee' , ' f f f ' ) ;
Query OK , 1 ro w affected (0.00 sec)

mysql> INSERT INTO mytest


-> VALUES ( ' d ' , NULL , NULL , ' f f f ' );
Query OK , 1 row affected (0.00 sec )

mysql> SELECT * FROM mytest\G;


1. row
4.3 1054b

t1: a
t2: bb
t3: bb
t4: ccc
2. row
t1: d
t2: ee
t3 : ee
t4: fff
3. row
t1: d
t2: NULL
t3: NULL
t4: fff
3 rows ìn set (0.00 sec)

mytest.ibd >

0000c070 73 75 70 72 65 6d 75 6d 03 02 01 00 00 00 10 00 Jsupremum.. .... .. J


0000c080 2c 00 00 00 2b 68 00 00 00 00 00 06 05 80 00 00 J , . . . +h .. ........J
0000c090 00 32 01 10 61 62 62 62 62 20 20 20 20 20 20 20 J .2. . abbbb
OOOOcOaO 20 63 63 63 03 02 01 00 00 00 18 00 2b 00 00 00 J ccc........ + . . . J
OOOOcObO 2b 68 01 00 00 00 00 06 06 80 00 00 00 32 01 10 J+h .. . .... ... . 2 . . J
OOOOcOcO 64 65 65 65 65 20 20 20 20 20 20 20 20 66 66 66 JdeeeefffJ
OOOOcOdO 03 01 06 00 00 20 ff 98 00 00 00 2 b 68 02 00 00 . . +h . . . J
OOOOcOeO 00 00 06 07 80 00 00 00 32 01 10 64 66 66 66 00 J ..... .. . 2. .dff f. J

03 02 */
00
00 00 10 00 2c /*Record
00 00 00 2b 68 OO/*RowID
00 00 00 00 06 05/*TransactìonID*/
80 00 0000 32 01 10/ *Ro11 Poìnter*/
‘-
62 */
62 62 20 20 20 20 20 20 20 */
63 63 */

Pointer

recorder ,

03

00 00 20 ff 98/*Record Header*/
00 00 00 2b 68
00 00 00 00 06 07/*TransactionIO*/
80 00 00 00 32 01 10/ *Ro11 Pointer*/
*I
66 66

4 .3 .2
t:...
4.3

header

deleted_ flag
min_rcc_ flag
n owned 4
heap_no 13
n fìelds 10
Ibyte_offs_fiag
ncxt record 16
Total 48

mysql> CREATE TABLE mytest2


-> ENGINE=InnodB ROW FORMAT=Redundant
-> AS
-> SELECT * FROM mytest;
Query OK , 3 rows affected (0.00 s ec )
Records: 3 Duplicates: 0 Warn ings : 0

mysql> SHOW TABLE STATUS LIKE 'mytest2'\G;


1. row
Name: mytest2
Engine: InnoDB
-
V-raadp
imHIll-
nt·-nnnnrm
efottea-L
roRwaaxtn
w-Ddn
0
RV9 -z
X Rhhhh··t
d
t
U d
nt 6
o
43
U510ON
n 4 6UL
3tttten
AaA9999ee

-
r
Create time: 2009-03-18 15:49:42
Update_time: NULL
Check time: NULL
Collation: latin1 swedish ci
Checksum: NULL
Create_ options : row_ format =REDUNDANT
Comment:
1 row in set (0.00 sec)

mysql> SELECT * FROM mytest2\G;


1. row

t1: a
t2: bb
t3: bb
t4: ccc
2. row
tl: d
t2: ee
t3: ee
t4: fff
3. row
tl: d
t2: NULL
t3: NULL
t4: fff
3 rows in set (0.00 sec)

0000c070 08 03 00 00 73 75 70 72 65 6d 00 23 20 1 6 I....supr emum.. . I


0000c080 14 1 3 Oc 06 00 00 10 Of 00 ba 00 00 00 2b 68 Ob 1......... . ...+h.1
0000c090 00 00 00 00 06 53 80 00 00 00 32 01 10 61 62 62 I . . . . . s. . . . 2 . . abb I
OOOOcOaO 62 62 20 20 20 20 20 20 20 20 63 63 63 23 20 16 Ibb ccc' . I
OOOOcObO 1 4 13 Oc 06 00 00 1 8 Of oo ea 00 00 ...
OOOOcOcO 00 00 00 00 06 53 80 00 o 00 32 01 1e 64 65 65 1. ...
OOOOcOdO 65 65 20 20 20 20 20 20
OOOOcOeO 1 4 13 Oc 06 00 00 20 Of 00 7 4 00 00 00 2b 68 Od 1 . ..... ..t... +h. 1
OOOOcOfO 00 00 00 00 06 53 80 00 00 00 32 01 2c 64 00 00 1 ..... S .... 2. , d.. 1
0000Cl00 00 00 00 00 00 00 00 00 66 66 66 00 00 0 0 00 00 1... ... .. fff..... 1

23 20 16 1 4 13 Oc
00 00 10 Of 00 ba/ * Record
00 00 00 2b 68 Ob/ * Ro wIO* /
00 00 00 00 06 53/ *TransactionI D*/
80 00 00 00 32 01 10/*Roll Poi nt*/
'a' */
62 */
62 62 20 20 20 20 20 20 20
63 63 */

23 20 16 14 13 Oc Oc, 13, 14, 16, 20,

,...__

0000000111

21 ge 94 14 13 Oc
00 00 20 Of 00 74/*Record
00 00 00 2b 68 Od/*RowID*/
00 00 00 00 06 53/*Transacti onIO*/
80 00 00 00 32 01 10/ * Roll Point* /
•/
00 00 00 00 00 00 00 00 00
66 66

13 1494 ge
I 10 $

4 .3.3

SQL

mysql> CREATE TABLE test (


-> a VARCHAR(65535)
-> )CHARSET=latin1 ENGINE=InnoDB;
ERROR 1118 (42000); Row size too large. The maximum row size for the used table
type , not counting BLOBs , is 65535 . You have to change some columns to TEXT or
BLOBs

mysql> CREATE TABLE test (


- > a VARCHAR(65 532)
-> )CHARSET=latin1 ENGINE=InnoDB;
Query OK , 0 rows affected (0.15 sec )
.{,I. e ....
4.3

mysql> CREATE TABLE test (


-> a VARCHAR(65 535 )
-> )CHARSET=latinl ENGINE=InnoDB;
Query OK , 0 rows affected , 1 warning (0.14 sec)

mysql> SHOW WARNINGS\G;


1. ro w
Level: Note
Code: 1246
Message : Converting co lumn 'a' from VARCHAR to TEXT
1 row in set (0.00 sec)

mysql> SROW CREATE TABLE test\G;


1. row
Table: test
Create Table: CREATE TABLE ' test' (
'a' mediumtext
ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

mysql> CREATE TABLE test (


-> a VARCHAR(65 532)
-> )CHARSET=GBK
ERROR 1074 (42000): Column length too big for column 'a' (max = 32767); use
BLOB or TEXT instead

mysql> mysql> CREATE TABLE test (


-> a VARCHAR(65532)
-> JCHARSET=UTF8 ENGINE- InnoDB;
ERROR 1074 (42000): Column length too big for column ' a' (max - 21845); use
BLOB or TEXT instead
.{)I.C...
I12

mysql> CREATE TABLE test2


-> a VARCHAR(22000) ,
-> b VARCHAR(22000) ,
- > c VARCHAR(22000)
-> )CHARSET= latln1 ENGINE=Inno DB;
ERROR 1118 (42000): Row size too large. The maximum row size for the used table
type , not counting BLOBs , is 65535. You have to change some columns to TEXT or
8L08s

mysq1> CREATE TABLE t (


-> a VARCHAR(65532)
-> )ENGINE= InnoDB CHARSET=latinl ;
Query OK , 0 rows affected (0.15 sec)

mysql> INSERT INTO t


-> SELECT REPEAT('a' , 65532);
Query OK , 1 row affected (0.08 sec)
Records: 1 Duplicates : 0 Warnings: 0

_innodb_p age

[root@nineyouO-43 mytestli py_ innodb_ page_i nfO.py - v t.lbd


page offset 00000000 , page type <file Space Header>
page offset 00000001 , page type < I nsert Buffer 8itmap>
page offset 00000002 , page type <File Segment inode>
page offset 00000003 , page type <8-tree Node> , page level <0000>
page offset 00000004 , page type <Uncompressed BL08 page>
page offset 00000005 , page type <Uncompressed BLOB Page>
page offset 00000006 , page type <Uncompressed BLOB Page>
page offset 00000007 , page type <Uncompressed BLOB Page>
Total numbe r o f page: 8:
Insert Buffer Bitmap: 1
4.3 /1 3 . .

Uncompressed 8 L08 page : 4


Fi l e Space Header: 1
8-tree Node : 1
Fi le Segme n t i node : 1

BLOB

0
nununUAUnunU c
CCCCCCCCCC oaoooo
ba050 39 fbaoa06c5
fnunvoonvoob-
g
ff7010d311 --g
LAU<Un I g. . . . . . . . . . . . . . . 1
1 0000od AU
1.... j ...E....... 1
2 21 <
UAU<U
3
&03ddo U
4 AUAUAUA

2
56789abede <unue··nu
1• • • • • • • • • • • • • 2 • • I
AU 9 eRd
i nfimum. . . . . . 1
1• • •
n 2611
oool U<
UAU I s up remum ....... . 1
b1 U
1 • • • • + . . . •QK . • • • • 1
nuhUAVAU<unu l--l 6 rozororoz r
orororororo
•• 1-. . aaaaa aaaaaaaa l
C 1111 6 11111
-11l1
l aaaaaaaaaaaaaaaa l
CCCC•• - 1 666
•• ••
l aaaaaaaaaaaaaaa a l
-l1l ••
l aaaaaaaaaaaaaaa a l
.• 1 ll o
l aaaaaaaaaaaaaaaa l
,A 1 1 6 ro
l aaaaaaaaaaaaaaaa l
OOOOcOfO 61 61 61 6 1 61 61 6 1 6 1 61 6 1 61 61 61 61 61 61 l aaaaa aaa aaa aaaaa l
0000c100 61 61 61 61 61 6 1 61 61 61 61 6 1 61 61 61 61 61 I aaaaaaaaaaaaa a aa I
0000c110 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 1 aaaaaaaaa a aaaaaa I

0000c390 61 61 6 1 00 00 00 c3 00 00 00 04 00 00 00 2 6 00 l aaa ........ . ..&. I


0000c3aO 00 00 00 00 00 fc fc 00 00 00 00 00 00 00 00 00 1.. .......... .... I

(65532)

BLOB

prcfix 768bytes

BLOB Pagc
...

mysql> CREATE TALBE t (


-> a VARCHAR(9000)
->
Query OK , 0 rows affected (0.13 sec)

mysql> INSERT INTO t


-> SELECT REPEAT('a' , 9000);
Query OK , 1 row affecte d (0.04 sec)
Records: 1 Duplicates: 0 Warnings: 0

mysql> INSERT INTO t


- > SELECT REPEAT (' a' , 9000) ;
Quer y OK , 1 row affected (0.04 sec)
Records: 1 Duplicates: 0 Warnings: 0

_ innodb_p age_

(root@ nineyouO-43 my t est) ,


py_innodb_page_info .py -v t.ibd
page offset 00000000 , page type <File Space Head er>
page offset 00000001 , page type <Insert Buffer Bitmap>
page offset 00000002 , page type <File Segment inode>
page offset 00000003 , page type <B-tree Node> , page level <0000>
page offset 00000004 , page type <Uncompressed BLOB Page>
page offset 00000005 , page type <Uncompressed BLOB Page>
Tota l number of page: 6:
Inse rt Buffer Bitmap: 1
Uncompressed BLOB page: 2
File Space Header: 1
B-tree Node: 1
File Segment inode : 1

_ innodb_page_
., I. CA

mysql> CREATE TABLE t (


-> a varchar(8098)
)ENGINE=I nnoDB;
Query OK , 0 rows affected (0.12 sec)

mysq1> INSERT INTO t SELECT REPEAT('a' , 8098);


Query OK , 1 row a ffected (0.04 sec)
Records: 1 Dupl i ca tes : 0 Warnings : 0

mysql> I NSERT INTO t SELECT REPEAT ('a' , 8098);


Query OK , 1 row affected (0.03 sec)
Records: 1 Duplicates : 0 Warnings: 0

SQL

[root@nineyouO-43 mytestl# py_ innodb_page_info.py -v t.ibd


page offset 00000000 , page type <File Space Header>
page offset 00000001 , page type <Insert Buffer Bitmap>
page offset 00000002 , page type <File Segment inode>
page offset 00000003 , page type <B-tree Node> , page leve l <0000>
page offset OQOOOOOO , page type <Freshly Allocated Page>
page offset 00000000 , page type <Freshly Allocated Page>
Total number of page: 6:
Freshly Allocated page: 2
Insert Buffer Bitmap: 1
Fi le Space Header: 1
B-tree Node: 1
File Segment inode: 1

Uncompressed BLOB

mysql> CREATE TAB LE t (


-> a BLOB
C...
..

- > )ENGINE= InnoDB;


Query OK , 0 rows affected (0.12 sec)

mysql > I NSERT INTO t SELECT REPEAT('a' , 8000);


Quer'y OK , 1 row af fected (0.03 sec)
Records : 1 Duplicates: 0 Warnings: 0

mysql> INSERT INTO t SELECT REPEAT('a' , 8 000);


Query OK , 1 row affected (0.03 sec)
Records: 1 Duplicates : 0 Warnings: 0

mysql> INSERT INTO t SELECT REPEAT('a' , 8000);


Query OK , 1 row affected (0.01 sec )
Records: 1 Duplicates : 0 Warnings: 0

mysql> INSERT INTO t SELECT REPEAT('a ' , 8000);


Query OK , 1 row affected (0.06 sec )
Records: 1 Duplicates: 0 Wa rni ngs: 0

_innodb_page

(root@nineyouO-43 mytestl# py_innodb_page_i nfo .py -v t.ibd


page offset 00000000 , page type <file Space Header>
page offset 00000001 , page type <Insert Buffer Bitmap>
page offset 00000002 , page type <Fi l e Segment inode>
page offset 00000003 , page type <B-tree Node> , page 1evel <0001>
page offset 00000004 , page type <B-tree Node> , page level <0000>
page offset 00000005 , page type <B- tree Node> , page level <0000>
page offset 0 0000006 , page type <B- tree Node> , page 1evel <0000>
page offset 00000000 , page type <Freshly Allocated Page>
Total number of page: 8:
Freshly A1 10cated page: 1
Inse rt Buffer Bitmap : 1
File Space Header: 1
B-tree Node: 4
File Segment inode : 1
‘ tt l. e ....

4.3.4 ••

InnoDB

| I • I ...... I
OffPage

4 .3.5

mysq1> CREATE TABLE j (


-> a CHAR(2)
-> )CHARSET;GBK ENGINE=InnoDB;
Query OK , 0 rows affected (0.11 sec)

mysq1> INSERT INTO j SELECT 'ab';


Query OK , 1 row affected (0.03 sec)
.{, I.C...

Records: 1 Duplicates: 0 Warnings: 0 ••

mysql> SET NAMES GBK;


Query OK , 0 rows affected (0.00 sec)

mysql> INSERT INTO j SELECT


Qu er y OK , 1 row affected (0.04 sec)
Records: 1 Duplicates: 0 Warnings: 0

mysql > INSERT INTO j SELECT 'a';


Query OK , 1 row affected (0.03 sec)
Records: 1 Duplicates: 0 Warnings: 0

mysql> SELECT a , CHAR_LENGTH(a) , LENGTH(a)


-> FROM j \G:
*************************** 1. row
a: ab
CHAR LENGTH(a): 2
LENGTH(a): 2
2. row

CHAR LENGTH(a): 2
LENGTH(a): 4
3. row
a: a
CHAR LENGTH(a): 1
LENGTH(a): 1
3 rows in set (0.00 sec)

mysql> SELECT a , HEX(a)


-> FROM j \G:
1. row ***************************
a: ab
HEX(a): 6162
2. row

HEX(a): CED2C3C7
*************************** 3. row ***************************
a: a
.ttl , CA
4.3

HEX (a): 61
3 rows in set (0.00 sec)

ab I

0000c070 73 75 70 72 65 6d 75 6d 02 00 00 00 10 00 1c 00 Isupremum. .• .. ... I


0000c080 00 00 b6 2b 2b 00 00 00 51 52 da 80 00 00 00 2d I . . . + + • • • QR. . . . . - I
0000c090 01 10 61 62 04 00 00 00 18 ff d5 00 00 00 b6 2b I . . ab . . . . . . . . . . . + I
OOOOcOaO 2c 00 00 00 51 52 db 80 00 00 00 2d 01 10 ce d2 I , . . . QR. . . . . - . . . . I
OOOOcObO c3 c7 00 00 00 00 00 00 00 00 00 00 00 00 00 00

02
00
00 00 10 00 1c /* Recoder Header */
00 00 00 b6 2b 2b /* RowID */
00 00 00 51 52 da */
80 00 00 00 2d 01 10 /* Ro11 Point */
61 62 */

04
00 /*
00 00 18 ff d5 / * Recoder Header */
00 00 00 b6 2b 2c /* RowID */
00 00 00 51 52 db /* TransactionID */
80 00 00 00 2d 01 10 /* Roll Point */
c3 d2 c3 c7 */

02
00 /*
00 00 20 ff b7 /* Recoder
00 00 00 b6 2b 2d /* RowID * /
00 00 00 51 53 17 /*
80 00 00 00 2d 01 10 /* Roll Point * /

61 20 */
-.
..ttl.CA
._'-

4 .4

Header
o Page Header
o Records
Records
Space
Dircctory
o File Trailer
..{,I. CA
4.4 121 - .

PageHeader

Infimun +

User Records

Page Size

PageDirectory
,

File Trailer

4 .4 .1 File Header

File

File

FIL PAGE SPACE


4
OR CHKSUM

FIL PAGE OFFSET 4


(10.

FIL PAGE PREV 4 B+


FIL PAGE NEXT 4
FlL PAGE LSN 8 (Log

FIL PAGE TYPE 2

FIL PAGE FILE


8
FLUSH LSN
FIL PAGE ARCH
LOG NO OR SPACE 4
ID
..{)I.CA
._'-

FIL PAGE INDEX Ox45BF


FIL PAGE UNDO LOG Ox0002 Undo
FIL PAGE INODE Ox0003
FIL PAGE mUF FREE L1ST OxOOO4 Insert
FIL PAGE TVPE ALLOCATED OxOOOO
FIL PAGE IBUF BITMAP Ox0005
FIL PAGE TYPE SYS OxOO06
FIL PAGE TYPE TRX SYS OxOO07
FIL PAGE TYPE FSP HDR OxOOO8 File Space Header
FIL PAGE TYPE XDES
FIL PAGE TVPE BLOB OxOOOA

4.4.2 Page Header

Page

Directory " 4.4.5 Page


PAGE N D1 R SLOTS 2

PAGE HEAP TOP 2


PAGE N HEAP 2
PAGE FREE 2
PAGE GARBAGE 2
PAGE LAST INSERT 2

LEFT ( OxOI )
RIGHT (Ox 02)
PAGE D1 RECTION 2
a PAGE SAME REC (Ox03)
SAME PAGE (Ox04)
NO D1RECTION (OX05)
PAGE N D1RECTION 2
PAGE N RECS 2
PAGE MAX TRX ID 8
PAGE LEVEL 2
PAGE INDEX ID 8
ð.{t I , CA

••

header
PAGE BTR SEG LEAF 10

PAGE BTR SEG TOP 10

4 .4 .3 Record

B+ Tree

, , -

Record

4 .4.4 User Space

User InnoDB

Free
C...
.,'- -,.

4 .4 .5 Page

Page Directory

InnoDB

'd' , 'c' , 'b' , 'h' , 'f' , 'j' , 'k' , 'a') ,


,

Page

4.4.6 File Trailer

File

File
C...
M

MySQL _ checksum_

strict

4 .4 .7

mysql> DROP TABLE IF EXISTS t;


Query OK , 0 rows affected (0.04 sec)

mysql> CREATE TABLE t (


-> a INT UNSIGNED NOT NULL AUTO INCREMENT ,
-> b CHAR(10) ,
-> PRIMARY KEY(a) ,
-> )ENG INE=InnoDB CHARSET=UTF8;
Query OK , 0 rows affected (0.00 sec)

mysql> DELIMITER $$
mysql> CREATE PROCEDURE load_t (count INT UNSIGNED)
-> BEGIN
-> SET @c ; 0;
-> WHILE @c < count 00
-> INSERT INTO t
-> SELECT NULL , RE PEAT(CHAR(97+RANO() *26 ) , 10);
-> SET @c =@c+ l ;
-> ENO WH ILE;
-> ENO;
- > $$
Query OK , 0 rows affected (0.00 sec )

mysql> OELIM ITER


mysql> CALL load t(lOO);
Query OK , 0 rows affected (0.60 sec)

mysql> SELECTa , bFROM t LIMIT 10;


+----+---…---------+
I a I b
+…--+------------- --+
1 dddddddddd
2 hhh hhhhhhh
3 bbbbbbbbbb
4 iiiiiiiiii
5 nnnnnnnnnn
6 qqqqqqqqqq
7 0000000000

8 yyyyyyyyyy
9 yyyyyyyyyy
10 I vvvvvvvvvv

10 rows in set (0.00 sec)

_innodb_p age_
[roo t@nineyouO - 43 mytes tlf py_ innodb_ page _inf o .py -v t.ibd
page offset 00000000 , p age type <File Space Header>
page offset 0000000 1 , page type <Inser t Buffer Bitmap>
page offse t 00000002 , page type <File Segment inode >
page offset 00000003 , page type <B-tree Node> , page level <0000>
page offse t 00000000 , page type <Freshly Allocated Page>
page offset 00000000 , page type <Freshly Allocated Page>
Total n umber of page: 6:
Freshly Alloca ted page : 2
Insert Buffer Bitmap: 1
File Space Header: 1
B-tree Node: 1
File Segment inode: 1
C...
4.4 127--'

f00000000od00009
f5d0105ab4180249oe21004oa
fbcob0608666060606010200
ffooaod004d802090eoood00
fooooooooac812095e01218
f06002500008d2090e010
f000000000oad2190e6
0916od666606060,bolo200087670
33a40cddd408020900od00009d9
EOOOC2bod00002d9
oaooooe504280221000oa1E1909aroob
ooc3006d04080200odoooof-d90906o
oc0000951408320921800oa29190
OOOOcOOO IR.$.............I
0000c010 1....j...E.......1
0000c020 1• • • • • • • • • • •f . .. . 1
0000c030 1.... .c.d. ... . ...1
0000c040
0000c050 1 • . • • . • • • • • • . • 2. • 1
0000c060 1 . . . infimum. • • . • . 1
0000c070 Isupremum..... .". 1
0000c080 1......Qm.....-.. 1
0000c090 Idddddddddd...... 1
OOOOcOaO 1".......Qm..... - 1
OOOOcObO 1 ..hhhhhhhhhh.... 1
OOOOcOcO 1 .".......Qm....1
OOOOcOdO 1 .-. .bbbbbbbbbb.. 1
OOOOcOeO 1..(.".......Qm..1
OOOOcOfO 1 • • • -. • iiiiiiiiii 1
0000c100 1....0.".......Qm l
OOOOcllO 1 • • • • • - • • nnnnnnnn 1
0000c120 1nn. . . . 8 . " . . . . . . . 1
0000c130 .• IQm.... . -. . qqqqqq 1
0000c140 ••
1qqqq. . . . @. .. . . . . . 1
0000c150 .• 1 . . Qm. . • . . - • .00001
0000c160 1000000.. ..H." . ..I
0000c170
1. 1 . . . . Qm: . • . • -. • yy 1
000oa5bl
0000c180 lyyyyyyyy....P.... 1
0000c190 1......Qm.....-.. 1
0000c 1aO l yyyyyyyyyy....X. 1
0000c1bO
1 .. 1".......Qm..... -1
60bc
0000c1cO 1. .vvvvvvvvvv.... 1

OOOOcldO 1 '.".......Qm....1
·od
0000c1eO 1 • -. • kkkkkkkkkk. • 1
OOOOclfO 1..h..........Qm.. 1

OOOOffcO 00 00 00 00 00 70 Od ld Oc 95 Oc Od Ob 85 Oa fd 1 • • • • • p. . . . . . . . . . 1
OOOOffdO Oa 75 09 ed 09 65 08 dd 08 55 07 cd 07 45 06 bd 1 .u.. .e.. .U.. .E.. 1
OOOOffeO 06 35 05 ad 05 25 04 9d 04 15 03 8d 03 05 02 7d 1 • 5. . • • • • • • • •} 1
OOOOfffO 01 f 5 01 6d 00 e5 00 63 95 ae 5d 39 6a eO ac 93 1 ...m...c..19 j ... 1
. --. C...

lb 24

o ffffffff
0

o 0

0000000 Oa 6a eO ac

0000000

95 ae 5d 39 6a eO ac 93

ae 5d 39.

eO ac
Page

page Header (56 bytesl:


PAGE N DIR SLOTS = OxOOla
PAGE HEAP TOP=OxOdcO
PAGE N HEAP=O x8066
PAGE FREE= OxOO OO
PAGE GARBAGE=OxOOOO
PAGE LAST INSERT=OxOda5
PAGE DIRECTION=Ox0002
PAGE N DIRECTION=Ox0063
PAGE N RECS = Ox0064
PAGE MAX TRX ID=OxOOOOOOOOOOOOOOOO
PAGE LEVEL=OO 00
PAGE INDEX ID=OxOOOOOOOOOOOOOlba
PAGE BTR SEG LEAF=OxOOOOOOdc0000000200f2
PAGE BTR SEG TOP = OxOOOOOOdc000000020032
4.4

OOOOffcO 00 00 00 00 00 70 Od 1d od Ob85Oa f d |
OOOOffdO Oa 75 09 ed 09 65 08 dd 08 55 07 cd 07 45 06 bd 1 .u...e. .1
OOOOffeO 06 35 05 ad 05 25 04 9d 04 15 03 8d 03 05 02 7d • • • • • • • • } 1
OOOOfffO 0 1 f5 01 6d 00 e5 00 63 95 ae 5d 39 6a eO ac 93 1.. .m...c..]9j... 1

PAGE HEAP

OOOOcdbO 00 00 00 2d 0 1 10 70 70 70 70 7 0 70 70 70 70 70 1... -.. ppppppppppl


OOOOcdcO 00 - 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
OOOOcddO 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
OOOOcdeO 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

PAGE N

PAGE

PAGE

PAGE LAST

OOOOcdaO 00 03 28 f2 cb 00 00 00 64 00 00 00 51 6e 4e 80 1. . (.... .d.. .QnN.1


OOOOcdbO 00 00 00 2d 0 1 10 70 70 70 70 70 70 70 70 70 70 1.. . -. .ppppppppppl
OOOOcdcO 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 1.. ...... .... .... 1

PAGE
PAGE
PAGE

N HEAP
PAGE
PAGE

PAGE INDEX

0000c050 00 02 00 f2 00 00 00 dc 00 00 00 02 00 32 01 00 1..... .. .. ....2.. 1


0000c060 02 00 1c 69 6e 66 69 6d 75 6d 00 05 00 Ob 00 00 1.. .infmum. ..... 1
0000c070 73 75 70 72 65 6d 75 6d Oa 00 00 00 10 00 22 00 Isup remum.. ....".1

fI
00 02 00 1c /* recorder header */
69 6e 66 69 6d 75 6d 00
/* .. /
11
00 Ob 00 00 /* recorder header */
73 75 70 72 65 6d 75 6d */

Oxc07f

0000c070 73 75 70 72 65 6d 75 6d Oa 00 00 00 10 00 22 00 Isupremum.... ..".1


0000c080 00 00 01 00 00 00 51 6d eb 80 00 00 00 2d 01 10 1... ...Qm.....-.. 1
0000c090 64 64 64 64 64 64 64 64 64 64 Oa 00 00 00 18 00 Idddddddddd......1
OOOOcOaO 22 00 00 00 02 00 00 00 51 6d ec 80 00 00 00 2d 1".......Qm..... - 1

00 00 00 01 */
00 00 00 51 6d eb /* Transaction ID */
80 00 00 00 2d 01 10 /* Ro11 Pointer */
64 64 64 64 64 64 64 64 64 64 */
.{,I. CA
4.4 J3J - .

mysql> SELECT a , b , hex(b ) FROM t ORDER BY a LIMIT 1;


+---+--- ---- ---- -+------- - ------------------- --- --+
1 a 1 b 1 hex (b)

+---+------------+------------------------…-----+
1 1 1 dddddddddd 1 64646464646464646464 1
+---+------------+ --------------------------------+
1 row in set (0.00 sec)

Page
OOOOffcO 00 00 00 00 00 70 Od 1d Oc 95 Oc Od Ob 85 Oa fd ,... . .p.. . ....... 1
OOOOffdO Oa 75 09 ed 09 65 08 dd 08 55 07 cd 07 45 06 bd 1 .U.. .e. ..U. .. E .. 1
OOOOffeO 06 35 05 ad 05 25 04 9d 04 15 03 8d 03 05 02 7d • • • • • • • • 11
OOOOfffO 01 f5 01 6d 00 e5 00 63 95 ae 5d 39 6a eO ac 93 1 . ..m...c.. 19j ... 1

Page
00

OOOOcOeO 04 00 28 00 22 00 00 00 04 00 00 00 51 6d ee 80 1..(..........Qm..1
OOOOcOfO 00 00 00 2d 01 10 69 69 69 69 69 69 69 69 69 69 1.. .-. . iiiiiiiiii l
0000cl00 Oa 00 00 00 30 00 22 00 00 00 05 00 00 00 51 6d 1....0..... .. ...Qm l
0000c110 ef 80 00 00 00 2d 01 10 6e 6e 6e 6e 6e 6e 6e 6e 1 . . . . . -. • nnnnnnnn 1
0000c120 6e 6e Oa 00 00 00 38 00 22 00 00 00 06 00 00 00 Inn .... 8..........1
0000c130 51 6d fO 80 00 00 00 2d 01 1 0 71 71 71 71 71 71 IQm .. ...-..qqqqqql
0000c140 7 1 71 71 71 Oa 00 00 00 40 00 22 00 00 00 07 00 Iqqqq....@." ..... 1

Record
‘'
..,.#t

4.5 Named File

COff
File

Dynamic

InnoDB

List of animal names representing file format . */


static const char*file_format_name_map[) = {
"Antelope" ,
"Barracuda " ,
4.5 NÍlI1Jed File

"Cheetah " , ...


"Dragon" ,
"Elk" ,
11 Fox" ,

"Gazel le" ,
" Hornet" ,
" Impala " ,
" Jaguar " ,
" Kangaroo" ,
"Leopard" ,
IIMoose" ,
"Nautilus" ,
"Ocelot" ,
"Porpoise" ,
"Quail" ,
"Rabbi t" ,
"Shark" ,
"Tiger " ,
" Urchin" ,
" Viper" ,
"Whale" ,
"Xenops" ,
" Yak" ,
"Zebra"
};

mysql> SELECT @@versi6n\G;


1. row ************* *********…* *

@@version: 5.1.37
1 row in set (0.00 sec)

mysql> SHOW VARIABLES LIKE ' innodb_versio n ' \G;


1. row
Variab1e name: innodb version
Va1ue: 1. 0 .4
1 row in set (0.00 sec)

mysql> SHOW VARIABLES LIKE ' innodb_file_fo rmat ' \G;


1.
1
row
varz--
l
awbl
evn
i
-
aue
met ..
e·· --nzo
a·oa
bu
dcs
nEAU 'Ae 6&
HUa}
-dc
-ma
a Runuv

o ( e
.{,I. CA
s

InnoDB: Warning: the s y stem tablespace is in a


file format that this v ersion doesn't support

4.6

4.6.1

Primary

Foreign

o Primary Key
Key
o Foreign Key
4.6

4.6.2

Key UNIQUE INDEX

mysql> CREATE TABLE u (


-> id INT ,
-> name VARCHAR(20) ,
-> id card CHAR(18) ,
-> PRIMARY KEY ( id) ,
-> UNIQUE KEY ( name ));
Query OK , 0 rows affected (0.16 sec)

mysql> SELECT constraint_name , constraint_type


-> FROM
- > informa tion schema.TABLE CONSTRA INTS
-> WHERE table_schema = 'mytest' AND table_ name='u';\G;
*************************** 1 . row
constraint name: PRlMARY
constraint_ type: PRIMARY KEY
*************** ******* ***** 2. row
constraint name: name
constraint_type: UNIQUE
2 rows in set (0.00 sec)

mysql> ALTER TABLE u


-> ADD UNIQUE KEY uk id card (id card);
C...
‘'
Query OK , 0 rows affected (0 . 19 sec)
Records: 0 Duplicates: 0 War nings: 0

mysql> SELECT constraint_ name , constraint _ type


-> FROM
-> information schema.TABLE CONSTRAINTS
-> WHERE table_schema='mytest' AND table_name= 'u '; \G;
1. row ************** * ************
constraint name: PRIMARY
constraint_ type: PRIMARY KEY
2. row
constraint name : name
constraint _ type: UNIQUE
3. row ******** *******************
constraint name: uk id card
constraint_type: UNIQUE
3 rows in set (0.00 sec)

oreign

mysql> CREATE TABLE P (


-> id INT ,
- > u id I NT ,
-> PRlMARY KEY ( id) ,
- > FOREIGN KEY (u id) REFERENCES P (id ) ) ;
Query OK , o rows affected (0. 13 sec)

mysql > SELECT constraint _ name , c onstraint_ type


-> FROM
- > information schema.TABLE CONSTRAINTS
-> WHERE table_ schema= ' mytest ' and table_name='p'\G;
1 . row ****** ** **** ************** *
constraint name: PRlMARY
constraint_ type : PRlMARY KEY
* ** * ** * ********* *********** 2 . row
constraint_name: p _ ibfk_1
constraint_type: FOREIGN KEY
2 rows in set (0.00 sec )
e
-
-3

mysql> SELECT * FROM


6
-> information schema.REFERENTIAL CONSTRAINTS
-> WHERE constraint_schema= 'my t est ' \G;
1 . row * * *.. * *
CONSTRAINT CATALOG: NULL
CONSTRAINT SCHEMA: test2
CONSTRAINT_ NAME: p _ ibfk_ 1
CONSTRAINT CATALOG: NULL
UNIQUE CONSTRAINT SCHEMA: test2
CONSTRAINT NAME: PRlMARY
MATCH OPTION: NONE
UPDATE RULE: RESTRICT
DELETE RULE: RESTRICT
TABLE_ NAME: p
REFERENCED_ TABLE_ NAME: p
1 row in set (0.00 sec)

4.6.3

4.6 .4

mysql> CREATE TABLE a (


-> id INT NOT NULL ,
-> date DATE NOT NULL) ;
Query OK , 0 rows affected (0.13 sec)

my s ql> I NSERT INTO a


.{,I. CA

-> SELECT NULL , '2009-02-30';


Query OK , 1 row affected , 2 warnings (0.04 sec)
Records: 1 Duplicates: 0 Warnings: 2

rnysql> SHOW WARN I NGS\G;


1. row
Level: Warning
Code: 1048
Message: Colurnn ' id ' cannot be null
*************************** 2. row
Level: Warning
Code: 1265
Message: Data truncated for colurnn 'date' at row 1
2 rows in set (0.00 sec)

rnysql> SELECT * FROM a\G;


1. row
id : 0
date : 0000-00-00
1 row in set (0.00 sec)

2009-02-30' 0

rnysql> SET sql_rnode = 'STRICT_TRANS_ TABLES';


Query OK , 0 rows affected (0.00 sec)

rnysq1> INSERT INTO a


-> SELECT NULL , '2009-02-30';
ERROR 1048 (23000): Column ' id ' cannot be nu11

rnysq1> INSERT INTO a


-> SELECT 1 , '2009-02-30';
ERROR 1292 (22007): Incorrect date value: '2009-02-30' for colurnn 'date' at row 1
46 ad t;%
4.6.5

mysql > CREATE TABLE a (


- > i d INT ,
-> sex ENUM('male' , ' fe male'));
Query OK , 0 rows a ffecte d (0.12 sec)

mysql> I NSERT INTO a


-> SELECT 1 , ' f ema l e';
Query OK , 1 row affected (0.03 sec)
Re c ords: 1 Duplica t es: 0 Warn i ngs: 0

mysql> I NSERT INTO a


-> SELECT 2 , 'bi';
Query OK , 1 row affected , 1 wa r ning (0.03 sec )
Records: 1 Dupl icates: 0 Wa r nin g s: 1

mysql > SET sql _mode = 'STRICT_ TRANS_TABLES';


Query OK , 0 rows affec t ed (0.00 sec)

mysql > I NSERT I NTO a


-> SELECT 2 , 'bi';
ERROR 1265 (01000): Data t runcated for column 'sex' at row 1

4.6.6
I 40 A

MySQL

CREATE
(DEFINER = { user I CURRENT USER }J
TRIGGER trigger_name BEFOREIAFTER INSERTIUPDATEIDELET E
ON tbl_name FOR EACH ROW trigger_stmt

EACH

mysql> CREATE TABLE usercash (


-> userid INT NOT NULL ,
- > cash INT UNSIGNED NOT NULL);
Query OK , 0 r ows affec ted (0.11 sec)

mysql> INSERT INTO usercash


-> SELECT 1 , 1000;
Query OK , 1 row affected (0.03 se c)
Records: 1 Duplicates : 0 Warnings: 0

mysql> UPDATE usercash


-> SET cash=cash-(-20) WHERE userid=l;
Query OK , 1 row affected (0.05 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> CREATE TABLE usercash_err_log (
-> userid I NT NOT NULL ,
-> old cash INT UNSIGNED NOT NULL ,
- > new cash INT UNSIGNED NOT NULL ,
-> user VARCHAR ( 30) ,
-> time DATETIME);
Query OK , 0 rows affected (0 .13 sec)

mysql> DELIMITER $$
Query OK , 0 rows affected (0.00 sec)

mysql> CREATE TRIGGER tgr_userc ash_update BEFORE UPDATE ON usercash


->FOR EACH ROW
->BEGIN
-> IF new.cash-old.cash > 0 THEN
->INSERT I NTO u sercash_e rr_log
->SELECT old.userid , old .cash , new.cash , USER() , NOW();
->SET new.cash u old .cash;
->END IF;
->END;
->$$
Query OK , 0 rows affected (0 . 00 sec)

mysql> DELIMITER $$
Query OK , 0 rows affected (0.00 sec)

_ err
_ usercash_

_ err
mysql> DELETE FROM usercash;
Query OK , 1 row affected (0.02 sec)

mysql> INSERT INTO usercash


-> SELECT 1 , 1000;
Query OK , 1 row affected (0.03 sec)
Records: 1 Duplicates: 0 Warnings: 0

mysql> UPDATE usercash


-> SET cash c cash - (-20)
-> WHERE userid- 1;
Query OK , 0 rows affected (0.02 sec)
••
Rows matched : 1 Changed: 0 Wa r nings ; 0

mysql> SELECT • FROM usercash\G;


1 . row

userid: 1
cash: 100
1 row 1n set (0.00 sec)

mysql> SELECT .. FROM usercash_err_log\G;


1. row
userid: 1
o1d cash: 1000
new cash: 1020
user: root @1ocalhost
t ime: 2009-11-0 6 11:4 9:49
Message: Column ' id' cannot be null
1 row in set (0.00 sec)

_err

4.6.7

ICONSTRAINT [symbol) ) FOREIGN KEY


lindex_name) (index_ col_name , ...)
REFERENCES tb1_name ( index_ col_name ,. ..)
[ON DELETE refe rence_optio n)
ION UPDATE refere nce_option)
re ference_optio n :
RESTRICT I CASCADE I SET NULL I NO ACTION

mysq1> CREATE TABLE parent


- > id INT NOT NULL ,
..{)I.CA

-> PRlMARY KEY (id)


- > ) ENGINE=INNODB;
Query OK , 0 rows affected (0.13 sec)

mysql> CREATE TABLE child (


-> id INT , parent_id INT ,
-> FOREIGN KEY (parent_id) REFERENCES parent(id)
- > ) ENGINE=INNODB;
Query OK , 0 rows affected (0.16 sec)

DELETE

o SETNULL

o RESTRICT

UPDATE ,

SQL Server
CREATE

mysql> SHOW CREATE TABLE child\G;


1 . row
Table: child
Create Table: CREATE TABLE ' child ' (
' id ' int(ll) DEFAULT NULL ,
' parent_id ' int(ll) NOT NULL ,
KEY 'parent_i d' ('parent_i d') ,
CONSTRA I NT 'child_ibf k_l' FOREIGN KEY ( ' parent_id' ) REFERENCES ' parent' ('id')
) DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

mysql> SET foreign _key_checks = 0;


mysql > LOAD DATA …...
mysql> SET foreign_key_c hecks = 1;

4.7.1

CREATE
(OR REPLACEJ
[ALGORITHM = {UNDEFINED I MERGE I TEMPTABLEJ ]
[DEFINER = { user I CURRENT USER JJ
[SQL SECURITY ( DEFINER I INVOKER J)
VIEW view name [( co1umn 1ist))
AS se1ect stateme n t
[WITH [CASCADED I LOCAL) CHECK OPTIONj

( updatable CHECK

mysq1> CREATE TABLE t ( id I NT );


Query OK , 0 rows affected (0. 13 sec)

mysq l> CREATE VIEW v t


-> AS
- > SELECT • FROM t WH ERE id<lO;
Query OK , 0 rows affected (0.00 sec)

mys ql> I NSERT INTO v t SELECT 20 ;


Quer y OK , 1 row affected (0.03 sec)
Records: 1 Dup1icates: 0 Warn i ngs: 0

mysq1> SELECT * FROM v t;


Empt y set (0 . 00 sec)

CHECK
mysql> ALTER VIEW v t
-> AS
-> t WHERE id<10
CHECK OPTION;
Query OK , 0 rows affected (0 . 00 sec)

mysql> INSERT INTO v _ t SELECT 20;


ERROR 1369 (HYOO O): CHECK OPTION failed ' my test. v t '
‘ tv

mysql> SHOW TABLES\G;


1. row

Tables_in_mytest: t
2. row
Tables_i n_mytest : v_t
2 rows in set (0 . 00 sec)

SQL
mysql> SELECT " FROM information schema.TABLES
-> WHERE table_ type- ' BASE T ABLE'
-> AND table
1. row

TABLE CATALOG: NULL


TABLE_SCHEMA: mytest
TABLE NAME: t
TABLE TYPE: BASE TABLE
ENGINE: InnoDB
VERSION: 10
ROW_ FORMAT: Compact
TABLE ROWS: 1
AVG ROW LENGTH: 16384
DATA LENGTH: 16384
MAX DATA LENGTH: 0
I NDEX LENGTH: 0
DATA FREE: 0
AUTO INCREMENT: NULL
CREATE TIME: 2009-11-09 16: 27:52
UPDATE TIME: NULL
CHECK TIME: NULL
TABLE_COLLATION: utf8_general_ci
CHECKSUM: NULL
CREATE OPTIONS:
TABLE COMMENT:
1 row in set (0.00 sec)

data)
mysq l> SELECT * FROM
-> info r mation s che ma . VIEWS
- > WH ERE tabl e sC h ema =database()\G ;
1 . row

TABLE CATALOG : NULL


TABLE_ SCHEMA : my test
TABL E NAME : v t
V1EW_ DEF1N1T10N : s e lect ' my t e s t '. ' t ' . ' i d ' AS ' id ' from ' my t est'. ' t ' wh e re

(' my test ' . ' t '. ' id ' < 10)


CHECK OPT I ON: CASCA DED
1 S UPDATABLE : YES
DEF1NER : roo t @loca l host
SECURITY TYPE : DEFINER
CHARACT ER SET CL1 ENT : l a t i nl
COLLAT1 0 N CONNECT10N: l atinl s wedish ci
1 row i n set (0.00 sec)

4 .7.2

SQL

o BUILD IMMEDIATE
DEFERRED

BUILD
BUILD
C...
--.

OONDEMAND
OONCOMMIT
ON

OCOMPLETE
.0 FORCE

mysql> CREATE TABLE Orders


-> (
-> order id INT UNSIGNED NOT NULL AUTO INCREMENT ,
-> product_ name VARCHAR(30) NOT NULL ,
-> price DECI MAL(8 , 2) NOT NULL ,
-> amount SMALLINT NOT NULL ,
-> PRIMARY KEY (order id)
-> )ENGINE=InnoDB;
Query OK , 0 rows affected (0.13 sec)

mysql> INSERT INTO Orders VALUES


-> (NULL , 'CPU' , 135.5 , 1) ,
-> (NULL , 'Memory' , 48.2 , 3) ,
-> (NULL , • CPU' , 125.6 , 3) ,
-> (NULL , • CPU' , 105.3 , 4)
->
Query OK , 4 rows affected (0 . 03 sec)
Records : 4 Duplicates: Warnings: 0

mysql> SELECT • FROM Orders\G;


row ******..*********** * * ******
dea
ru --acn
etpmdmet
r-ro··
-n-lu
pv-o e···.
C3U-
P-RJ

CJAU

******************* ******** 2. row


dea
ru
0.d - ·dmet
-
etpm
r-roacn
··e····
-n-lu
pr o . MS
e-oo
m2ry

3. row
oddea
ru dm
etpm
r-ro
··e··
-n-lu
pr HU-

et EO<
U

4. row
order id: 4
product name: CPU
p rice: 105.30
amount: 4
4 rows in set (0.00 sec)

mysql> CREATE TABLE OrderS_ MV(


-> product_n ame .VARCHAR(30) NOT NULL
-> , price_sum DEClMAL(8 , 2) NOT NULL
-> apo
,,,oidt-s
ne
uc
mrz svc
uqn
mtI NT -a- NOT NULL
-> FLOAT NOT NULL
-> ez INT NOT NULL
-> , UNIQUE INDEX (product name)
-> );
Query OK , 0 rows affected (0.13 sec)

mysql> I NSERT INTO Orders MV


-> SELECT product narne
-> , SUM(price ) , SUM(amount) , AVG(price )
-> , COUNT(*)
-> FROM Orders
-> GROUP BY product_ name;
Query OK , 2 rows affec te d (0.02 sec)
Records: 2 Duplicates: 0 Warnings: 0

mysql>
mysql>
mysql> SELECT * FROM Orders MV\G;
1 . row
tp-m
p r o d U eap-O .. C6m··t
e---a
nacneE
met-sU·23
-luce 3uqn
nu-

svc
rr qJ

2. row **** ************* **** * *****


dpmrr
Ctener
Z oapos asvct
n--
uro--dl "03·o
eam--t(
m4uqdnve
mo
z e
sc
-
-luc
et

-
a-e
-
SS
en
r H }

DELIMITER $$

RFOE
ETRGSSS
CAFB
RAPH
pM GGTWpa
RS ROt
EzmEe n
qo-t-
ds
Ers-a
EEm
T··PHDHAUAUAUFeEp
oemuq Se q--e zOJ
.lnJ
E -
duSV
RO NCUC
RA

Y-
-PHPUP-

lll --oi zz
unu <

---
= ·'
,
5ET @o l d orders cnt = 0;

5ELECT IFNULL (price_sum , 0) , IFNULL (amount_sum , 0) , IFNULL (price_avg , 0) ,


IFNULL(orders cnt , 0)
FROM Orders MV
WHERE product_ name = NEW.product_name
INTO @old_price_ sum , @old_amount_sum , @old_price_avg , @old_ orders_ cnt;

5ET @new_ price_ sum = @old_p rice_sum + NEW.price;


5ET @new amount ' sum = @o l d amount sum + NEW.amount;
5ET @new o r ders cnt = @o l d orders cnt + 1;
5ET @new_price_avg = @new_price_sum / @new_orders_cnt ;

REPLACE INTO Orders MV


VALUE5(NEW.product_name , @new_p r i ce_sum , @new_amoun t_sum , @new_p rice_avg ,
@new orders cnt );

ENO;
$$

OELIMITER ;

mysql> IN5ERT INTO Orders VALUE5 (NULL , '550' , 299 , 3);


Query OK , 1 row affected , 1 warning (0.03 sec)

mysql> IN5ERT INTO Orders VALUE5 (NULL , 'Memory' , 47.9 , 5);


Query OK , 1 row affected (0 . 03 sec)

mysql> 5ELECT * FROM Orders_MV\G;


1. row
tpmrr
PE O d ucapo.. C6m··t
e--
acnez
n-luce
met-s P U·23
··ro--
3uqn
nu-

svc
-
a ,.. 3d

.
2 . row
pr e--uce
oapon--Ea
uroLd oz vd
MOB-
e6m··t
asve
m9uqn
-GP-mZE eO
mRd
-et-s

··a"2··
. -.
C...

3. row
oadpmrrs
etenern
n--
uro---d e9m·
·· eanunu
asvce
m2uqnt nunu
P
-r
·
-LUce
e 9
t- ··
-
p a 2
-OH ·
S t ··
-
-A s { se c}

4.8

4.8.1

MySQL

Ç)
48

mysql> SHOW VARIABLES LIKE


1. row
v
ar
ll
aw
o
r evn
ll met
n aue vo
aso
e·· hE· l •·" 0 n
e D&e
ac nq
- -
als·· v··nv
( s

mysql> SHOW PLUGINS\G;

2 . row

Name : partition
Status: ACTIVE
Type: STORAGE ENGINE
Library: NULL
License: GPL

9 rows in set ( 0.01 sec)

5.5

5.5

mysql> CREATE TABLE t l (


-> co11 INT NOT NULL ,
••
-> co12 DATE NOT NULL ,
-> co13 INT NOT NULL ,
-> co14 INT NOT NULL ,
-> UNIQUE KEY (coll , co12)
->
- > PARTITION BY HASH(co13)
-> PARTITIONS 4:
ERROR 1503 (HYOOO): A PRIMARY KEY must inc1ude a11 co1umns in the tab1e's
partitioning function

mysq1> CREATE TABLE t l


-> co1l INT NULL ,
-> co12 DATE NULL ,
-> co13 INT NULL ,
-> co14 INT NULL ,
-> UNIQUE KEY (co1l , co12 , co13 , co14)
->
-> PARTITION BY HASH(co13)
-> PARTITIONS 4:
Query OK , 0 rows affected (0.53 sec)

CREATE TABLE t l (
coll INT NULL ,
co12 DATE NULL ,
co13 INT NULL ,
co14 INT NULL
)engine=innodb
PARTITION BY HASH(co13)
PARTITIONS 4:

CREATE TABLE t l
co1l INT NULL ,
co12 DATE NULL ,
co13 INT NULL ,
co14 INT NULL ,
key (co14)
)engine=innodb
4.8

PARTITION BY HASH(co13)
PARTIT IONS 4;

4.8.2

CREATE TABLE t (
i d INT
)ENGINE=INNDB
PARTITION BY RANGE (id) (
PARTITION pO VALUES LESS THAN (10) ,
PARTITION p1 VALUES LESS THAN (20)) ;

t# P#p l .ibd:

mysq1> system 1 s -1h /usr /1ocal/mysq1/data/t est2/t*


-rw-rw---- 1 mysq1 mysq1 8.4K 14:11 /u s r/1ocal/mysq1/data /test2/t.frm
-rw-rw- --- 1 mysq1 mysq1 28 14:11 /usr/1oca1/mysql/d ata/test2/t.par
-rw-rw---- 1 mysq1 mysq1 96K 14:12 /usr/local/mysq1/da ta /test2/t ' P lpO.
ibd
- rw- r w--- - 1 mysql mysq1 96K 14:12 /usr/ 1ocal/mysq1 /data/test2/tI Plp1 .
ibd

mysq1> INS ERT INTO t SELECT 9;


Query OK , 1 row affected (0.03 sec)
Records : 1 Dup1icates: 0 Warnings : 0

mysql> I NSERT INTO tSELECT 1 0 ;


Query OK , 1 row affected (0.03 sec)
Records: 1 Duplicates: 0 Warnings: 0

mysq1> INSERT INTO t SELECT 15;


Query OK , 1 row affecte d (0.03 sec )
Records : 1 Duplicates : 0 Warnings : 0
156 .J.. ‘'

mysq1> SELECT ... FROM information_schema.P ARTITIONS


-> WHERE tab1e schema =database() AND table name = ' t'\G;
1. row
T.ABLE CATALOG: NULL
T.AB LE SCHEMA: test2
TABLE NAME: t
PARTITION_ NAME: pO
SUBPARTITION NAME: NULL
PARTITION ORDINAL POSITION: 1
SUBPARTITION ORDINAL POSITION: NULL
PARTITION METHOD: RANGE
SUBPARTITION METHOD: NULL
PARTITION EXPRESSION: i d
SUBPARTITION EXPRESSION: NULL
PARTITION DESCRIPTION: 1 0
TABLE ROWS: 1
-_
AVG ROW LENGT H: 16384
DATA LENGTH: 16384
MAX DATA_LENGTH:
_ NULL
INDEX LENGTH: 0
DATA FREE : 0
CREATE TIME: NULL
UPDATE T I ME: NULL
CHECK TIME: NULL
CHECKSUM: NULL
PARTITION COMMENT:
NODEGROUP: defau1t
TABLES PACE NAME: NULL
2. row ******************* ********
TABLE CATALOG: NULL
TABLE SCHEMA: test2
TABLE NAME: t
PARTITION_ NAME: p1
SUBPARTITION NAME: NULL
PARTITION ORDINAL POSITION: 2
SUBPARTITION ORDINAL POSITION: NULL
PARTI TION METHOD: RANGE
SUBPARTITION METHOD : NULL
PARTITION EXPRESSION : id
SUBPARTITION EXPRESSION: NULL
PARTITION DESCRIPTION: 20
4.8

TABLE ROWS: 2
AVG ROW LENGTH: 8192
DATA LENGTH: 16384
MAX DATA LENGTH: NULL
INDEX LENGTH: 0
DATA FREE: 0
CREATE TIME: NULL
UPDATE TIME: NULL
CHECK TIME: NULL
CHECKSUM: NULL
PARTITION
NODEGROUP: default
TABLESPACE NAME: NULL
2 rows in set (0.00 sec)

TABLE

mysql> INSERT INTO t SELECT 30;


ERROR 1526 (HYOOO): Table has no partition for value 30

mysql> ALTER TABLE t


-> ADD PARTITION(
-> partition p2 values less than maxvalue );
Query OK , 0 rows affected (0.45 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> INSERT INTO t SELECT 30;


Query OK , 1 row affected (0.03 sec)
Records: 1 Duplicates: 0 Warnings: 0
.() I. C....
..
mysql> CREATE TABLE sales (
-> money INT UNSIGNED NOT NULL ,
-> date
-> )ENGINEe INNODB
-> PARTITION by RANGE (YEAR (date) )
-> PARTITION p2008 VALUE LESS THEN (2009) ,
-> PARTITION p2009 VALUE LESS THEN (2010) ,
-> PARTITION p2010 VALUE LESS THEN (2011)
- > );
Query OK , 0 rows affected (0.34 sec)

mysql> I NSERT I NTO sales SELECT 100 , '2008-0 1-01 ';


Qu ery OK , 1 ro w affected (0.03 sec)
Record s : 1 Ouplicates : 0 Warnings: 0

mysql> INSERT INTO sal es SELECT 100 , '2008-02-01';


Query OK , 1 r o w affected (0.03 sec)
Records: 1 Ouplicates: 0 Warnings: 0

mysq1> INSERT I NTO sales SELECT 200 , '2008-01 - 02 ' ;


Query OK, 1 row a ffected (0.04 sec)
Records: 1 Oupl ica tes: 0 Warnings: 0

mysql> INSERT INTO sales SELECT 100 , '2009-03-01';


Query OK , 1 r o w a ffected (0 . 03 sec)
Records: 1 Oup1icates : 0 Warnings: 0

mysql> INSERT INTO sales SELECT 2 00 , '2010-03-0 1 ' ;


Query OK , 1 row affected (0 .03 sec)
Record s : 1 Ouplicates: 0 Warnings: 0

FROM sales WHERE date>= , 2008-01-01' and date < , 2009-01-01 ' ,

mysql> a1ter tab1e sa1es drop partition p2008;


Query OK , 0 rows affected (0.18 s ec)
Records: 0 Ouplicates : 0 Warnings : 0

mysql> EXPLAI N PARTITIONS


-> SELECT * FROM sales
4.8
. -.
'-

-> WHERE date>= '2008-0 1-01' AND date<= '2008-12-31 ' \G;
1 . row **************************.
id: 1
sel ec t _ typ e: SIMPLE •
table: sales
partitions: p2008
type: ALL
possible_keys: NULL
k ey: NULL
key_ len: NULL
ref: NULL
rows : 5
Extra: Using where
1 row in set (0.00 sec)

Pruning

mysql> EXPLAIN PARTITIOENS


- > SELECT * FROM sales
- > WHERE date>= ' 2008-01-01 ' AN D date<'2009-01-01 ' \G;
1 . row

id: 1
select_type: SIMPLE
table : sales
part i ti o ns: p2008 , p2009
t ype : ALL
possible_ keys : NULL
key: NULL
key_le n: NULL
ref: NULL
rows: 5
Extra: Using where
1 row i n set ( 0 .00 sec)

' 2009-0] -0] 2008-] 2-3]


mysql> CREATE TABLE sales(
-> money INT UNSIGNED NOT NULL ,
-> date DATETIME
-> )ENGINE=INNODB
-> PARTITION by RANGE (YEAR(date)*lOO+MONTH(date))
- > PARTITION p201001 VALUES LESS THEN (201002) ,
-> PARTITION p201002 VALUES LESS THEN (201003) ,
- > PARTITION p201003 VALUES LESS THEN (201004)
-> );
Query OK , 0 rows affected (0.37 sec)

mysql> EXPLAIN PARTITIONS


-> SELECT * FROM sales
-> WHERE date>= '2010-01-01' AND date<='2010-01-31'\G;
1. row

id: 1
select_type: SIMPLE
table: sales
partitions: p201001 , p201002 , p201003
type: ALL
possibl e_keys: NULL
key: NULL
key l en: NULL
ref: NULL
rows: 4
Extra: Us i ng whe re
1 row in set (0.00 sec)

p201002 ,
TO_
SECONDSO ,

mysql> CREATE TABLE sales(


-> money INT UNSIGNED NOT NULL ,
-> date DATETO , E
-> )ENGINE= INNODB
-> PARTITION by range (TO_DAYS(date))
-> PARTITION p201001
-> VALUES LESS THEN(TO DAYS('2010-02-01')) ,
-> PARTITION p201002
-> VALUES LESS THEN (TO DAYS('2010- 03-01 ' )) , •
- > PARTITION p201003
-> VALUES LESS THEN (TO DAYS( '2010 - 0 4-01 ' ))
-> );
Query OK , 0 rows affected (0 .36 sec)

mysql> EXPLAIN PATITIONS


-> SELECT * FROM sales
-> WH ERE AND date<= '2 010-01-31 ' \ G;
1 . row

id: 1
select_ typ e : SIMPLE
table: sales
partitions : p201001
type: ALL
possible_keys: NULL
key : NULL
key_len: NULL
ref: NULL
rows: 4
Ex tra : Using where
1 row in set (0.00 sec)

2.

mysql> CREAT E TABLE t (


- > a I NT ,
-> b INT) ENGINE=INNODB
-> PART ITION BY LIST(b) (
- > PARTITION pO VALUES IN (1 , 3 , 5 , 7 , 9) ,
- > PARTITION p1 VALUES IN (0 , 2 , 4 , 6 , 8)
-> );
Query OK , 0 rows affected (0.26 sec)

LESS

mysql> I NSERT INTO t SELECT 1 , 1;


Query OK , 1 row affected (0.03 sec)
.,,'-

Records: 1 Duplicates: 0 Warnings: 0

mysql> INSERT INTO t SELECT 1 , 2:


Query OK , 1 row affected (0.03 sec)
Records: 1 Duplicates: 0 Warnings: 0

mysql > INSERT INTO t SELECT 1 , 3:


Query OK , 1 row affected (0.03 sec)
Records: 1 Duplicates: 0 Warnings : 0

mysql> INSERT INTO t SELECT 1 , 4:


Query OK , 1 row affected (0.03 sec)
Records: 1 Dup1icates: 0 Warnings: 0

mysql> SELECT table_ name , partition_name , table_rows


-> FROM information schema.PARTITIONS
-> WHERE table name= ' t ' AND table schema=DATABASE()\G:
1. row
btb
eoe
a--a ems
nano
..
maw
-n
pa r e··D-AU
-
r
2. row ***************************
btbs
a--aw anos
emst
mawe
n-E
eoe--·
·e··
-n-n
a rz
p-2 .. P.
'i

( ec}
U AU cu
<

mysq1> INSERT INTO t SELECT 1 , 10 :


ERROR 1526 (HYOOO): Tab1e has no partition for value 10

mysql > CRATE TABLE t (



-> a INT ,
- > b INT)ENGINE=MyISAM
-> PARTITION BY LIST(b) (
- > PARTITION pO VALUES IN (1, 3 , 5 , 7 , 9) ,
-> PARTITION p1 VALUES IN (0 , 2 , 4 , 6 , 8)
- > );
Query OK , 0 rows affected (0.05 sec)

mysql> INSERT INTO t VALUES (1 , 2) , (2 , 4) , (6 , 10) , (5 , 3);


ERROR 1526 (HYOOO) : Table has no part i tion for value 10

mysql> SELECT * FROM t ;


+-- ----+------ +
I a I b
+------+----- -+
1 I 2 I
2 I 4 I
+------+----- -+
2 rows in set (0.00 sec)

2) , ( 2 , 4)

mysql> TRUNCATE TABLE t;


Query OK , 2 rows affected (0.00 sec)

mysq1> ALTER TABLE t ENGINE- I nnoDB;


Query OK , 0 rows affected (0.25 sec)
Records: 0 Duplicates : 0 Warnings: 0

mysql> INSERT INTO t VALUES (1 , 2) , (2 , 4) , (6 , 10) , (5 , 3);


ERROR 1526 (HYOOO) : Table has no parti tion for value 10

mysql> t;
Empty set (0.00 sec)

BY HASH
164

CREATE TABLE t hash


a INT ,
b DATETIME
)ENGINE= I n n oDB
PARTIT I ON BY HA SH (YEAR(b ))
PARTITI ONS 4;

MOD (YEAR (' 2010 - 04 - 01 ' ). 4)

-2

my s q1> INSERT INTO t _ hash SEL ECT 1 , '2010- 04-0 1 ';


Query OK , 1 row affected (0.04 sec)
Rec o r ds: 1 Duplicates : 0 Warnings: 0

mysq1> SELECT table_ name , partiti on_n ame , t abl e _ro ws


-> FROM information s c hema.PARTITIONS
-> WHERE t able_ schema o DATABASE () AND table_ name= ' t _hash' \G ;
1 . row

t able name: t h ash


pa r --aoe aw
n noe·· P
ms A
U
-
r AU

2. row
a btb
- e n- --
mem
an as KH
hu'A
-n -
pa E --a-L oe a e· P
l Eo ws ·
.. * ,..... 3. row
a e n ano
mems
: a s hu
-n -nr
pa r -lao awe ..
m
e r ··
4 . row
t able name: t h ash
parti t i on_name: p3
e
5--
3
tab l e rows: 0
6
4 rows in set (0.00 sec )

linear

CREATE TABLE t_1inear_has h(


a INT ,
b DATETIME:
)ENGINE= InnoDB
PARTITION BY LINEAR HASH (YEAR(b))
PARTITIONS 4;

201 0-04-01

V=POWER(2. CEIL fN G(LOG(2.


num)))=4;

YEAR( , 2010-04-01

mysql> I NSERT INTO t _ linear_hash SELECT 1 , '2010-04-01';


Query OK , 1 row affected (0.02 sec)
Records: 1 Duplicates: 0 Warnings: 0

mysql> SELECT table_name , partition_ name , table _rows


-> FROM information schema.PARTITIONS
-> WHERE tab1e _ schema =DATABASE()
-> AND
1. row
table name: t linear hash
partition_ name: pO
table rows: 0
2. row ******************* ********
.{,I. CA
-.

tðb1e name: t 1inear hash


partition_name: pl
tab1e rows: 0
3. row
--l
a--a
btb
eoe
-
ano
maw
ems
n-z .,. -Pl n e ar
--e·· h as h
par n
‘t
a

4. row * * '*..
tab1e name: t l i near hash
partition name: p3
tab1e rows: 0
4 rows i n set (0.01 sec)

LINEAR

mysq1> CREATE TABLE t _ key (


-> a INT ,
-> b
-> PARTITION BY KEY (b)
-> PARTITIONS 4;
Query OK , 0 rows affected (0.43 sec)

interger )
YEARO , TO_DAYSO ,

RANGE
.{,I. CA
4.8 167- .

CREATE TABLE t_co lumns_range(


a INT ,
b DATETIME
)ENGINE=INNODB
PARTITION BY RANGE COLUMNS (B) (
PARTITION pO VALUES LESS THAN ('2009-01-01') ,
PARTITION p1 VALUES LESS THAN ('2010-01-01')

CREATE TABLE customers 1


first name VARCHAR(25) ,
last_name VARCHAR(25) ,
street_1 VARCHAR(30) ,
street 2 VARCHAR(30) ,
city VARCHAR(15) ,
renewal DATE

PARTITION BY LIST COLUMNS(city)


PARTITION pRegion_1
VALUES IN('Oskarshamn' , 'Högsby' , 'Mönsterås') ,
PARTITION pRegion_2
VALUES IN('Vimmerby' , 'Hu ltsfred ' , 'Västervik') ,
PARTITION pRegion_3
VALUES IN('Nässjö' , 'Eksjö' , 'Vetlanda') ,
PARTITION pRegion_4
VALUES IN('Uppvidinge' , 'Alvesta' , 'Växjo')
.{,I. CA

CREATE TABLE rcx (


a INT ,
b INT ,
c CHAR(3) ,
d INT
) Engine=InnoDB
PARTITION BY RANGE COLUMNS(a , d , c) (
PARTITION pO VALUES LESS THAN (5 , 10 , 'ggg') ,
PARTIT I ON p1 VALUES LESS THAN (10 , 20 , 'mmmm') ,
PARTITION p2 VALUES LESS THAN (15 , 30 , 'sss') ,
PARTIT I ON p3 VALUES LESS THAN (MAXVALUE , MAXVALUE , MAXVALUE)

4.8.3

partitioning)o

mysql> CREATE TABLE ts (a INT , b DATE)eng i ne=i nnodb


- > PARTITION BY RANGE( YEAR(b) )
-> SUBPART ITION BY HASH( TO DAYS(b) )
-> SUBPARTITIONS 2
-> PARTITION pO VALUES LESS THAN (1990) ,
-> PARTITION p1 VALUES LESS THAN (2000) ,
- > PARTITION p2 VALUES LESS THAN MAXVALUE
-> );
Query OK , o rows affected (0 . 0 1 sec)

mysq1> system ls -lh /usr/local/mysql/data/test2/ts*


-rw-rw - - -- 1 mysql mysql 8.4K Aug 1 15:50 /usr/ l oca l /mysql/data/test2/ts.frm
-rw-rw- -- - 1 mysql mysql 96 Aug 1 15:50 /usr/loca1/mysq1/data/test2/ts.par
- rw-rw - --- 1 mysq1 mysq1 96K Aug 1 15: 50 /usr/1oca1/mysq1/data/test2/
tsNP'pOtSP'pOspO.ibd
-rw-rw---- 1 mysq1 mysq1 96K Aug 1 15 :50 /usr/loca1/mysq1/data/test2/
tslP'pONSP'pOsp1.ibd
-rw-rw---- l mysq1 mysq1 96K Aug 1 15: 50 /usr/1oca1/mysql/data/test2/
tsiPNp11SP'plspO.ibd
- rw-rw- --- 1 mysq1 mysq1 96K Aug 1 15 :5 0 /usr/loca1/mysq 1 /data/test2/
8-U

•••
ts'Plpl'SP'plspl . ibd
-rw-rw---- 1 mysql my5ql 96K Aug 1 15: 50
ts # P#p2#SPlp 25pO.ibd
1 mysql my5ql 96K Aug 1 15:50 IU5r/loca l/m Y5ql/data/test21
ts#PØp2KSP#p2sp1.ib d

mysql> CREATE TABLE t5 ( a I NT , b DATE)


-> PARTITION BY RANGE( YEAR(b ))
-> SUBPARTITION BY HASH( TO DAYS ( b) ) (
-> PARTITION pO VALUES LESS THAN (1990)
-> SUBPARTITION sO ,
-> SUBPARTITION sl
-> ),
-> PARTITION p1 VALUES LESS THAN (2000) (
- > SUBPARTITION 52 ,
-> SUBPARTITION s3
-> ),
-> PARTITION p2 VALUES LESS THAN MAXVALUE
-> SUBPARTITION 54 ,
-> SUBPARTITION 55
-> )
-> );
Query OK , 0 r OW5 affected (0.00 sec)

my5ql> CREATE TABLE ts (a INT , b DATE)


-> PARTITION BY RANGE( YEAR(b) )
-> SUBPARTITION BY HASH( TO DAYS(b) ) (
-> PARTITION pO VALUES LESS THAN (1990)
-> SUBPARTITION sO ,
-> SUBPARTITION sl
-> ),

-> PARTITION p1 VALUES LESS THAN (2000) ,


-> PARTITION p2 VALUES LESS THAN MAXVALUE (
••
-> SUBPARTITION s2 ,
SUBPARTITION s3
->
-> );
ERROR 1064 (42000): Wrong number of 8ubpartitions defined , miamatch with
previOU8 setting near '
PARTITION p2 VALUES LESS THAN MAXVALUE (
SUBPARTITION s2 ,
SUBPARTITION s3

)' at line 8

mysql> CREATE TABLE ts (a INT , b DATE)


-> PARTITION BY RANGE( YEAR(b) )
-> SUBPARTITION BY HASH ( TO DAYS (b) ) (
-> PARTITION pO VALUES LESS THAN (1990)
-> SUBPARTITION sO ,
-> SUBPARTITION s1
-> ),
-> PARTITION p1 VALUES LESS THAN (2000) (
-> SUBPARTITION sO ,
-> SUBPARTITION s1
-> ),
-> PARTITION p2 VALUES LESS THAN MAXVALUE (
-> SUBPARTITION sO ,
-> SUBPARTITION s1
->
-> );
ERROR 1517 (HYOOO): Dup1icate partition name aO

mysql> CREATE TABLE ts (a INT , b DATE)ENGINE=MYISAM


-> PARTITION BY RANGE( YEAR(b) )
-> SUBPARTITION BY HASH( TO DAYS(b) ) (
-> PARTITION pO VALUES LESS THAN (2000)
-> SUBPARTITION sO
-> DATA DIRECTORY = '/diskO/data'
-> INDEX DIRECTORY = '/diskO/idx' ,
-> SUBPARTITION sl
-> DATA D1RECTORY = '/di5 k1/data '
-> 1NDEX D1RECTORY - '/disk1/idx '
-> ),
-> PART1TION p1 VALUES LESS THAN (2010)
-> SUBPART1TION 52
-> DATA DIRECTORY = ' /disk2/data'
-> 1NDEX DlRECTORY m '/di5k2/idx' ,
-> SUBPART1T10N 53
-> DATA DIRECTORY = '/di5k3/data'
-> INDEX D1RECTORY - '/disk3/idx'
-> ),
-> PARTIT10N p2 VALUES LESS THAN MAXVALUE
-> SUBPARTITION s4
-> DATA
-> 1NDEX
-> SUBPARTITION 55
-> DATA DlRECTORY - '/disk5/data '
- > INDEX DIRECTORY = '/disk5/idx'
-> )
-> );
Query OK , 0 rows affected (0.02 sec)

mysq1> CREATE TABLE ts (a 1NT , b DATE)engine= innodb


-> PARTITION BY RANGE( YEAR(b) )
-> SUBPART1TION BY HASH( TO_DAYS(b) ) (
-> PARTITION pO VALUES LESS THAN (2000)
-> SUBPARTITION sO
-> DATA DIRECTORY - '/diskO/data'
-> INDEX DIRECTORY • ' /diskO/idx ' ,
-> SUBPARTITION s1
-> DATA DlRECTORY - ' /disk1/data'
-> 1NDEX DIRECTORY - '/disk1/idx'
-> ) ,
-> PART1T10N p1 VALUES LESS THAN (2010)
-> SUBPARTITION s2
-> DATA
-> 1NDEX D1RECTORY - '/dis k2/idx' ,
-> SUBPARTIT10N s3
-> DATA DIRECTORY D '/di5k3/data'
C...
e> 3

-> INDEX DIRECTORY = '/disk3/idx '


••
-> ),
-> PARTITION p2 VALUES LESS THAN MAXVALU E
-> SUBPARTITION s4
- > DATA DIRECTORY = '/disk4 /da ta '
-> INDEX DIRECTORY = '/disk4/idx' ,
-> SUBPARTITION s5
- > DATA DIRECTORY = '/dis k5/data'
-> INDEX DIRECTORY = '/disk5 / idx'
->
-> );
Query OK , 0 rows affected (0.02 sec)

mysq1> system 1s -1h /usr/ 1oca1/mysq1/data /test2/ t s*


- rw-rw---- 1 mysq1 mysq1 8.4K Aug 1 1 6:24 /usr/1oca1/mysq1/data/tes t2/ts.frm
-rw-rw---- 1 mysq1 mysq1 80 Aug 1 16:24 /usr/1oca1/mysq1/data/test2/ts.par
-rw-rw---- 1 mysq1 mysq1 96K Aug 1 16:25 /usr/1oca1/mysq1/data/test2/ts#P#pO'SPIsO.
i bd
-rw-rw---- 1 mysq1 mysq1 96K Aug 1 1 6:25 /usr/1oca1/mysq1/data/test2/ts'P#pO'SP'sl.
ibd
- rw- rw---- 1 mysq1 mysq1 96K Aug 1 16:25 /usr/1oca1/mysq1/data/test2/ts'Plp1#SPJs2.
ibd
-rw-rw---- 1 mysq1 mysq1 96K Aug 1 16 :25 /usr/1oca1/mysq1/data/test2/tslP#p1'SPis3.
ibd
-rw-rw---- 1 mysq1 mysq1 96K Aug 1 16:25 /usr/1oca1/mysq1/data/test2/tsiP#p2#SPis4.
ibd
-rw-rw---- 1 mysq1 mysq1 96K Aug 1 16 : 25 /usr/1oca1 /mysq1/data/test2/ts'Plp2tSP's5.
ibd

4.8 .4

mvds 1&> ATT A


TPHPH
CnE ENN TGYM Itn
nDMH aDe(
nB
-
ab
>> T--TA
'}
MH <
b vA n
4.8

-> PARTITION BY RANGE(b) (


••
- > PARTITION p O VALUES LESS THAN ,
( 10 )
-> PARTITION pl VALUES LESS THAN (20) ,
-> PARTITION p2 VALUES LESS THAN MAXVALUE
- > );
Query OK , o rows affected (0.01 sec)

my s ql> I NSERT INTO t _ range SELECT 1 , 1;


Query OK , 1 rowaffected (0.00 sec)
Records : 1 Duplicates : 0 Warnings: 0

mysql> INSERT INTO t _ range SELECT 1 , NULL;


Query OK , 1 row affec ted (0.00 sec)
Records : 1 Duplicates : 0 Warnings: 0

mysql> SELECT " FROM t_ r ange\G ;


1. r ow
a: 1
b: 1
2. row
a: 1
b: NULL
2 rows in set (0. 00 sec)

mysql > SELECT table_ name , partition_name , table_ rows


-> FROM information schema.PARTITIONS
-> WHERE AND
1 . row
btb
eoe
a'la ems
ano
maw
n-r a n qe
··e··rnv
- -
P·AM Z n .. P·
-
******************* *.****** 2. row
eoe ems
ano
maw
n-E +lwnu
--e· ann3e
-n -
PaM r P
·
3 . row
abthus
eoen- memst
anos 'L r ao
nq
e s
e
-n -nr
a r--a
nrqd r awe 9'"nv
r •. unu <

-n
w eL { c}
·
.{, I.C ...
-.

mysq1> ALTER TABLE t_range DROP PARTITION pO;


Query OK , 0 rows affected ( 0.01 sec)
Records: 0 Dup1icates: 0 Warnings: 0

mysq1> SELECT • FROM t_range;


Empty set (0.00 sec)

mys q >>R E-
caAN
TTTAEML PU
pu' --eu (

-> b INT)ENGINEaINNODB
-> PARTITION BY LIST (b) (
-> PARTITION pO VALUES IN (1 , 3 , 5 , 7 , 9) ,
-> PARTITION p1 VALUES IN (0 , 2 , 4 , 6 , 8)
) ;

Query OK , 0 rows affected (0.00 sec)

mysq1> INSERT INTO t_1 ist SELECT 1 , NULL;


ERROR 1526 (HYOOO): Table has no partition for value NULL

mysql> CREATE TABLE t list(


-> a I NT ,
- > b INT)ENGINE= INNODB
-> PARTITION BY LIST(b) (
-> PARTITION pO VALUES IN (1 , 3 , 5 , 7 , 9 , NULL) ,
->PARTITION pl VALUES IN (0 , 2 , 4 , 6 , 8)

Query OK , 0 rows affected (0.00 sec)

mysql> INSERT INTO t l i s t SELECT 1 , NULL;


Query OK , 1 row affected (0.00 sec)
Records: 1 Duplicates: 0 Warnings: 0

mysql> SELECT table_name , partition_name , table _ rows


-> FROM information schema.PARTITIONS
-> WH ERE tab l e_schema-DATABASE() AND table_name- 't_list ' \G;
1. row

table name: t list


partition_na.m e: pO
4.8

table rows: 1 ••
********* **** ************* * 2.
btbs
a--aw nanos
eoeLma
-n-n -P·
··e·· s ts
emst ·-<
p-azz
-
..
r w
<,‘ e ( U ec}

mysql> CREATE TABLE t hash(


-> a INT ,
-> b
- > PARTITION BY HASH(b)
-> PARTITIONS 4;
Query OK , 0 rows affected (0.00 sec)

mysql> INSERT INTO t hash SELECT 1 , 0;


Query OK , 1 row affec ted (0.00 sec)
Records: 1 Duplicates: 0 Warnings: 0

mysql> INSERT INTO t_hash SELECT 1 , NULL;


Query OK , 1 row affected (0.01 sec)
Records: 1 Duplicates: 0 Warnings: 0

mysql> SELECT table_ name , parti t ion _ name , table_rows


-> FROM information schema.PARTIT ION S
-> WHERE table_ schema = DATABASE() AND table_name=' t_hash ' \G;
1. row
ano
ahu?L n-z..
ems
ma ho
a s hu
-
nra r --, e·· P
ahu w
2 . row
btb
a--a ems
ma
n-z.. h a s ku
-n -P
par e··-- 1
·
w
3. row

table name: t hash


pa r -L n naw
oe e-- p
ms
-
aH r o : nu
4. row
table name: t hash
pa E -la
t oe a ms
n no e-- P93
-z
b w ·· AU
..ftl.CA
.,'-

4 rows in set (0.00 sec)

4.8.5

* FROM TABLE WHERE

SELECT * FROM TABLE


WHERE
4.8 ;%

mysql> CRE ATE TABLE 'Profile' (


-> ' id' int(11) NOT NULL AUTO INCREMENT ,
-> 'nickname' varchar(20) NOT NULL DEFAULT "
-> ' password ' varchar(32) NOT NULL DEFAULT "
-> ' sex ' char(l) NOT NULL DEFAULT "
-> ' rdate ' date NOT NULL DEFAULT ' 0000-00 - 00 ' ,
-> PRIMARY KEY ('id') ,
-> KEY ' nickname ' ('nickname')
-> )
-> PARTITION BY HASH (id)
-> PARTITIONS 10;
Query OK , 0 rows affected (1.29 sec)

mysql> SELECT COUNT (nickname) FROM Profile;


1. row
count(1): 9999248
1 row in set (1 min 24.62 sec)

mysql> SELECT tabl e_name , partition_name , table_ rows


-> FROM information schema.PARTITIONS
-> WH ERE table_schema=DATABASE() AND
1. row
btb
a--a
+EM-- ems
ano
eoemaw
n-r
-
zp9.•e
11
--1
·- nrAMd
00o
ar -+L
nr n e··
AU<J

2. row
btb
a--a ems
nano
maw f
-e1
rpo
nr'··
··e·· t
li
-n
par
-
..
r QJ

3. row
a--a
btb ems
ano
maw
n-r zp
··e·· 84
e4
-
pa z n ..
7
4. row
tt
hua
.• -
an
n-em
ma··e f-l e
· EP
P

paE -•L
n
._'- -.
table rows: 986937
5. row
t teoe
ta-La ems
ano
maw
n-E
hu+--hu EP9
··e··
P&Qd'··
-
p aE n ..
ro

6. row *.*********************.**.
t-btb
leoe
ti-
ta--a n-zems
ano
maw zp7
··e·· f
i f
}
' ero
AAM-
-n
nraE ..

7. row ***************************
·Dtb
a--a
eoe ems
nano
maw EP9
··e··
PQ f--f
-n
pau E
-
..
.. z d
t

8. row ***************************
btb
aLa
eoe --e··
ems
nano
maw z .• e9
nrAy
-
par n p7
-
r
*.************************* 9. row
a-La
eoe ems
nano
maw -
.. EPO -
.• e2d
- li
-n
pa E e·
-
r ·
*************.************* 10. row ***************************
btbw
tttz emse
n mawspqd{
EP80
··e··t e8
-
ao
r n
-
..
En
--
L s e c}

mysql> EXPLAIN PARTITIONS Profile WHERE id =l \G;


1. row
id: 1
select_ type: SIMPLE
table: Profile
partiti ons: p l
type: const
possible_keys: PRlMARY
key: PRlMARY
key_ len: 4
....
4.8

ref: const
rows: 1
Extra:
1 row in set (0.00 sec)

mysql> EXPLAIN PARTITIONS


-> SELECT * FROM Profile WHERE nickname = ' david ' \G;
1. row
id: 1
select_type: SIMPLE
table: Profile
partitions: pO , p1 , p2 , p3 , p4 , p5 , p6 , p7 , p8 , p9
type: ref
possible_keys: nickname
key: nickname
key len: 62
ref: const
rows: 1 0
Extra: Using where
1 row in set (0 . 00 sec)

mysql> SELECT * FROM Profìle WHERE nickname- ' david' \G;


1. row
id: 5566
nickname : david
password: 3e35d1025659d07ae28 e006gec51ab92
sex: M
rdate: 2003-09-2 0
1 row in set (1.05 sec)

--
..{,I. CA

4.8.6

TABLE … EXCHANGE

TABLE … EXCHANGE

CREATE TABLE e (
id INT NOT NULL ,
fname VARCHAR(30) ,
lname VARCHAR(30)

PARTITION BY RANGE (id) (


PARTITION pO VALUES LESS THAN (50) ,
PARTITION p1 VALUES LESS THAN (100) ,
PARTITION p2 VALUES LESS THAN (150) ,
PARTITION p3 VALUES LESS THAN (MAXVALUE)

INSERT INTO e VALUES


( 1669 , "Jim" , ,
"Smith")
(337 , "Mary" , "Jones") ,
(16 , "Frank" , "White") ,
(200 5 , " Linda " , "Black");

mysql> CREATE TABLE e2 LIKE e;


Query OK , 0 rows affected (1.34 sec)
e
l
l
4.8 -3

mysql> ALTER TABLE e2 REMOVE PARTITIONING;


6
Query OK , 0 rows affected (0.90 sec)
Records: 0 Duplicates: 0 Wa rning s: 0

mysql> SELECT PARTITION_ NAME , TABLE_ ROWS


- > FROM INFORMATION SCHEMA .PARTITIONS
-> WHERE TABLE NAME - ' e ';
+I ----------------+… ----------+
PARTITION_ NAME I TABLE_ ROWS I
+----- -----------+------ ------+
PPPP

+----…----------+ ------------+
4 rows in set (0.00 sec)

mysql> ALTER TABLE e EXCHANGE PARTITION pO WITH TABLE e2 ;


Query OK , 0 rows affected (0.28 sec)

my sql> SELECT PARTITION NAME , TABLE ROWS


-> FROM INFORMATION SCHEMA.PARTITIONS
- > WHERE TABLE NAME = ' e ';

+---------…------+ ------------+
I PARTITION_ NAME I T ABLE_ ROWS I
+----------------+ ------------+
nrnv

nvn

+----------------+ ------------+
4 rows i n set (0.00 sec)

mysql> SELECT * FROM e2;

+----+-------+-----…+
I id I fname I lname I
+----+-------+----- - -+
I 16 I Frank I Whit e I
+----+-------+----- - -+
1 row in set (0.0 0 sec )
...

4.9
C...

5.1
.{,I. CA
s

5.2

5.2.1

- OU I AY l l AO eJ
,,
CJ Z

a··EE
--
ll
p323- -ll
hyhyll "+-m
5.2

5.2.2

/\
2
/\ \
5 8

6.

1+ 2+3+4+5+6 )
1)
._'V

3
\
3
\
,6
/ \

/ ' "'"

j ' \ j \
/ \
yd

IJL
/ -\
2
,
"
9
C...

5.3

5.3.1
Leaf Index
No No

Index
Yes No

Yes Yes Page


5)

Page
C...
53 189 @

Page

| 25 I I I
28 30
C...
..

5.3.2

No No

Yes No Page
1
Yes Yes Page
5.4 1914b

Fill

5.4

B+
. --.
C...

2 ,..._.
,..._.

index)

5 .4 .1

CREATE TABLE t (
a I NT NOT NULL ,
b VARCHAR(8000) ,
c I NT NOT NULL ,
PRlMARY KEY (a) ,
KEY idx c (c)
)ENGINE= INNODB ;

INSERT INTO t SE LECT 1 , REPEAT(' a ' , 7 000) , -1;


INSERT INTO t SELECT 2 , REPEAT ('a' , 7000) , -2;
INSERT INTO t S ELECT 3 , REPEAT('a' , 7000) , -3;
INSERT INTO t SELECT 4 , REPEAT('a' , 7000) , -4;
_innodb_p age_
!root@n ine youO- 43 dataJ' p y_ innodb_page_info . py -v my test/t . i bd
page offset 00000000 , page t ype <file Space Header>
page offset 000 00 001 , page type <Insert Buffer Bitmap>
page offs et 00000002 , page t ype <file Segment inode>
page offset 0000000 3 , page t ype <B-tree Node> , page lev el <0001>
page offset 0000000 4 , page type <B-t ree Node> , page level <0000>
page offset 00000005 , page type <B-tree Node> , page level <0000>
page offset 00000006 , page type <B-tree Node> , page level <0000>
page o ffs et 00000000 , page typ e <Freshly Allocated page>
Total number of page: 8:
Fres hly Al l ocated page: 1
Insert Buffer Bitmap : 1
f ile Space Header: 1
B-tree Node: 4
f i!e Segment inode : 1

page

OOOOcOOO c2 33 6 2 95 00 00 0 0 03 ff ff ff ff ff ff ff ff I . 3b. . . . . . . . . . . . . I
0000c010 00 00 00 Oa b6 8c c e 57 45 bf 00 00 00 00 00 00 I . . . . . . . WE. • • . • • • I
0000c020 00 00 00 00 00 f9 00 02 00 a2 80 05 00 00 00 00
0000c030 00 9a 00 02 00 02 00 03 00 00 00 00 00 00 00 00
0000c040 00 01 00 00 00 00 00 00 01 e2 00 00 00 f9 00 00
0000c050 00 02 00 f2 00 00 00 f9 00 00 00 02 00 32 01 0 0 I • . . . • . . . . . . . .2. . I
0000c060 02 00 lb 69 6e 66 69 6d 75 6d 00 04 00 Ob 00 00 I . . . infimum . . . . . . I
0000c070 73 75 70 72 65 6d 75 6d 00 1 0 00 1 1 00 Oe 80 00 Isupremum.. ...... I
0000c080 00 01 00 00 00 04 00 00 00 19 00 Oe 80 00 00 02
0000c090 00 00 00 05 00 00 00 2 1 f f d6 80 00 00 04 00 00
OOOOcOaO 00 06 00 00 00 00 00 00 00 00 00 00 00 00 00 00
OOOOcObO 00 00 00 0 0 00 00 00 00 00 00 0 0 00 00 00 00 00
OO OOcOcO 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

OOOOfffO 00 00 00 00 00 70 00 63 73 d8 52 3a b6 8c ce 57 1.... .p. cs.R:.. . WI

6e 66 69 6d 75
6d 0002 00 Header ,
Page

00 00
Ox0001) , 8000

0003

Key: 8000 00 01 Key: 80 00 00 02 II Key: 80


Pointer: 00 04 Pointer: 00 05 I I

00 04 I I
(' ( ‘ a' 4 ,repeat
3,repeat (‘ a'
.tt I. e ....
5.4

mysql>EXPLAIN
-> SELECT * FROM Profile ORDER BY id LIMIT 10 \G;
******************* ******** 1. row ***********.******* ********
id: 1
select_type: SIMPLE
t able: Profile
type: index
possible ke ys : NULL
key: PRlMARY
key_len: 4
ref: NU LL
rows: 10
Extra:
1 row in set (0.00 sec)

query)

mysql> EXPLAIN
-> SELECT * FROM Profile
-> WHERE id>10 AND id<10000\G;
1. row
id: 1
select_type: SIMPLE
table: Profi1e
type: range
possible_keys: PRlMARY
key: PRlMARY
key_len: 4
r ef: NULL
rows: 14868
Extra: Using where
1 row in se t <0.01 sec)
d
., I.C.6

mysql> SELECT COUNT(*) from Profile


-> WHERE id>10 AN D i d< 10000;
.. 1.. row

COU NT(l): 99 46
1 ro w in set (0.00 s ec )

5.4.2

Sccondary Index Secondary Index

Clustcrcd Indcx

5-15
5.4

SQL

Identifiedr , RID)

SQL SQL

(On- Line Analytical

ahead)

It all

mysql> ALTER TABLE t


- > ADD c INT NOT NULL;
Query OK , 4 rows affected (0.24 sec)
Records: 4 Duplicates: 0 Warnings: 0

mysql> UPDATE t SET c = O-a ;


Query OK , 4 rows affected (0.04 sec)
._'- -.
C.A

Rows matched: 4 Changed: 4 Warnings: 0

mysql> ALTER TALBE t ADDKEY idx_ c (c);


Query OK , 4 rows affected (0.28 sec)
Records: 4 Duplicates: 0 Warnings: 0

mysql> SHOW INDEX FROM t\G;


*************************** 1. row
an-lqada--iakuye
binnntlpCNtm
TUY-nlnbpxemu
eumemotrelpn
··eexenytdlet
+E.

N nkiuodsd OP-aA2NNB
-enml·-uec
IA
RUUT
MLRv‘ LE
LL
RS
qe
quecc -l-aa-a-m
o aI

2. row
-umemotzelpn
bqadai--akuvde
t111CA2NNB
Tn-LEaa-am
a--nnntlpcNtm
eeexenytdlet
N qoaz
uy-nlnbipxo
nkiuodsd
-enml·-uec
dXU
C L U
L T
R
E
-LLE
cdecc

2 rows in set <0.00 sec)

mysql> select a , c from t;


+---+----+
I a I c
+---+ ----+
4 -4
3 -3
2 -2
1 -1
+---+----+
4 rows in set (0.00 sec)
.{,I. CA
5 .4 199 - .

_ inno d b _p age

[root@nineyouO-43 mytestJ# py_ innodb_ page_info.py -v t.ibd


page offset 00000000 , page type <File Space Header>
page offset 00000001 , page type <Insert Buffer Bitmap>
page offset 00000002 , page type <File Segment inode>
page offset 00000003 , page t ype <B-t ree Node> , page level <0001>
page offset 00000004 , page type <B- tree Node> , page level <0000>
page offset 00000005 , page type <B- tree Node> , page level <0000>
page offset 00000006 , page type <B-tree Node> , page level <0000>
page offset 00000007 , page type <B-tree Node> , page level <0000>
page offset 00000000 , page type <Freshly Allocated Page>
Total number of page: 9:
Freshly Allocated page: 1
Insert Buffer Bitmap: 1
Fi le Space Header: 1
B-tree Node: 5
Fi l e Segment inode: 1

00010000 b9 aa 8e dO 00 00 00 04 ff ff ff ff ff ff ff ff
00010010 00 00 00 Oa ec ea 4e 27 45 bf 00 00 00 00 00 00 1. . .. . .N ' E....... I
00010020 00 00 00 00 01 02 00 02 00 ac 80 06 00 00 00 00
00010030 a4 00 01 00 03 00 04 00 00 00 00 00 52 d4 8b I • • . . • . . . . . . . . R. . I
00010040 00 00 00 00 00 00 00 00 01 f2 00 00 01 02 00 00
00010050 00 02 02 72 00 00 01 02 00 00 00 02 01 b2 01 00 I. . .r . . . . . . . . . . . . I
00010060 02 00 41 69 6e 66 69 6d 75 6d 00 05 00 Ob 00 00 I . • Ainfimum. . . . . . I
73 75 70 72 65 6d 75 6d 00 00 10 ff f3 7f ff ff Isupremum . .. . .... I
00010080 ff 80 00 00 01 00 00 18 ff f3 7f ff ff fe 80 00
00010090 00 02 00 00 20 ff f3 7f ff ff fd 80 00 00 03 00
000100aO 00 28 ff f3 7f ff ff fc 80 00 00 04 00 00 00 00 1. (..... .. .. .. ... I

00013ffO 00 00 00 00 00 70 00 63 f 3 46 77 f2 ec ea 4e 27 I . . • . . p . c . Fw. . . N I I
11 Key:7f ff ff fe 11 Key:7f ff 11 Key:7ffffffc
11 11 Pointer. 80 00 00 03 11

Key:80 00 00 01 Key:80 00 00 02
Pointer:OO 05 Pointer. OO 06

page Offset:OO 05 06 Page

t,repeat 2,repeat ,-2 ('a' ,-4


3,repeat ('a',7000) ,-3

5.4.3

Pl:
P2:
5.4 20/ - .

LAST-INSERT ‘ UJ
Y
o PAGE_DlRECTION
o PAGE N DlRECTION

• Page
cursor record splil rccord

II II II I 1 :
I I I II I: I
I I I I : I
…I I

I I
.
: I

I I I I I
I I I I : I

record 10 be insert

split

Page Ri gblPage
cursor record

record 10 be insert
-.
..{) I. CA
._'-
Page

cursor record

record

record 10 be insert
After Splil

Page
Righl Page

cursor record

splil record

record 10 be insert

5.4.4

ALTER TABLE tbl name


I ADD {INDEXIKEY) [ inde x name]
[index_type) (index_col_ name , ...) [index_option)

ALTER TABLE tbl name


DROP PRlMARY KEY
I DROP {INDEXIKEY} inde x name

CREATEIDROP
CREATE [UNIQUE] INDEX index name
[index_type]
ON tbl_name (index_col_name , ...)

DROP INDEX index name ON tbl name


mysql >ALTER TABLE t
-> ADD KEY idx b (b(lOO));
Query OK , 4 rows affected (0.32 sec)
Records: 4 Duplicates: 0 Warnings: 0

mysql> ALTER TABLE t


-> ADD KEY idx a c (a , c);
Query OK , 4 rows affected (0.31 sec)
Records: 4 Duplicates: 0 Warnings: 0

mysql>SHOW INDEX FROM t\G;


1. row
TUY
an lqada--iakuye
binnntlpCNtm
.. •L
eumemotze1&P-n
N
nki o
-enml·-uec
......................
oplaA2NNB
qeexenytdlet
az
RTM RU
-
VA L
L UL L R
T E
-
CMeCC -l
-
u nlnbpxo
-
odsd aa

-
a

-
m
2 . row
Table: t
Non_unique: 1
Key_name: idx_b
Seq_in_ index : 1
Column name: b
Collation: A
Cardinality: 2
Sub_p art: 100
Packed: NULL
Null: YES
Index_type: BTREE
Comment:
3. row
Table: t
6.{, I.CA
..'-

N0· uy- qada--iakuye


q
- o --l1aA2NNB
az
nnntlpCNtm
eexenytdlet
n--l-aa-am
umemotzelpn
dX aUcL U
nLR T
b
-
qdecc
-l - ..................
nlnbp
czn

LLE

x
e
4. row
Tn- .......
-um
bqada
eeetlL2C
ainnntlpcNtm
uy
nkiu
-e
d
Xa U U
c L
-
qu ecc - emotzelpn
l-aa x.
nml·-uec
-
nlnbpxo ................
enytdlet
odsd ---La A
2NNB
LLE
-am
kuyeLR

T PM

5. r ow
Tn lumem
bqada
t
a--nnn
eee
uy-n 1·llc
nkiu
-enm
dx c
-i
sec x
e
Collation: A
Cardinality: 2
SUb_part: NULL
Packed: NULL
Null :
Index_type: BTREE
Comment:
5 r o ws in set (0.00 sec)

INDEX

CJ
5.4

•••
o
in_ a
o Column

mysql> analyze table t\G;


1. row
Table: mytest.t
Op: analyze
Msg_type: status
Msg_text: OK
1 row in set (0.01 sec)

mysql> show index from t\G;


1. row
.{,I. CA
.,_'- -.

Table: t
Non_unique: 0
Key_name: PRlMARY
Se q__ in_index: 1
Column name: a
Collation: A
Cardinality: 4
SUb_part: NULL
Packed: NULL
Null:
Index_type: BTREE
Comment:

mysql> show index from Profile\G;


1. row
bin
eum
an-
TUY p&rou
··ee os
eN
I r ae
NO nk
-e ....
a me
Seq_in_index: 1
Column name: username
Collation: A
ca r -L
AUqu a--la
--nr
nb ..
'hr
ytNN
HUU
T
UVU

U ..
Packed: NULL
Null:
Index_type: BTREE
Comment:

2. Fast Index Creation


MySQL
5.4

....

SQL

Index Creation

3. Online Schema Change


Online Schema Change

openarkkit toolkit
o
-.
.{) I. CA
._'-

o
o

sql_bin
4.0nline DDL

DDL
C...
5.4

ALTER TABLE tbl name


I ADD {INDEXIKEYI [index name)
[index_type) (index_col_name ,...) [index_option]
ALGORITHM [=) {DEFAULT I INPLACEICOPYI
LOCK [=) {DEFAULTINONEISHAREDIEXCLUSIVEI

mysql> SELECT @@version\G;


1. row
@@version: 5.6.6-m9
1 row in set (0.00 sec)

mysql> SHOW VARIABLES LIKE 'old alter table'\G;


1. row ***************************
Variable name: old alter table
Value: OFF
1 row in set (0.00 sec)

(1) NONE

(2) SHARE
.{, I.C...

(3) EXCLUSIVE

(4) DEFAULT

online_ alter_log_max_
_online_ alter_log_max_

Error:1799SQLSTATE:HYOOO(ER INNODB ONLINE LOG TOO BIG)


Message: Creating index 'idx_aaa ' required more than 'innodb_ online_alter_log_
max_size' bytes of modification log . Please try again.

5.5

5.5.1
SELECT * FROM student WHERE sex='M'

SELECT * FROM member WHERE usernick c 'David'

mysql>EXPLAIN SELECT * FROM member


-> WHERE usernick= 'David'\G;
1. row
i d: 1
se ec tby -- seo
pee
YID-
e····zmn
nret
Mbs
'LUE
FL
-at
me

possible_keys: usernick
v-e
-Kel e2z n --e k
··nu euro
bA e va
-
ref: const
rows: 1
Extra:
1 row in set (0.00 sec)
C...

5.5.2

lNSERT

modified counter>2 000 000

modified
.{)I.C...
5.5

P2. …. P8 0
(P l+P2+ …+P8)

SHOW I NDEX FROM OrderDetails

• 1 A

2 A E
,
, A 2ol2 l:1:li -
' A 2OJ2
,
,, .....10
'‘
=- 8TRU
-"'11) 8TÆf
'‘

INDEX FROM

: T.. IU
• ' "
2 A 21 S2
: A lZ!lI C22II
1
' cm
8TREf

' A
CIIII

:.. A 168 CIID omu

INDEX FROM
_ stats_sample

_ unequal , nulls_

_
stats
TABLE INDEX

InnoDB

innodb_stats_pe rsistent

OFF
TABLE

innodb stats on metadata

OFF
_stats
innodb_ stats_persistent_sample_p ages
20
sample
innodb_stats_transient_sample_pages
8
.. {, I.e....
M

5.6

5.6.1

Think Differento

5.6.2
.{, I.C ...

CRE Þ. TE TÞ. BLE t (


a INT ,
b INT ,
PRI MÞ.RY KEY (a) ,
KEY idx a b ( a , b)
)ENGINE- I NNOOB

• FROM TABLE WHERE a=xxx and


* FROM TABLE WHERE
• FROM TABLE
WHERE

CREATE TABLE b uy_1og (


userid I NT UNS I GNEO NOT NULL ,
buy_date DATE
)ENG INE- InnoOB;

I NSERT INTO buy_l og VALUES ( 1 , ' 2009-01-01 ' );


INSERT INTO bu y_ log VALUES ( 2 , '2009 - 01 - 01 ' );
6ft
5.6

INSERT INTO buy_log VALUES ( 3, '2009-01-01');


INSERT INTO buy_ log VALUES ( 1, '2009-02-01');
INSERT INTO buy_log VALUES ( 3, '2009-02-01');
INSERT INTO buy_log VALUES ( 1, '2009-03-01' );
INSERT INTO buy_log VALUES ( 1, '2009-04-01');

ALTER TABLE buy_log ADD KEY ( userid );


ALTER TABLE buy_ log ADD KEY ( userid , buy_date );

SELECT * FROM buy_log WHERE userid=2 ;

.
1.

userid , buy_

SELECT* FROM buy_log


WHERE userid=l ORDER BY buy_date DESC LIMIT 3

. ."'....……………….. e.cr. ..
...._...……
.. 1 SINPLE ,.., 4 3

buy_

userid , buy_ date

Þ
C...
218

SELECT ••• FROM TABLE WHERE a=xxx ORDER BY b

b,
SELECT .•. FROM TABLE WHERE a=xxx ORDER BY b
SELECT •.. FROM TABLE WHERE a=xxx AND b=xxx ORDER BY c

SELECT ••• FROM TABLE WHERE a=xxx ORDER BY c

5.6.3

key 1 , primary key2 ,…, key 1, key2

SELECT key2 FROM table WHERE keyl=xxx;


SELECT primary key2 , key2 FROM table WHERE keyl=xxx;
SELECT primary keyl , key2 FROM table WHERE keyl=xxx;
SELECT primary keyl , primary key2. key2 FROM table WHERE keyl =xxx;
C...
219 - -,

SELECT COUNT(*) FROM buy_ log ;

l I id.... t)po rd . ..... bInI


!.
__

COUNT

SELECT COUNT(*) FROM buy_log


WHERE buy_date>= ' 2011-01-01' AND buy_ date<'2011-02-01'

buy_

! """ -

5.6 .4
..\I , CA

SELECT * FROM orderdetails


WHERE orderid>lOOOO and orderid<102000;

INDEX
FROM
..
•. •
2 A 2311 8 TRfE
1 00det!D A 8TREE
a.-O A lZIlII
A 177 lZIlII CDJ
!..j j ... 1 1 f'IociJcIlD A 177 aTREe

• {' 4 1155

table scan)
..{)
5.6

SELECT * FROM orderdetails FORCE INDEX(OrderID I


WHERE orderid>lOOOO and orderid<102000;

I ! íd _keys key
I •

5.6.5

HINT:
o

tbl name ((AS) alias) [index hint list]


index hint list:
index hint (, index hint)
index hint:
USE {INDEXIKEYI
[{FOR (JOINIORDER BYIGROUP BYI) ([index list)l
I IGNORE (INDEXIKEYI
({FOR (JOINIORDER BYIGROUP BYI) (index listl
I FORCE {INDEXIKEYI
( {FOR (JOINIORDER BYIGROUP BY }) (index list)
index list:
index_name ( , index_ name )
C...
222

CREATE TABLE t (
a INT ,
b INT ,
KEY (a) ,
KEY (b)
)ENGINE=INNODB;

INSERT INTO t SELECT 1 , 1;


INSERT INTO t SE LECT 1 , 2 ;
I NSERT INTO t SELECT 2 , 3;
INSERT INTO t SELECT 2 , 4;
I NSERT INTO t SELECT 1 , 2 ;

SELECT * FROM t WHERE a=1 AND b = 2;

LJ.1 SIMPlE I 5.5 ..l:

intersect

SELECT * FROM t USE INDEX(a) WHERE a=1 AND b = 2;

-
t All a 5
.'i

SELECT * FROM t FORCE a;1 AND

" rel rows


SIMPLE I rel ð a 5 3

INOEXo

5.6.6 Multi-Range

Read

ref,
._'- -.
..{)I.CA

SELECT * FROM sa l aries WHERE sa l ary>10000 AND salary<40000;

.......
b j1 sm 4

index

.. koy . .. ...... e.n


W SIMPLE Mm laIc_s idx_s 4 23378 usng

Read 43.213
4.212

Multi-Range

SELECT * FROM t

WHERE key_p artl 1000 AND key_part1 < 2000


AND key part2 10000;
3
5.6

1, key_part2

1000) , (1 001 ,
1000) , (1002 , 1000) ,…, (1 999 ,

SELECT * FROM salaries


WHERE (from date between ' 1 986-01-01' AND ' 1 995-01-01')
AND (salary between 38000 and 40000);

1----.
. .Pt---…--"'-- ‘…

Multi-Range

_
_ cost_
mrr cost

mysql> SET @@optimizer_switch='mrr=on , mrr_cost_based=off';


Query OK , 0 rows affected (0_00 sec)

256K:
--.

mysql> SELECT @@read_rnd_buffer_ size\G:


1. row

@@read rnd buffer size: 262144


1 row in set (0.00 sec)

5.6.7 Index Condition Pushdown

Index Condition
Condition

Condition

Index Condition or
Condition
index

NDB Condition
Pushdown ,
Cluster
Condition

last_name ,
SELECT * FROM people
WHERE zipcode = ' 95054 ,
AND l astname LIKE
AND address LIKE ' %Main

LIKE ' % etrunia % ' AND address LIKE' % Main


Street% Condition
SELECT * FROM salari es
WHE RE (from date be tween ' 1 986-01-01' AND '1 9 9 5 - 0 1-0 1' )
AND (salary between 3 8 0 0 0 and 40000);

SIMPLE idIc_s idIc_s 4 .......

index
Condition

MySQL Condition

MySQL5.5
MySQL 5.6 with ICP
MySQL 5.6 with ICP & MRR

Condition

5.7
5.7.1 ••

1,…,

T [O..m-l

[k] =NULL
h( K ,)

h( K.)

K,
K =h( K s )

K,
K

h(k) e k mod m

5.7.2
.{,I. CA
s

_ buffer_p ool_

space_ id<<20+space

5.7.3

* FROM TABLE
WHERE index col=' xxx ' ENGINE
INNODB
mysql>SHOW ENGINE INNODB STATUS\G;
1. row
Status:
=====================================
090922 11:52:51 INNODB MONITOR OUTPUT
=====================================
Per second averages calculated from the last 15 seconds

------------…----------------------
INSERT BUFFER AND ADAPTIVE HASH INDEX
-------------------------------------
Ibuf: size 2249 , free list len 3346 , seg size 5596 ,
374650 inserts , 51897 merged recs , 14300 merges
Hash table size 4980499 , node heap has 1246 buffer(s)
1640.60 hash searches/s , 3709.46 non-hash searches/s
5.8

SELECT * FROM table WHERE index col= 'xxx'

non-hash searches:non-hash

_adaptive_hash_

5.8

5.8.1

SELECT * FROM blog WHERE content like 'xxx%'

SELECT * FROM blog WHERE content like

Text
5.8.2

file

Documentld Text Documentld


Pease ponidge ho t. pease porridge cold 4 Some like it hot, some like it cold
2 Pease in the pot 5 Some like it in the pot
3 Nine days old 6 Nine days old

file

inverted file
Number Text Documents Number Text Documents
code 1, 4 8 3, 6
2 days 3, 6 9 pease 1, 2
3 hot 1, 4 10 1, 2
4 m 2, 5 11 pot 2, 5
5 It 4, 5 12 some 4, 5
6 like 4. 5 13 the 2, 5
7 nme 3, 6
file inverted
(Documentld ,

full inverted
Number Text Documents Number Text Documents
code (1:6) , (4:8) 8 old (3:3) , (6:3)
2 days (3 :2) , (6:2) 9 pease (1 :1 ,4) , (2: 1)
3 hOI (1 :3) , (4:4) 10 porridge (1:2,5) , (2:2)
4 m (2:3) , (5:4) 11 pot (2:5) , (5:6)
5 11 (4: 3,7) , (5:3) 12 some (4:1 ,5) , (5: 1)
6 Iike (4:2 ,6) , (5:2) 13 the (2:4) , (5:5)
7 mne (3 :1) , (6:1)

full inverted : 6) ,
inverted

5.8.3

inverted index

Auxiliary Table

Auxiliary
Index Cache

FTS Index
2'34 $

Auxiliary ""'Table

Auxiliary lndex Table

Index

Table:
mysq l >SET GLOBAL innodb_ft_aux_table = 'test/fts_a';
Query OK , 0 rows affected (0.00 sec)

FT
INDEX

Index

Index Table
Index

FTS lndex
Index
UNSIGNED NOT

mysql> CREATE TABLE fts_ a(


-> FTS OOC 10 1NT UNS1GNEO AUTO 1NCREMENT NOT NULL ,
--
>> dR
b op XY
T
Vez PMDH
'K
M b v
R

tF qM D}

- > );
ERROR 1166 (42000): 1ncorrect column name 'FTS OOC 10'

DOC
column name

mysql> CREATE TABLE fts_a(


-> FTS OOC 10 B1G1NT UNS1GNEO AUTO 1NCREMENT NOT NULL ,
-> body TEXT ,
-> PR1MARY KEY(FTS OOC 10)
-> );
Query OK , 0 rows affected (0.02 sec)

Cache
Document

Document
.{, I.C...
2'36

OPTIMIZE
_ optimize_ fulltext_

mysql>SET GLOBAL i nnodb_optimize_ full.tex t_only=l;


mysql>OPTIMIZE TABLEfts_a;

ft_num_word_
a:
CREATE T ABLE fts a(
FTS OOC 10 BIG1NT UNSIGNEO AUTO 1NCREMENT NOT NULL ,
body TEXT ,
PRIMARY KEY(FTS OOC 10)
);

INSERT INTO fts a


SELECT NULL , ' Pease porridge in the pot';
INSERT I NTO fts a
SELECT NULL , 'Pease porridge hot , pease porridge cold ' ;
1NSERT INTO fts a
SELECT NULL , ' Ni ne days old';
1NSERT INTO fts a
SELECT NULL , ' Some like i t hot , some like i t col d';
1NSERT 1NTO fts a
SELECT NULL , 'Some like i t in the pot';
1NSERT 1NTO fts a
SELECT NULL , 'Nine days old';
1NSERT 1 NTO fts a
SELECT NULL , ' 1 like code days';

CREATE FULLTEXT 1NOEX idx_fts ON fts_a(body);

mysql> SELECT * FROM fts a;

I FTS_DOC_ IO I body

+----…------+---------------------…-------------------+
5.8

Pease porridge in t he pot


Pease porridge hot , pease porridge cold
Nine days old
4 I Some like it hot , sorne l i ke it cold
5 I Some l ike it in the p ot
6 I Nine days old
7 I 1 like code days

+------------+------------------…------…-----------…-+
7 rows in set (0 . 00 sec)

n
rnysql> SET GLOBAL innodb_ft_au x _table= ' test/fts_ a';
Query OK , 0 r o ws affected (0 . 00 s ec)
rnysql> S ELECT • FROM inforrnation_schema .INNODB_FT_I NDEX_TABLE;

+----------+-------
I WORD
-------+-------……+-----------+---…._---+----------+
I FIRST_DOC_ID I LAST_DOC_ID I DOC_COUNT I DOC_ID I POSITION I

+----------+-------
code
-------+-----…._-----+-----------+---…----+----------+
751552535
cold
cold
days
days
days
hot •

hot
like
like
like
like
n l. ne
n l. ne
••

old
pease
pease ••

pease ••

porridge ••

,
porridge ••

porridge ••

pot
pot
l
sorne
sorne
2'38 5*

4 I 5 I 2 I 5 I ••

DOC

mysql> DELETE FROM test.fts_a WHERE FTS_DOC_IDe7;


Query OK , 1 row affected (0.00 sec)

mysql> INNODB FT DELETED;

+-----…--+
I DOC_ID I
+--------+
7 I
+--------+
1 row in set (0.00 sec)

mysql> SET GLOBAL inn odb_optimize_ful ltext_only-l ;


Query OK , 0 rows affected (0.00 sec )

mysql> OPTIMIZE TABLE test.fts a;

+---------…-+-----
I Table I Op
-----+------…-+… -----…--+
I MS9_ type I Msg_text I
+------------+----- -----+----------+-- --------+
I test .fts_a I optimize I status I OK
+------------+----- -----+----------+-- --------+
1 row in set (0.01 sec)

mysql> SELECT * FROM I NNODB FT DELETED;


+--------+
'1+ nv- VA-
nu- nu"|+

7 I
+…------+
5.8 239 4b

1 row in set (0.00 sec)

mysql> SELECT * FROM INNOOB_FT_BEING_OELETEO;


+--------+
I OOC 10 I
+----- ---+
7 I
+--------+
1 row in set (0.00 sec)

FT BEING

mysql> 1NSERT INTO test.fts_ a SELECT 7 , '1 like this days';


ERROR 1 82 (HYOOO): 1nvalid 1nnoOB FTS Doc 10

_ ft_ server_ stopword_

mysql> CREATE TABLE user_stopword(


-> value VARCHAR(30)
-> ) ENG1NE ; 1NNOOB;
Query OK , 0 rows affected (0.03 sec)

mysql> SET GLOBAL


-> innodb_ft_server_stopword_table = "test/user_stopword";
Query OK , 0 rows affected (0.00 sec)
., I.C...

5.8 .4 ••

MATCH (coll , co12 ,...) AGAINST (expr [ search_ modifier))


search modifier:

IN NATURAL LANGUAGE MODE


I IN NATURAL LANGUAGE MODE WITH QUERY EXPANSION
I IN BOOLEAN MODE
I WITH QUERY EXPANSION

1. Natural Language

mysql> SELECT * FROM fts _ a WHERE body LIKE

mysql> SELECT * FROM fts_ a


-> WHERE MATCH(body)
->AGAINST ('Porridge' IN NATURAL LANGUAGE MODE);
+------------+-----------------------------------------+
I FTS_DOC_ID I body
+------------+-----------------------------------------+
2 I Pease porridge hot , pease porridge cold I
1 I Pease porridge in the pot
+------------+-----------------------------------------+
2 rows in set (0.00 sec)

LANGUAGE

SELECT * FROM fts_a WHERE MATCH(body) AGAINST ('Porridge');


't'.
mysql> EXPLAIN SELECT * FROM fts _ a
-> WHERE MATCH(body) AGAINST ('Porridge')\G;
1. row ********.******************
,..• AUV
··nr
qu e32- ec •,
e quv M‘p yunb

table: fts a
type: fulltext
possible_keys: idx_fts
key: idx fts
key_len: 0
ref: NULL
rows: 1
Extra: Using where
1 row in set (0.00 sec)

mysq1> SELECT * FROM fts b


-> WHERE MATCH(body) AGAINST ('Porridge');
ERROR 1191 (HYOOO): Can't find FULLTEXT index matching the column list

mysql> SELECT count(*)


-> FROM fts a WHERE
->MATCH (body) AGAINST ('Porridge' IN NATURAL LANGUAGE MODE);

I count(FTS_DOC_ID) I
..' -..

+-------------------+
2 I

+----------…------+
1 row in set (0.00 sec)

mysql> SELECT
- > COUNT(IF(MATCH (body)
-> AGAINST ('Porridge' IN NATURAL LANGUAGE MODE) , 1 , NULL))
-> AS count
-> FROM fts a ;
+-------+
I count I
+-------+
2 I
+-------+
1 row in set (0.00 sec)

mysql> SELECT fts_doc_id , body ,


-> MATCH(body) AGAINST ('Porridge' IN NATURAL LANGUAGE MODE)
-> AS Relevance
-> FROM fts a;

+--------…-+-…-------------------------------------+---…--------------+
I fts_doc_id I body I Relevance

+------------+--…------------------------------------+--------------------+
1 I Pease porridge in the pot 0.2960100471973419
2 I Pease porridge hot , pease porridge cold 0.5920200943946838
3 I Nine days old o I
4 I Some 1ike i t hot , some 1 ike i t cold 0
5 I Some 1ike i t in the pot 0
6 I Nine days old 0
7 I I 1ike hot and code days o I

+-…----…-+-----------------------------------------+--------------------+
7 rows in set
(0.01 sec)
‘() 1. t: ..

_ ft_ min_ token_size ,


token

mys q l> SELECT fts_d oc_id AS i d , b o dy ,


- > MATCH( b ody) AGA1NST (' t he' 1N NATURAL LANGUAGE MODE)
-> AS r1
-> FROM ft s a ;

+----+-------------…------…-----…
i d I body
------……+------+
rl

1 I Peas e p orr i dge in the pot I 0 I


2 I Pease p o rridge hot , pease porridge cold I 0 I
3 I Ni ne day s ol d I 0 I
4 I Some l i k e i t hot , s ome like i t cold I 0 I
5 I Some 1 i k e i t in the p o t I 0 I
6 I Nine day s old I 0 I
7 I 1 l ik e hot and c o de da y s I 0 I

+----+---…------------------------------------+…-----+
7 rows in set (0.00 s ec)

_ ft_ min_token_
token token

2. Boolean
BOOLEAN MODE

my s q l> SELEC T * FROM fts a


->WHERE MATCH (bod y) AGA1NST ('+Pease -ho t ' 1N BOOLEAN MODE)\G ;
1 . row
FTS DOC 10: 1
body: Pea se porri dge i n t he p ot
..ttl.CA
.,,'-

(body)
AGAINST ("'Pease pot"@30' IN BOOLEAN

mysql> SELECT * FROM fts_a


-> WHERE MATCH(body) AGA1NST ('+Pease +hot' 1N BOOLEAN MOOE)\G;
1. row
FTS OOC 10 : 2
body: Pease porridge hot , pease porridge cold
1 row in set (0.00 sec)

mysql> SELECT * FROM fts_a


-> WHERE MATCH(body) AGA1NST ('+Pease -hot' 1N BOOLEAN MOOE)\G;
*************************** 1. row
FTS OOC 10: 1
body: Fease porridge in the pot
1 row in set (0.00 sec)

mysql> SELECT * FROM fts_a


- > WHERE MATCH(body) AGA1NST ('Fease hot' 1N BOOLEAN MOOE);

+-…--------+-----------------------------------------+
I FTS_OOC_10 I body
+------------+----------------- - - ----------------------+
2 I Fease porridge hot , pease porridge cold
1 I Pease porridge in the pot
4 I Some like i t hot , some like i t cold
-x-
5.8 zt

7 I 1 like hot and code days


+---- - -------+--- -- -------------- ----------- -----------+
4 rows in set (0.00 sec)

Search:
mysql> SELECT fts _doc_id , body FROM fts_a
-> WHERE MATCH(body)
-> AGAINST ('"Pease pot" @30' IN BOOLEAN MODE)\G;
*************************** 1. row
fts doc id: 1
body: Pease porridge in the pot
1 row in set (0.01 sec)

mysql> SELECT fts_doc_id , body FROM fts_a


-> WHERE MATCH(body)
-> AGAINST ("'Pease pot" @10' I N BOOLEAN MODE);
Empty set (0.01 sec)

mysql> SELECT fts_doc_id , body ,


-> MATCH(body) AGAINST ('like >pot' IN BOOLEAN MODE)
-> AS Re1evance FROM fts a;

+------------+------------…--------------------------+---------------------+
I fts _ doc_ id I body I Relevance
+------------+-----------------------------------------+---------- -----------+
1 I Pease porridge in the pot 1.2960100173950195
2 I Pease porridge hot , pease porridge 'cold
3 I Nine days old 0
4 I Some like it hot , some li ke it cold 0.27081382274627686
5 I Some like it in the pot 1 .431 4169883728027
6 I Nine days old 0
7 I 1 like hot and code days 0.13540691137313843
+------------+-----------------------------------------+------------- --------+
7 rows in set (0.00 sec)

mysql> SELECT fts_doc_id , body ,


-> MATCH(body) AGAINST ('like >hot <some' IN BOOLEAN MODE)
-> AS Re1evance
-> FROM fts a;
246

1 Pease porridge in the pot


2 Pease porridge hot , pease porridge cold 1.2960100173950195
3 Nine days old
Some like it hot , some like it cold 1.158843994140625
4
5 Some like it in the pot
. -0.5685830116271973
6 Nine days old
7 1 like hot and code days 0.13540691137313843
+------------+-----------------------------------------+---------------------+
7 rows in set (0.00 sec)

mysql> fts_a
- > WHERE MATCH(body) AGAINST ('po*' IN BOOLEAN MODE);
+------------+-----------------------------------------+
FTS_DOC_ID I body
+------------+-----------------------------------------+
2 I Pease porridge hot , pease porridge cold
1 I Pease porridge in the pot
5 I Some like i t in the pot

+------------+--------------------…------------------+
3 rows in set (0.00 sec)

poto

mysql> SELECT * FROM fts_a


-> WHERE MATCH(body) AGAINST ('like hot' IN BOOLEAN MODE);

+------------+-----------------------------……-------+
FTS_DOC_ID I body

+-…--------+-----------------------------------------+
I 4 I Some like it hot , some like it cold I
I 7 I 1 like hot and code days I
I 2 I Pease porridge hot , pease porridge cold I
I 5 I Some like it in the pot I

+…---------+-----------------------------------------+
4 rows in set (0.00 sec)

mysql> fts a
-> WHERE MATCH(body) AGAINST ('''1ike hot …IN BOOLEAN MODE);
+---------- - -+----------------- ----------+
FTS_DOC_ID I body
+------------+---------------------------+
247 ?"
#tÆ .lJ..

7 I 1 l i ke hot and code days I


+------------+---------------------------+
1 row in set (0.00 sec)

3. Query Expansion

knowledge

QUERY NATURAL LANGUAGE


MODE WITH QUERY query expansion
relevance

CREATE TABLE articles (


id INT UNSIGNED AUTO 1NCREMENT NOT NULL PRlMARY KEY ,
title VARCHAR(200) ,
body TEXT ,
FULLTEXT ( title , body )
ENG1NE;1nnoOB;

1NSERT 1NTO articles (title , body) VALUES


('MySQL Tutorial' , ' OBMS stands for OataBase •.. '),
('How To Use MySQL Well' , 'Af ter you went through a .. . '),
('Optimiz ing MySQL' , '1n this tutorial we will show ...'),
('1001 MySQL Tricks' , ' 1. Never run mysqld as root. 2. ....),
('MySQL vs. YourSQL' , '1n the following database comparison .•. '),
('MySQL Security' , 'When configured properly , MySQL ...'),
( 'Tuning OB2' , 'For IBM databôse .. .'),
('1BM History' , 'OB2 hitory for IBM •.. ');
.,
._'- -.
I. CA

mysql> SELECT * FROM articles


-> WHERE MATCH(title , body)
-> AGAINST('database' IN NATURAL LANGUAGE MODE);
+----+-- ----------------- +---------------- ------------------- ----- --+
id I title body
+----+--------- ------ ----+------------- ---------------------- -------+
1 I MySQL Tutorial DBMS stands for DataBase
5 I MySQL vs. YourSQL In the following database comparison
7 I Tuning DB2 For IBM database

3 rows in set (0.00 sec)

mysql> SELECT * FROM articles


-> WHERE MATCH(title , body)
-> AGAINST('database' WITH QUERY EXPANSION);
+----+---------- ------- -- ----+------------------------------------------+
id I title body
+----+-----------------------+------------------------------------------+
lIll---
5 I MySQL vs. YourSQL In the following database comparison
1 I MySQL Tutorial DBMS stands for DataBase
7 I Tuning DB2 For IBM database
8 I IBM History DB2 hitory for IBM
3 I Optimizing MySQL In this tutorial we will show
-
6 I MySQL Security When configured properly , MySQL
2 I How To Use MySQL Well After you went through a
4 I 1001 MySQL Tricks 1. Never run mysqld as root. 2.

+----+-----------------------+--------------------------------…-------+
8 rows in set (0.00 sec)

Expansiono

5.9
C...

6.1
2'50

SQL

SQL SQL Server

Microsoft SQL

SQL

6.2
6ft
6.2

lock latch

Lock

I!Iysq1.> SHOW

I Type I I Status

I lnn008 I I os_waits=5 I
I lnnoDB I I I

2 (8.83 sec)
INNODB

11UTEX;

I Type I"_ I Status

_.
I Inn008
I InnoOB I
I
3 set (8.81 sec)
I count=S4.
os_yi e\d S2t.
I
I

ENGINE INNODB

count
spin lock
5pm_waUs

spin_rounds

os watls

os_yields _thread
05 wall IJmcs
2'52 $ •

LOCK

6.3

6.3.1

s
x
s

granularity
6.3

IS IX S x
15
IX
S
x
C...
..

ENGINE INNOOB
mysql> SHOW ENGINE INNOOB STATUS\G;

TRANSACTIONS

Trx id counter 48B89BF


purge done for trx's n:o < 48B89BA undo n:o < 0
History list length 0
LIST OF TRANSACTIONS FOR EACH SESSION:
---TRANSACTION 0 , not started , process no 13757 , OS thread id 1255176512
MySQL thread id 42 , query id 80424887 loca1host root
show engine innodb status
---TRANSACTION 48B89BE , ACTIVE 193 sec , process no 13757 , OS thread id
1254910272 starting index read
mysql tables in use 1 , locked 1
LOCK WAIT 2 lock struct(s) , heap size 368 , 1 row lock(s)
MySQL thread id 41 , query id 80424886 localhost root Sending data
aelect * from t where a < 4 lock in ahare mode
------- TRX BAS BEEN NAITING 2 SEC FOR TBIS TO BE GRAHTED:
RECORD LOCKS apace id 30 page no 3 n bita 72 index 'PRlMARY' of table
'teat' . ' t ' trx id 48B89BE lock mode S waiting

TABLE LOCK table 'test'.'t' trx id 48B89BE lock mode IS


RECORO LOCKS space id 30 page no 3 n bi ts 72 index 'PR1MARY' of table
'test'. ' t ' trx id 48B89BE lock mode S wa iting
---TRANSACTION 48B89BO , ACTIVE 205 sec , process no 13757 , OS thread id
1257838912
2 lock struct(s) , heap size 368 , 1 row lock(s)
MySQL thread id 40 , query id 80424881 localhost root
TABLE LOCK table 'test'. ' t ' trx id 48B89BO lock mode 1X
RECORO LOCKS space id 30 page no 3 n bi ts 72 index 'PRIMARY' of table
'test'. ' t ' trx id 48B89BO lock_mode X locks rec but not gap

END OF INNOOB MONITOR OUTPUT


=== =========================

1 row in set (0.01 sec )

t where a<4 lock io share RECORD


LOCKS space id 30 page 00 3 0 bits 72 index I PRIMARY I of table I test I • I tI 48B89BO
lock mode X locks rec but oot rec but oot
6.3

SH

JNNODB

trx id
trx statc
startcd

lrx_rcquestcd_lock_id

trx wait startcd

trx_mysql_thread_id SHOW PROCESSLlST


trx_query

mysql> SELECT • FROM information_s chema.INNODB_TRX\G;


… * * * * * * * * * * * * * … * * . * . * * * 1. row

trx id : 7311F4
trx state: LOCK WAIT
trx started: 2010-01-04 10:49:33
trx_requested_l ock_ id : 7311F4 :96:3 : 2
trx wait started: 2010-01 -04 10:49:33
trx_ weight: 2
trx_mysql_ thread_ id: 471719
trx query: select • f rom parent lock in share mode
2. row
trx id: 730FEE
trx state : RUNNING
trx started: 2010-01-04 10:18:37
tr x_requested_lock_id : NULL
trx started: NULL
trx_we ight: 2
trx_ mysql _ thrcad_i d: 47 1718
trx_query: NULL
2 rows in set (0.00 sec)
parent lock in
share

lock id
lock trx id
lock mode
lock_type
lock table
lock index
lock_space id
lock_page
lock
lock data

mysql> SELECT * FROM informat i on_ schema.INNODB_ LOCKS\G;


1. row
KE--Eoybdaara
ctkkdidp 4FReM
-lCYR
13Emp6
Flot-
··deeexeeca
··4DSA
q 23 a·
-xmtanp-P
..........
-
-tis
l p z en?LW
--
ecqet
.
-kck .......
-
k
-
d ••

*************************** 2. row
lock id: 730FEE:96:3:2
lock trx id: 730FEE
kk- dp
ooccc0100 XR··9321
oybdaaran
eeexeecas
mtanpp
..........•...
--
ot-
CYRO
Emp6{
ReMO
DSAO
l
tis-}k- - v
a r e-
·
p ns
c-}
· · e
ecqet
--
K
I
cks
-
di ··e
6.3

trx

lock

LOCK

blocking_trx_id
requesting_lock_id blocking_trx_ id

mysql> SELECT* FROM information schema.INNODB LOCK WAITS\G;


* 1.
requesting_trx_id: 7311F4
requested_lock_ id: 7311F4:96:3:2
blocking_trx_id: 730FEE
b1ock ing_lock_ id: 730FEE:96:3:2
1 row in set (0.00 sec)
--.

mysql> SELECT
r.trx_id waiting_trx_id ,
r.trx_mysql_thread_id waiting_thread ,
r.trx_query waiti ng_query ,
b.trx_id blocking_ trx_ id ,
b.trx_ mysql_thread_id blocking_thread ,
b.trx_query blocking_query
FROM information schema .innodb lock waits w
INNER JOIN information schema.innodb trx b
ON b.trx_ id = w.blocking_ trx_ id
INNER JOIN information schema.innodb trx r
ON r.trx_id = w.requesting_trx_id\G;
1. row ***************************
wwl-L-l
·Lnn-lnn-l-1
ttLKKew
aawoo--aztt-tt-s
xzuxrut
zhqrhqe
99nqqnn
-Lt·--LK
- -ee
--TU--LS
2
27·L17Le
·laz--az{
ddyddy0
1
-q--9
aceoo
bbl F
-
l ee lc
b
nu

6.3.2

nonlocking

SQL
Query

XLocked Snapshot Data 2


6.3

Version
Concurrency Control.
READ

, Session A
mysql> BEGIN;
Query OK , 0 rows affe cted (0.00 sec)

mysql> SELECT* FROM parent WHERE id = 1;


+----+
I id I
+----+
I 1 I
+…--+
1 row i n set (0.00 sec )
.{,I. CA

mysql> BEGIN;
Query OK , 0 rows affected (0.00 sec)

mysql> UPDATE parent SET id=3 WHERE id=l;


Query OK , 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0

FROM parent WHERE id=

mysql> SELECT * FROM parent WHERE id = 1;


+----+
I id I
+----+
I 1 I
+----+
1 row in set (0.00 sec)

It Session B
mysq1> commit;
Query OK , 0 rows affected (0.01 sec)

* FROM parent WHERE

mysql> SELECT @@tx isolation\G;


1. row ***************************
@@tx isolation: READ-COMMITTED
1 row in set (0.00 sec)

mysql> SELECT * FROM parent WHERE id = 1;


Empty set (0.00 sec)
. 6. ftl.CA
6.3 261

mysq l> SELECT @@tx_isolation \G;


1 . row
REPEATABLE-READ
1 row in set (0.00 sec)

my sql> SELECT .. FROM p arent WHERE i d = 1;


+----+
I id I
+----+
I 1 I
+----+
1 r ow in set (0. 0 0 sec )

BEGIN
SELECT • FROM
2
WHEREid= 1;
3 BEGIN
SET id=3
4
WHERE id - 1;
SELECT • FROM parent
5
WHEREid= 1;
6 COMMIT;
SELECT • FROM parent
7
WHEREid= 1;
8 COMMIT

6.3.3
o SELECT"'FOR UPDATE
LOCK IN SHARE MODE
SELECT'" FOR

LOCK IN SHARE

UPDATE , SELECT'"
LOCK IN SHARE

START

6.3.4

SELECT MAX(au t o inc col) FROM t FOR UPDATE;

AUTO-INC
.{)I.C...
63

SELECT ,
insert-Iike

simple
simple insert.s simple DUPLl CATE KEY

bulk
bulk inserts
REPLACE...SELECT, LOAD DATA
mixed.mode
mixed-mode inserts INSERT INTO tl (cl ,c2) VALUES (S,'c').
fNSERT … ON DUPLl CATE KEY

I
o I

bulk inserts

5imple inserts

2
C...
,_'

mysql> CREATE TA.BLE t (


-> a I NT AUTO INCREMENT ,
- > B INT ,
-> I<EY(b , a)
->
ERROR 1075 (42000): Incorrect tab l e definition; there can be on l y one auto
column and i t must be defined as a key

mysql > CREATE TABLE t (


-> a INT AUTO INCREMENT ,
-> B INT ,
- > I<EY(b , a)
->
Query 0 1<, 0 rows affected (0.0 1 sec)

6.3.5

BEGIN
2 DELETE FROM parent WHERE id=3;
3 BEGIN
lNSERT INTO child SELECT 2 ,3
4
(waiting)
'!JJI

rnysql> SELECT* FROM inforrnation scherna.INNODB LOCKS\G;


1 . row
oct
kzd·-
Lm··d 83 QJEO..
QUEM
··OO
-x
EAO C
-
kk
lloooo ..........
dplececa
oybdaqdet
eeexe--··
cckkkkck
mtanpara
qunn··ny

Emp-6
CYRReM
DSA
·v·
••pa r en '
-
---k

343
l
o ..
2 . row
-kKE
o-
ct --di
··i
-
3B
B3AYPO

OC

-- dple
kk---kcks
OOCCCO1&OO
X
X
-
eek-KKCOCM
oybdaazan
eeexeecas
mtanpp-di
••••••••••••
-
REmp6{
o
CYRO
ReMO
D t
S- A -O
-tis
ooo--E ··9343 -
·
'
Y
p·e
en S
a rc }

cqet

--
K ····e

6.4

6.4.1
o Gap
Lock : Gap Lock+Record
Record

Next-Key
11.

)
(10 , 11 )
(11 , 13)
(1 3. 20)
(20 ,

[10 , 11)
[11 , 13)
[13. 20)

13)

13)

Lock ,

DROP TABLE IF EXISTS t;


d4
..

CREATE TABLE t ( a INT PRlMARY KEY );


INSERT INTO t SELECT 1;
INSERT INTO t SELECT 2;
INSERT IN TO t SELECT 5;

BEGJN;
SELECT • FROM t
2
WHERE a =5 FOR UPDATE:
3 BEGIN:
4 JNSERT JNTO t SELECT 4:
COMMIT:
5

6 COMMIT

Next-Key

CREATE TABLE z ( a INT , b INT , PRl MARY KEY(a) , KEY(b) );


INSERT INTO z SELECT 1 , 1;
INSERT I NTO z SELECT 3 , 1 ;
INSERT INTO z SELECT 5 , 3;
I NS ERT INTO z SELECT 7 , 6;
I NSERT INTO z SELECT 10 , 8;

SELECT * FROM z WHERE FOR UPDATE

Lock.
..'i I.CA

SELECT * FROM z WHERE a = 5 LOCK I N SHARE MODE;


INSERT INTO z SELECT 4 , 2;
INSERT I NTO z SELECT 6 , 5;

(3 ,
INSERT INTO z SELECT 8 , 6;
INSERT INTO z SELECT 2 , 0;
INSERT INTO z SELECT 6 , 7;

Gap

Problem
Lock:
COMMITTED
_locks_ unsafe_for_

READ

I NSERT I NTO z SELECT 2 , 2 ;


..'ft I.CA

INSERT INTO z SELECT 2 , 0;

Next-Key Lock

Problem

Next-Key Problem

Phantom'

SELECT * FROM t WHERE a> 2 FOR UPDATE;

SQL

Phantom

SETSESSION
tx isolation='REAO-OMMITTEO';
2 BEGIN;
SELECT • FROM t
WHERE a > 2 FOR UPOATE;
3
………. I. row ...……
a:4
..{,I. CA

4 BEGIN;
5 INSERT INTO t SELECT 4;
6 COMMIT;
SELECT .. FROM t
WHERE a > 2 FOR UPDATE;

7
……….. …......
1. row
a:4
..........2. row •••••••••
a:5

SQL * FROM t WHERE a>2 FOR

READ

SELECT * FROM table WHERE col=xxx LOCK IN SHARE MODE:

If not found any row:


* unique for insert value
INSERT INTO table VALUES (...);

IN SHARE
BEGIN
mysql>SELECT * FROM z
2 WHERE b=4
LOCK IN SHARE MODE:
mysql>SELECT * FROM z
3 WHEREb=4
LOCK IN SHARE MODE;
mysql>INSERT INTO z SELECT 4,4;
4

mysql>INSERT INTO z
SELECT4,4;
5 ERROR 1213 (40001):Dcadlock found when
trying to get lock;try restarting transaction

6 #

6.5

6.5.1
.ttl , CA

Ti me
SET
@@tx_isolation='read-ncommitted';
SET
2
@@tx_isolation='read-ncommitted';
3 BEGIN;
mysql> SELECT • FROM
••••••••• 1.
4
a: 1
1 row in set (0.00 sec)
5 INSERT INTO t SELECT 2;
mysql> SELECT • FROM
row ••••••••••••
••••••••• J.

a: 1
6
……… 2. row
a:2
2 row in set (0.00

COMMITTEDo
REPEATABLE , Microsoft SQL COMMITTED ,
6.5.2

Ti me

2 SET @@tx_isolation='read-committed';
3 BEGIN BEGIN
mysql>SELECT • FROM t ;

4
a: I
I row in set (0.00 sec)
5 INSERT INTO t SELECT 2;
6 COMMIT;
mysql>SELECT • FROM t ;
••••••••• 1. row
a: I
7
row
a: 2
2 row in set (0.00

SQL Server)
Key

6.5.3

3)
4)
C...
6:5

Ti me
BEGIN;
into @cash
2 FROM
WHERE user = pUser FOR UPDATE;
SELECT cash into @cash
FROM account
3
WHERE user = pUser FOR UPDATE;

... . ... ....... ..


UPDATE account
m
WHERE use r=p User
m+1 COMMIT
UPDATE account SET cash=@cash-I
m+2
WHERE user=pUser;
m+3 COMMIT

SELECT... FOR
e
m p?tt l. 6%

6.6

mysql> SET @@innodb_10ck_wait_ timeout=60;


Query OK , 0 rows affected (0.00 sec)

mysql> SET @@innodb rollback on timeout=on;


ERROR 1238 (HYOOO): Variab1e ' innodb_ rollback_ oo_timeout ' is a read only
variable

mysql> BEGIN;
Query OK , 0 rows affected (0.00 sec)

mysql> SELECT * FROM t WHERE a = 1 FORUPDATE;


ERROR 1205 (HYOOO): Lock wa it timeout exceeded; try restarting transaction
mysql> SELECT * FROM t;
+---+
I a I
+---+
I 1 I
I 2 I
141
+---+
3 rows in set (0.00 sec)

mysql> BEGIN;
Query OK , 0 rows affected (0.00 sec)

mysql> SELECT * FROM t WHERE a < 4 FOR UPOATE;


+---+
I a I
+---+
I 1 I
121
+---+
2 rows in set (0.00 sec)

mysql> BEGIN;
Query OK , 0 rows affected (0.00 sec)

mysql> INSERT INTO t SELECT 5;


Query OK , 1 row affected (0.00 sec)
Records: 1 Ouplicates: 0 Warnings: 0

mysql> INSERTINTO t SELECT 3;


ERROR 1205 (H YOOO ): Lock wait timeout exceeded; try restarting transaction

Next-Key

mysql>SELECT * FROM t;
C...
‘'
+---+
I a I
+---+
I 1 I
I 2 I
I 4 I
I 5 I
I 8 I
+--- +
5 r ows in set (0.00 s e c)

6.7

6.7.1

graph
••

Transaction • Lock Lists


Wait Lists rowl

Wait

wait-for
( tI ) -{ t2

wait-for
ø X
wait-fôr graph
••

6.7.2

(1 +2+3+ …+r) / = r/2

PW=nr/2R

=
.ttl , CA

6.7.3

BEGIN;
mysql>SELECT • FROM t
WHERE a = 1 FOR UPDATE;
2 ………1. row ………… BEGIN
a: 1
1 row in set (0.00 sec)
mysql>SELECT • FROM t
WHERE a = 2 FOR UPDATE;
3 ……… 1. row …………
a: 2
1 row in set (0.00 sec)
mysql>SELECT • FROM t WHERE a =2
4 FOR UPDATE;

mysql>SELECT • FROM t WHERE a = 1


FOR UPDATE;
5
ERROR 1213 (40001): Deadlock found when
to get lock; try restarting transaction

InnoOB
G

mysql> CREATE TABLE P


-> aINT ,
-> PRlMARY KEY(a)
->
Query OK , 0 rows affected (0.00 sec)

mysql> CREATE TABLE c (


-> bINT ,
-> FOREIGH KEY(b) REFERENCES p( a)
-> )ENGINE=InnoDB;
Query OK , 0 rows affected (0.00 sec)

mysql> SHOW I NDEX FROM C\G;


1 . row

Table: c
Non_unique: 1
Key_name: b
Seq_ in_ index: 1
Column name: b
Collation: A
Cardinality: 0
Sub part: NULL
Packed: NULL
Null: YES
Index_type: BTREE
Comment:
1 row in set (0.00 sec )

mysql> DROP INDEX b ON c ;


ERROR 1553 (HYOOO): Cannot drop index 'b': needed in a foreign key constraint

CREATE TABLE t (
a I NT PRlMARY KEY
)ENG I NE =I nnoDB;

INSERT INTO t VALUES (1) I (2) I (4) , ( 5);

BEGIN;
2 BEGIN;
SELECT .. FROM 1
3
WHERE a = 4 FOR UPOATE;
SELECT .. FROM t
4 WHERE a <= 4 LOCK IN SHARE MOOE;

INSERT INTO t VALUES(3);


5 -- ERROR 1213 (40001): Oeadlock found whcn
trying 10 get lock; Iry restarting Iransaclion
6

6.8

Microsoft SQL
284

6.9

SQL
..ftl.CA
.,'-

7.1

7.1.1
NDB

5)
C

I control

7.1.2

Flat Transactions)
-.
.{) I. CA
._'-
Transactions with Savepoints)
Chained Transactions)
Transactions)
Transactions)

BEGINWORK BEGINWORK BEGINWORK


Operalion 1 Operalion 1 Operation 1
Operation 2 Operalion 2 Operation 2

Operalion K
COMMlTWORK ROLLBACK WORK

BEGINWORK
SI
7.1 289 .

Transactions with

WORK :
WORK :

WORK :
..{,I. CA

BEGIN WORK
I

Action
} ,
Action

SAVEWORK: 2

Action

Action

SAVEWORK: 5

Action

SAVEWORK: 6

Action

Action

SAVEWORK: 7

ROLLBACK WORK: 7

Action

Action

SAVEWORK: 8

Action

COMMITWORK

volatile) ,
••
T
T3

A A A C

Nested
level
C

. BEGINWORK
BEGIN WORK

"-,-j -1 BEGIN WORK BEGINWORK


, ,
,,

, ,,
,,
BEGINWORK COM MIT WORK l 'i COMMIT WORK
-:
COMMITWORK

BEGINWORK
,,
, ,,
' /1 BEGIN WORK
,,,

COMMITWORK COMMIT WORK


COMMITWORK
6

( predecessor

Tk
81
811

812

8121

83

831

Commit

time
7. 1 293

_.
7.2

7.2.1 redo

log log fi le) ,

Log at

IW redo logo redo


undo
_
7.2

_flush _ log_ at_trx _

2.

CREATE TABLE test load (


a INT ,
b CHAR (80)
) ENGINE= INNOOB;

DELI MITER /1
PROCEOURE p _ load(count INT UNSIGNED)
BEGIN
DECLARE s INT UNSIGNED DEFAULT 1;
DECLARE c CHA R(80 ) DEFAULT REPEAT('a' , 80);
WHILE s 00
I NSERT I NTO test load SELECT NULL , c ;
COMMIT;
SET s = 5+1;
END WHILE;
END;
/1
DELIMITER ;
p_ load

mysql> CALL p_load(500000) ;


Query OK , 0 cows affected (1 min 53 . 11 sec)

mysql > SHOW VARI ABLES LIKE ' innodb_ fJ ush_lo9_at_t rx_commit '\G
1 . row
Variable_name : innodb_flu s h_l o9_at_trx_commit
Va lue : 0
1 row in set (0 . 00 sec)

mysql > CALL p_load(500000) ;


Query OK, 0 rows affected (13 .90 sec)

_ flush_log_at_trx_

10g_ at_ trx_

innodb_ f1 ush_lo 9_at_trx_commit I


Y-15
-
7
2
e@
7.2

binlog

TI T4 T3 1'2 T8 T6 T7 T5

rcod log

TI T2 TI *T2 T3 T1 I *T3 I *TI

2. 109 block

log
3
&
block
block

RcdoLog

LOG_B LOCK_HDR_NO

LOG_B LOCK_HDR_DATA_LEN

LOG_BLOCK_FIRSLREC_GROUP
Log Block
512 Bytes
LOG_BLOCK_CHECKPOINT_NO

LOG_B LOCK_TRL_N O

block
block
log block

-2 109 block header

LOG BLOCK HDR NO


LOG BLOCK HDR DATA LEN
LOG BLOCK FIRST REC GROUP
LOG CHECKPOINT NO

log
z2 299 %

LOG

LOG BLOCK FIRST REC

Tl 270 bytes

1'2 100 bytes LOG_BLOCKYIRST_REC_GROUR


TI 492 bytes

Free

log block

REC

log block

l09 block

LOG BLOCK TRL NO 4


log group

log group

log

log log
log
log
log log
log
log file ,

redo 109

10g fi1e header 512


chcckpoint 1 512
g
L 512
checkpoint2 512

log
7.2

log log
log

log

LogGroup 1

Redo Log File2

Lo g Group 2
Redo Log Filel

Redo Log File2

log log

filer

| I | I
..ftl.CA
.,'-

log

MLOG_REC_INSERT
Icn& i oriein i mis_malc I
cxtra info inlo_bils! ! I rcc body

MLOG_REC_DELETE

|Iype I space Ipagc_no I

5. LSN
Sequence

PAGE
7.2

ENGINE INNODB
mysq1> SHOW ENGINE INNODB STATUS\G;

LOG

Log sequence number 11 3047174608


Log flushed up to 11 3047 174608
Last checkpoint at 11 3047174608
o pending 10g writes , 0 pending chkp writes
142 10g i/o's done , 0.00 10g i/o's/second

1 row in set (0.00 sec)

Log sequence Log ftushed up

Last checkpoint

Log sequence ftushed up

mysql> show engine innodb status\G;

LOG

Log sequence number 203318213447


Log ßushed up to 203318213326
Last checkpoint at 203252831194
1 pending 10g writes , 0 pending chkp wr i tes
103447 10g i/o's done , 7.00 10g i/o's/second

1 row in set (0.00 sec)

sequence ftushed up checkpoint


10000 ---
redo 10g

| I I
Buffcr Pool

checkpoint LSN:

CREATE TABLE t ( a INT , b INT , PRIMARY KEY(a) , KEY(b));

INSERT INTO t SELECT 1 , 2;

page(2 , 3) , offset 32 , value 1 , 2


offset 64 , value 2

f(f(x)) =f(x)
..{,I. CA

7.2.2 undo

segment) 0

(root@xen-se r ve r - )1 py tho n py_ in nodb_ page_ i nfO.py / usr /local/mysq l /data/


ibdatal
Total number of page: 46208:
I nse rt Buffer Free List: 13093
Insert Buffer Bitmap: 3
System Page : 5
T ran saction system page: 1
Freshly Allocated Page: 4579
undo Log page: 2222
File Segment inode: 6
B-tree Node: 26296
File Space Header: 1
2
..'i I.CA

DELETE ,

undo

2.

log log
segment
PAGE TYPE

_ undo_directory

_ undo_ tablespaces
_ undo_
rollback
7.2

_undo

_rollback_

_undo_

i!l ys pl> SHOW VARIABLES LIKE

I Variable name I Va l ue I

| -L
-LL-E
||
nnn-w
AUAU
000-s
----noa
bbb-iddd
uuu-
000-tce
dit-{
l Eqdb
nnn- es-
op-v lll+
ra
a
00

AU Ce s-
+3 - - ---- ---- - +
se o Oo s e e}

mysql> SHOW VARIABLES LI KE ' datadir ';

+--------------+-----------…._-----------------+
I var i ab1e namel Va 1ue
+- - - - - ---- -----+--------- - ------- --------------+
I datadir I /users/david/mysq1_data/dat a/ I
+--------------+-------------------------------+
1 row in set (0.00 sec)

mysq1> system ls -lh/Users/david/mysq1_ da ta/data/undO*


- rw-rw…- 1 david staff 10M 11 22 16:55/Users/david/mysq1_data/data/undo001
-rw-rw---- 1 david staff 10M 11 22 16:51/Users/davi d/mysql_data/data/undo002
1 david staff 10M 11 22 16:51/Users/david/mysql_ da t a /data/undo003

segment

log
.{, I.C ...

log ,

mysql> SHOW ENGINE INNODB STATUS\G;


1. row

TRANSACTIONS

Trx id counter 3000


purge done for trx's n:o< 2C03 undo n:o < 0
History 1ist 1ength 12
LIST OF TRANSACTIONS FOR EACH SESSION:
---TRANSACTION 0 , not started'
MySQL thread id 1 , OS thread handle Ox1500f1000 , query id 4 localhost root
show engine innodb status

History list
History Iist

3.

undo log
o update undo log
insert undo

undo
z2 3 0.19

undo

undo
undo table

update undo

undo

update undo log record


next
type_cmpl
.undo no
.table id
info bits
.DATA TRX ID
.DATA_ROLL]TR
.lenl
n_unique_index .len2
insen undo log record ... ... ..... .
ncxt .lcnN i colN
: - n_updatc_ficld
type_cmpl update vcctor ---
: .posl : .Ienl :
.undono 1"" - - - - -1"" - - - - _1...……---.
: .pos2 : *len2 :
.table id
L.... •- - - - - - - - - - --:
: .posN : .lcnN ! u_old_co lN:
:
.len2 col2 - - - - -r -
: *len col1
n_uniquc_index y… -- -1"" - - - -
I
_1.. - - --- - _
I

…... ... ... : -pos :·len j con i

.lenN colN
;·"s;·lcn
start I stan

insert undo update undo


C...
d

update undó undo


uodo
undo

o 13 delete
o 14 TRX UNDO DEL MARK
_ update_

SQL

mysql> DESC I NNODB_ TRX_ ROLLBACK_ SEGMENT ;

+--------------------+---------------------+
I Field I Type
--…--+…--+……----+-------+
I Nul l I Key I Default I Extra I

segment _ id b i g i nt(21) unsigned NO


space bigint(2 1 ) unsigned NO
page_ no bigint(21) unsigned NO
last_page_ no bigint(21) unsigned YES NULL
last offset bigint(21) u ns i gned NO
las t t rx no varchar (18) NO
update undo list bigint(21) unsigned NO
update_ undo_cached b i gint(2 1 ) unsigned NO
insert undo list bigint(21) unsigned NO
insert undo cached bigint(21) unsigned NO

10 rows in set (0.00 sec)

INNOOB TRX ROLLBACK


7. 2 ...

mysql > SELECT


- > FROM INNODB TRX ROLLBACK SEGMENT;
+--------- - --+-------+---------+
I segment_id I space I page_no I

+------------+-…----+---------+ nunU

<
U

128 rows in set (0.00 sec)

TRX
CREATE TABLE t (
a INT ,
b VARCHAR(32) ,
PRlMARY KEY (a) ,
KEY(b)
)ENGINE- InnoDB;

mysql> TBEGIN;
Query OK , 0 rows af fe cted (0.00 sec)

mysql> I NSERT INTO t SELECT 1 , '1';


Query OK , 1 rowaffected (0.00 sec)
Records: 1 Duplicates: 0 Warnings: 0

mysql> SELECT * FROM information schema.INNODB TRX UNDO\G;


1. row
ts -L ddn
xq
rez U .... <

ro -L
-
und
-
ec
-
..
n
u dhMo
lt--ae'US
r Lspqf
ryzce···
pee
!-- ··
·u
·D -
N
{ Ns e
qMFuRT REC TA
-w
'··nu

·
not
-
e ea
se
t nu C
C...
3 12

INSERT insert undo 10go

0 1 1c Ob 00 16 04 80 00 00 01 01 10

01 1c
Ob II undo
00 II undo
16
04
80 00 00 01
01 10 # undo 272= Ox0110

mysql> SELECT segment_ id , insert_undo_ list , insert_undo_cached


- > FROM information schema.1NNOOB TRX ROL LBACK SEGMENT
-> WHERE segment_ id=2\G;
1. row
sr en
qdmu
eE to
nd
-
-l·d·-
i
-Lns e
- - s
i nsert undo cached: 0
1 row in set (O.OO sec)

undo

mysql> COMM1 T ;
Query OK , 0 rows affected (0.00 sec)

mysql> SELECT segment_id , insert_undo_list , insert_ undo_cached


-> FROM information schema . INNOOB TRX ROLLBACK SEGMENT
- > WHERE segment_ id=2\G;
1. row

segment_id: 2
insert undo list : 0
inser t undo cached: 1
1 row in set (0.00 sec )

mysq1> BEGIN;
Query OK , 0 rows affected (0 . 00 sec)

mysq1> DELETE FROM t WHERE a = l ;


Query OK , 1 row affected (0.00 sec)
Records: 1 Duplicates: 0 Warnings: 0

mysql> SELECT * FROM information_schema.INNODB_ TRX_UNDO\G;


1. row
ts ddn
x l-
Eer 32
l- U ....<

roqd
-c
und e
-
undo_rec_ type: TRX_UNDO_DEL_ MARK_REC
size: 37
space: 0
page_no: 326
offset: 620
1 row in set (0 . 00 sec)

0518260 00 00 00 00 00 00 00 00 00 00 00 00 02 91 Oe 00
0518270 16 00 00 00 00 30 01 eO 82 00 00 01 4e 01 10 04
0518280 80 00 00 01 00 Ob 00 04 80 00 00 01 03 01 31 02
0518290 6c 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

02 91
Oe
# undo no
16 id
00 # info bits
00 00 00 30 01 eO #
82 00 00 01 4e 01 10
..ttl.CA
--.

04
80 00 00 01
00 Ob
00
04
80 00 00 01
03 -
01
31 1
02 6c

mysql> SELECT segment_id , update_undo_ list , update_undo_ cached


-> FROM information schema.INNODB TRX ROLLBACK SEGMENT
-> WHERE segment_id= 2\G;
1. row
qmu-ds
nd
st--L
eeun toe·l d-Lh·
--seo
n4····
-
dtw
ae
pao
udz lco tdoloe
- -a(
p
u1 n
-
t s c}

mysql> COMMIT;
Query OK , 0 rows affected (0.00 sec)

mysql > SELECT segment_id , update_undo_list , update_undo_cached


-> FROM informat i on schema.INNODB TRX ROLLBACK SEGMENT
-> WHERE segment_id=2\G;
row ***************************
st q-n
eeun toe
enoe
nd
mu-ds dih-
--seo
-lco
-
dtw
ae
pao
udr tdo ole
-a(
p
ul ••
- -
i t s c}

mysql >INSERT INTO t SELECT 1 , '1';


.() I. c...
7.2

mysq1> BEGI N;
Query OK , 0 r ows affected (O.OO sec )

mysq1> UPOATE t SET b='2' WH ERE a=l;


Query OK , 1 row a f fected (O.OO sec)
Rows matched: 1 Changed : 1 Wa rni ngs: 0

mysq1> SELECT * FROM information schema.INNOOB TRX UNOO\G;


1 . row
ts-
r xqd ....
ddn<U RJ
-LL-

er
zo
-c
u nd e ..
undo_rec_ type: TRX_ UNOO_ UPO_ EXI ST_REC
size : 4 1
space : 0
page_ no: 3 18
offset : 724
1 row in set (0.00 sec)

0 4 f82dO 00 00 00 00 02 fd Oc 00 16 00 00 00 00 32 04
04f82eO 8 4 0 0 00 0 1 4 8 01 10 0 4 80 00 0 0 01 01 03 01 31
04f82fO 0 0 Ob 0 0 04 80 00 00 01 03 01 3 1 0 2 d4 00 00 00

02 fd
Oc
undo
16 tab1e id
00 # i n fo bits
00 00 00 32 04 e O trx id
84 00 00 01 48 01 10 :#
04
80 00 00 01
01 #: upda t e
03 :# update
01 :# update
31 1'
00 Ob
00
04
80 00 00 01
03
31 ••
02 d4 # undo

mysq1 > ROLLBACK;


Quer y OK , 1 row a f fe cted (0.00 sec)

my sq1> UPDATE t SET a= 2 WHERE a= l ;


Rows matched: 1 Changed: 1 Wa r nings: 0

mysq 1>. SELECT * FROM information s chema . I NNODB TRX UNDO


- > ORDER BY und o r ec no \ G;
1. row
ts-
zer
xqe i--...
ddnnu F
ro
" .
u nd c
- ..
und o_rec_ type : TRX_UNDO_DEL_MARK_REC
size: 37
space: 0
page_ no: 3 2 4
492
2. r ow
tSEcs . 31
x ddoe--·
- ..
zee-spe nUF qfi
fn-
-E .
r qdet
u nd-
orp-s
-
...1
u nd. o eao yz. T2370 V oo
HVN
A ?·.M Fa
n
H S
u c
ERT RVMC
-
--a
c ·
nes
··
-
s ··t
2 z ow o e }

_ UNDO
log 0 undo_
rec

4‘
6ft

7.2.3 purge

DELETE FROM t WHERE a=l;

history

logo
..ttl.CA

History List

trx7

undo pagcl undo pagc2


undo

list

_p urge_ batch_

undo

history

de la y = (( length(history_list) - i nnodb_max_purge_lag) *10)-5


7.2 319 .,

_ max_purge_lag_delay

innodb_ max_purge_lag_

7.2.4 group commit

group

1
.'i I.CA

2)
3)

_
innodb_fiush_log_at_trx _
B na ZLOob
Session Server Enginc
COMMIT
prepare

… ….

wnle

commlt

I OK

......... .. ......... ......... I


7-18

_ commit_

_ commit_
TI T2 T3 Binary Log [nnoDB
Prcparc
Prepare

Prcparc

Write@
Writc@200
Write @300

| I Commit I
|comlnit|

Commit

TI T2 T3 Binary Log InnoDB


Prcparc

Write@
Commit

Preoarc
WrÌlc@300

…_--- - -- --… ----…- - …On-line Backup Taken …… . - -……………………. -


Commit

Prcpare
Writc

Commit

Facebook
6 "".-

_commit_
Log Group Co mmit

MySQL 5.6

MySQL5.6

o
_commit_

group

group
‘, I.C ....

7.3 ••

o STARTTRANSACTION I
o

identifier :

SAVEPOINT identifier

TO[SAVEPOINT]

TO

TRANSACTION
READ
REPEATABLE
3'2 4 _'"

START

COMMIT AND

mysql> CREATE TABLE t ( a INT , PRlMARY KEY (a))ENGINE= INNODB;


Query OK , 0 rows affected (0.00 sec)

mysql> SELECT @@autocomm i t\G ;


1 . ro w
@@autocommit : 1
1 ro w i n set (0. 00 sec)

mysql> SET @@comp l etion_t y pe=1 ;


Que ry OK , 0 rows affected ( 0.00 sec)

mysql> BEGIN;
Query OK , 0 rows affected (0 . 00 sec)

mysql> INSERT INTO t SELECT 1 ;


Query OK , 1 row affected ( 0 . 00 sec)
Records: 1 Dupl icates: 0 Warnings : 0

mys q l> COMMIT l 'lORK ;


Que ry OK , 0 rows affected (0.01 sec)

mysql> INSERT INTO t SELECT 2;


Query OK , 1 row affected (0.00 sec)
Records : 1 Duplicates: 0 Warnings: 0

mysql> INSERT INTO t SELECT 2;


ERROR 1062 ( 23000 ) : Duplicate e ntry '2' for k ey ' PRI MARY '

mysql> ROLLBACK;
I. Cø _
7.3

Query OK , 0 rows affected (0.00 sec)

mysql>SELECT * FROM t\G;


*************************** 1. row ***************************
a: 1
1 row in set (0.00 sec)

_
TRANSACTION)

_type
COMMIT INTO t SELECT

COMMIT AND

mysql> SET @@completion_type=2;


Query OK , 0 rows affected (0.00 sec)

mysql> BEGIN;
Query OK , 0 rows affected (0.00 sec)

mysql> INSERT INTO t SELECT 3;


Query OK , 1 row affected (0.00 sec)
Records: 1 Duplicates: 0 Warnings: 0

mysql> COMMIT WORK;


Query OK , 0 rows affected (0.01 sec)

mysql> SELECT @@version\G;


ERROR 2006 (BYOOO): MySQL server has gone away
No connection. Trying to reconnect...
Connection id: 54
Current database: test

1. row
aevr
eo
EH --
n ··s5t
s on ,.• ·
RJnu
a"1· os
34
-nu qde
-l e <
U c}

_ COMMIT WORK
2006
has gone

TO

mysql> BEGIN;
Query OK , 0 rows affected (0.00 sec)

mysql > ROLLBACK TO SAVEPOINT t l ;


ERROR 1305 (42 000): SAVEPOINT t1 does not exist

mysql> CREATE TABLE t (a INT , PRIMARY KEY(a))ENGINE=INNODB;


Query OK , 0 rows affected (0.00 sec)

mysql > BEGIN;


Query OK , 0 rows affected (0.00 sec)

mysql> INSERT INTO t SELECT 1;


Query OK , 1 row affected (0.00 sec)
Records: 1 Duplicates: 0 Warnings: 0

mysql> INSERT INTO t SELECT 1;


ERROR 1062 (23000): Duplicate entry ' 1 ' for key 'PRlMARY'

mysq1> SELECT * FROM t\G;


1. row
a: 1
1 row in set (0.00 sec)
TO
TO

mysql> CREATE TABLE t ( a INT , PRlMARY KEY (a))ENGINE-INNODB;


Query OK , 0 rows affected (0.00 sec)

mysql> BEGIN;
Query OK , 0 rows affected (0.00 sec)

mysql> INSERT INTO t SELECT 1;


Query OK , 1 row affected (0.00 sec )
Records: 1 Duplicates: 0 Warnings: 0

mysql> SAVEPOINT t1;


Query OK , 0 rows affected (0.00 sec)

mysql> INSERT INTO t SELECT 2;


Query OK , 1 row affected (0.00 sec)
Records: 1 Duplicates: 0 Warnings: 0

mysql> SAVEPOINT t2;


Query OK , 0 rows affected (0.00 sec)

mysql> RELEASE SAVEPOINT t1;


Query OK , 0 rows affected (0 . 00 sec)

mysql> INSERT INTO t SELECT 2;


ERROR 1062 (23000): Duplicate entry ' 2' for key 'PRlMARY '

mysql> ROLLBACK TO SAVEPOINT t2 ;


Query OK , 0 rows affected (0.00 sec)

mysql> SELECT * FROM t;

I a I
+---+
I 1 I
I 2 I
+---+
2 rows in set (0.00 sec)
.". -..
mysql> ROLLBACK;
Query OK , 0 rows affected (0.00 sec)

ym
mE qt
sp > se
ly L rur v Re
*nu
mA· OCt .,
M}

s ‘
nu S

TO SAVEPOINT
ROLLBACK TO SAVEPOINT

7 .4

ALTER DATABASE...UPGRADE DATA DIRECTORY NAME ,


ALTER EVEN T. ALTER PROCEDURE , ALTER TABLE , ALTER VIEW ,
CREATE DATABASE , CREATE EVENT , CREATE INDEX , CREATE
PROCEDURE , CREATE TABLE , CREATE TRIGGER , CREATE VIEW ,
DROP DATABASE , DROP EVENT , DROP INDEX , DROP PROCEDURE ,
DROP TABLE , DROP TRIGGER , DROP VIEW , RENAME TABLE ,
TRUNCATE
CREATE

ANALYZE

Microsoft SQL
SQL

TRUNCATE TABLE
.tt l. e ....

mysql> SELECT * FRM t\G;


1. row ***
a: 1
******************* **** * *** row
a: 2
2 rows in set (0.00 sec)

mysql> BEGIN;
Query OK , 0 rows affected (0.01 sec)

mysql> TRUNCATE TABLE t;


Query OK , 0 rows affected (0.00 sec)

mysql> ROLLBACK;
Query OK , 0 rows affected (0.00 sec)

mysql> SELECT * FROM t;


Empty set (0 . 00 sec)

7.5

Per Second , Q Trans action

Per Second ,

commi t+com rollback)

autocomrnit= 1)

mysql> SHOW GLOBAL STATUS LIKE 'com commit'\G;


1. row
Variable name: Com commit
Value : 5
1 row in set (0.00 sec)

mysql> INSERT INTO t SELECT 3;


Query OK , 1 row affected (0.00 sec )
Records : 1 Duplicates : 0 Warnings : 0
.{)I.CA

mysql> SELECT * FROM t\G;


1. row

a: 1
2. row

a: 2
3. row
a: 3
3 rows in s et (0.00 sec)

mysql> SHOW GLOBAL STATUS LIKE 'com_commit '\G;


..************************* 1. row
Variable name: Com commit
Value: 5
1 row in set (0.00 sec)

7.6

UNCOMMITTED
o READ COMMITTED
o REPEATABLE READ
o SERIALIZABLE
331 .

READ
READ 0 REPEATABLE

SQL Server

SET [GLOBAL I SESSION) TRANSACTION ISOLATION LEVEL

READ UNCOMMITTED
I READ COMMITTED
I REPEATABLE READ
I SERIALIZABLE

[mysqld)
transaction-isolation = READ-COMMITTED

mysql>SELECT @@tx isolation\G;


rst-e%
14 row
••
@@t x isolation: REPEATABLE-REAO
1 row in set (0.0 1 sec)

mysql>SELECT @@global.tx_isolati on\G;


1. row

@@global.t x _isolation : REPEATABLE-REAO


1 row in set (0.00 sec)

IN SHARE

Iock ,
READ

mysql> CREATE TABLE a (


-> b INT , PRlMARY KEY(b)
-> )ENGINE=INNOOB;
Query OK , 0 rows affected (0.01 sec)

mysql>SET @@tx isolation= ' REAO-COMMITTEO';


Query OK , 0 rows affected (0.00 sec)

mysql> SELECT @@tx isolation\G;


1. row

@@tx isolation: REPEATABLE-REAO


1 row in set (0.00 sec)

mysql> BEGIN;
.{, I.e_
7.6 ...

Query OK , 0 rows affected (0.00 sec)

mysql>INSERT INTO a SELECT 1 ;


ERROR 1598 (HYOOO): Binary logging not possibl e. Message: Transaction level
' REAO-COMMITTEO ' in InnoOB is not safe for binlog mode

_locks_ unsafe_ for_

mysql> SELCT @@version\G


1. row
VEzs -Ll
eO n··qu - -AU -
'A 9e
w e <
U c}

mysql> SHOW VARIABLES LIKE ' innodb_ l ocks_unsafe_ for_binlog'\G;


1. row ***************************
Variable_name : innodb_ locks_unsafe_for_binlog
Value: ON
1 row in set (0.00 sec)

mysql> SET @@t x_isola tion : ' REAO-COMMITTEO';


Query OK , 0 rows affected (0 . 00 sec)

mysq1> BEGIN;
Query OK , 0 rows affected (0 .00 sec)

mysql> INSERT INTO a SELECT 1;


Query OK , 0 rows affected (0 . 00 sec)

mysql > COMMIT;


Query OK , 0 rows affected (0.00 sec)

mysql> SELECT " FROM ' a\G;


1. row
b: 1
2. row
b: 2
3. row
b: 4
4. row
b: 5
4 rows i n set (0.00 sec)
, Sessio n A on mas t er
my sq1 > BEGIN;
Que ry OK , 0 row s a f fec t ed (0 . 0 0 se c )

mysq 1 > DELETE FROM a WHERE b <=5;


Query OK , 4 rows a ffected (0 . 01 s ec)

t Session B on master
mysq1> BEGIN;
Query OK , 0 row s affected ( 0 . 00 sec)

mysql> I NSERT I NTO a SEL ECT 3 ;


Query OK , 0 rows affected (0 . 01 sec )

mysq1> COMMIT;
Quer y OK , 0 rows affected (0 . 00 s ec)

* Se s s i on A on maste r
mys q 1 > COMMIT;
Query OK , 0 rows affected (0.0 0 sec)

mysql> SELECT * FROM a\G;


1.
b: 3

, S1 ave
mys ql > a;
Emp ty set (0 . 00 sec )
7.7

7.7.1

t Bank@Shanghai:
UPDATE account SET money = money - 10000 WHERE user='David';

• Bank@Beijing
UPDATE account SET money = money + 1 0000 WHERE user='Mar i ah';
.{,I. CA
d

(Transaction

lNSERT.UPDTE.
DELETE.
SELECT

Two-Phase Commit

XA (START IBEGI NI xid [JOIN IRESUME )


7. 7

XA END xid [SUSPENO (FOR MIGRATE}} 'f'..


XA PREPARE xid

XA COMMIT xid [ONE PHASE}

XA ROLLBACK xid

XA RECOVER

mysql> XA START 'a';


Query OK , 0 rows affected (0.00 sec)

mysql> INSERT INTO z SELECT 11;


Query OK, 1 row affected (0.00 sec)
Records : 1 Duplicates: 0 Warnings: 0

mysql> XA ENO ' a ' ;


Query OK , 0 rows affected (0.00 sec)

mysql> XA PREPARE 'a';


Query OK , 0 rows affected (0.05 sec)

mysq1> XA RECOVER\G;
1. row
formatID: 1
gtrid_length: 1
bqual_length: 0
data: a
1 row in set (0.00 sec )

mysql> XA COMMIT 'a';


Query OK , 0 rows affected (0.05 sec)

(Java Transaction
..{) I. CA
d

import java.sql.Connection;
import javax.sql.XAConnection;
import javax.transaction.xa.*;
import com.mysql.jdbc.jdbc2.optional.MysqlXADataSource;
import java.sql.*;

claaa MyXid implementa Xid

public int formatld;


public byte gtrid[];
public byte bqual[];

public MyXid() (

public MyXid(int format ld , byte gtrid[] , byt. bqual[])

thia.formatld = formatld;
this.g trid = gtrid;
thia. bqual = bqual;

public int getFormatld()

r.turn formatld;

public byt. [] getBranchQualifier ()

return bqual;

public byte[ ] getGlobalTransactionld()

return gtrid;

public claaa xa demo {

public atatic MysqlXADataSource GetDataSource(


String connString ,
String user ,
.() I. c...
7.7

String passwd) (
try {
MysqlXADataSource ds : ne. MysqlXADataSource();
ds.setUrl(connString);
dS.setUser(user);
ds.setPassword(passwd);
r.turn ds;

catch(Exception e) (
System.out.println(e.toString());
return null;

public atatic void main(String[] args) {


String connString1 : "jdbc:mysql://192.168. 24.43:3306/bank_ shanghai";
String connString2: "jdbc:mysql://192 .168.2 4.1 66:3306/ bank
beijing";
try (
MysqlXADa taSource dsl =
GetDataSource(connString1 , "peter" ," 12345");
MysqlXADataSource ds2 =
GetDataSource(connString2 , "david" , "1234S");

XAConnection xaConn1 = ds1.getXAConnection();


XAResource xaRes1 = xaConn1.getXAResource();
Connection conn1 : xaConn1.getConnection();
Statement stmt1 = conn1.createStatement();

XAConnection xaConn2 = ds2.getXAConnection();


XAResource xaRes2 = xaConn2.getXAResource();
Connection conn2 = xaCon n2. g etConnection{);
Statement stmt2 = conn2.createStatement{);

xid xid1 = n.. MyXid(


100 ,
n.. byt. [] (Ox01) ,
n.. byt. (] IOx02});
Xid xid2 = n.. MyXid(
100 ,
n.. byte []IOx11 } ,
n.. byte f] (O x12 });
try {
xaRes1.start(xidl , XAResource.TMNOFLAGS);
..{,I. CA
340

stmtl.execute("
UPDATE account SET money :TWJ
WHERE user='david'"

xaResl.end(xidl , XAResource.TMSUCCESS);

xaRes2.start(xid2 , XAResource.TMNOFL AGS);


stmt2.execute("
UPDATE account SET money = money+10000
WHERE user= 'mariah …
xaRes2.end(xid2 , XAResource.TMSUCCESS);

int ret2 = xaRes2.prepare(xid2);


int retl = xaRes 1 .prepare(xidl);

i f ( retl == XAResource.XA OK
&& ret2 == XAResourc e .XA_ OK ) {
xaRe sl.commi t(xidl , falsa);
xaRes2.commit(xid2 , false);

lcatch(Exception e) {
e.printStackT race();

catch (Exception e) {
System.Out.println(e.toStri ng());

mysql> SHOW VARIABLES LIKE 'innodb_support_xa'\G;


1. row
Variable_name: innodb_support_xa
Value: ON
1 row in set (0.01 sec)

7.7.2
-,.

••

innodb sJave

slave

7.8

7.8.1
CREATE PROCEDURE load1(count INT UNSIGNED)
••
BEGIN
DECLARE 5 INT UNSIGNEO OEFAULT 1;
DECLARE c CHAR(80) OEFAULT REPEAT('a' , 80);
WHILE 5 <= count 00
INSERT INTO t1 SELECT NULL , c;
COMMIT;
SET 5 = 5+1;
ENO WHILE;
END;

CREATE PROCEOURE load2(count INT UNSIGNED)


BEGIN
OECLARE 5 INT UNSIGNEO OEFAULT 1;
DECLARE c CHAR(80) DEFAULT REPEAT( ' a' , 80);
WHILE 5 <= count 00
INSERT INTO tl SELECT NULL , C;
SET 5 = 5+1;
END WHILE;
ENO;

CREATE PROCEOURE load3(count INT UNSIGNED)


BEGIN
OECLARE 5 INT UNSIGNEO OEFAULT 1;
OECLARE c CHAR(80) OEFAULT REPEAT('a' , 80);
START TRANSACTION;
WHILE 5 <= count 00
INSERT INTO t1 SELECT NULL , c;
SET 5 = 5+1;
ENO WHILE;
COMMIT;
ENO;
mysql> CALL loadl(10000);
Query OK , 0 rows affected (1 min 3.15 sec)

mysql> TABLE tl;


Query OK , 0 rows affected (0.05 sec)

mysql> CALL load2(10000);


Query OK , 1 row affected (1 min 1 .69 sec)

mysql> TRUNCATE TABLE tl;


Query OK , 0 rows affected (0.05 sec)

mysql> CALL load3(10000);


Query OK , 0 rows affected (0.63 sec)

mysql> BEGIN;
Query OK , 0 rows a ffected (0.00 s e c)

mysql> CALL load2(10000);


Query OK , 1 row affected (0.56 sec)

mysql> COMMIT;
Query OK , 0 rows affected (0.03 sec)

Too

7.8.2
C...
..

mysql> SET autocommi t s O;

Query OK , 0 rows affected (0.00 sec)

TRANSACTION ,
_

AUTOCOMMIT=
C
Python

7.8.3

CREATE PROCEDURE sp_ auto_rol l back_demo ()


BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION ROLLBACK;
START TRANSACTION;
I NSERT INTO b SELECT 1;
INSERT I NTO b SELECT 2;
INSERT INTO b SELECT 1;
INSERT INTO b SELECT 3;
COMMIT;
END;
.{,I. CA
_

mysql>SHOW CREATE TABLE b\G;

*************************** 1. row
Table: b
Create Table: CREATE TABLE 'b '
'a' int(ll) NOT NULL DEFAULT '0' ,
PRlMARY KEY ('a')
ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.00 sec)

mysql>CALL sp_ auto_rollback_demo;

Query OK , 0 rows affected (0.06 sec )

mysql>SELECT * FROM b;
Empty set (0.00 sec)

_auto_rollback_
_auto_rollback_

CREATE PROCEDURE sp_auto_ rollback_demo ()


BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION BEGIN ROLLBACK; SELECT -1; END;
STA.RT TRANSACTION;
INSERT INTO b SELECT 1 ;
INSERT INTO b SELECT 2;
INSERT INTO b SELECT 1;
INSERT INTO b SELECT 3;
COMMIT;
SELECT 1;
END;

mysql>CALL sp_auto_rollbaCk_demo ()\G;


*************************** 1. row
-te

-1: - 1 't'.. ••
1 row in set (0.04 sec)

mysq1>SELECT * FROM b;
Empty set (0.00 sec)

SQL
Microsoft SQL XABORT
SQL
Microsoft SQL

_rollback_ _ auto_rollback_

CREATE PROCEDURE sp_rollback_demo ()


BEGIN
INSERT INTO b SELECT 1;
INSERT INTO b SELECT 2;
INSERT INTO b SELECT 1;
I NSERT I NTO b SELECT 3;
END;

_auto_rollback_ _rollback_

t! /usr/bin/env python

' encodi ng=utf-8

impor t MySQLdb
‘() 1. t: ..
M

try :
conn&
MySQLdb . connect (host-"192. 168 . 8 .7" , user="root" , passwd=" xx" , db="test")

cur - conn . cu rs or( )


cur . e xecute ("SET autocommit=O")
c u r . e xecu te("CALL sp_rollback_demo")
cur. execute (" COMMIT")
except Ex cep t ion , e :
cur.exec ut e ("ROLLBACK")
p rint e

[root@n i neyouO-4 3 ‘ ). python test_ demo.py


starti ng rollbac k
( 1062 , " Duplicate e n t ry '1' fo r key 'PRIMARY' ")

l' for key


'PRIMARY '

7.9

UPDATE account
SET account total - account total + (1 + interest rate)
348

void Computeln terest (doub1e interest rate) (

10ng 1ast_accou n t _done , max_acc ount_ no , 10g_size;


int batch size = 100000;

EXEC SQL SELECT COUNT(*) INTO 10g_ size FROM batchconte x t;

if ( SQLCODE != 0 II 10g_s ize == 0 ) (


EXEC SQL DROP TABLE IF EXISTS batchcont ext;
EXEC SQL CREATE TABLE ba tchcontext ( last account done BIGINT );

las t account done = 0 ;


INSERT I NTO batchconte xt SELECT 0;

e1se
EXEC SQL SELECT 1ast ac count
INTO las t account done
FROM batchcont ext;

EXEC SQL SELECT COUNT(*) INTO max account no


FROM ac count LOCK I N SHARE MODE;

WHILE 1ast_accou nt_no < max_ account_no ) {


EXEC SQL START TRANSACTIO N;
EXEC SQL UPDATE account
SET account_to ta1 - account_to ta1 • ( l+interest _rate );
WHERE account no
BETWEEN 1ast account no
AN D 1ast_acco unt_ no + batch_si ze;
EXEC SQL UPDATE batchconte x t
SET 1ast_accou nt_done = 1ast_accou nt_done + batch_size ;
EXEC SQL COMMIT WORK;
1ast_accou nt_done = 1ast_accou nt_done + batch_ size;
account

7.10

READ
C...

8.1

o Hot Backup
o Cold Backup
o Warm Backup
Hot
Backup 0 Cold

Backup Warm
8.1 351

SELECT*INTO

replication
slave/standby
( Database Admin

8.2
o

8.3

8.3.1 mysqldump

SQL

shell>mysqldump (arguments) >flle name

s hel l>mysqldump --all-databases >dump.sql

shell>mysqldump - -da t abases dbl db2 db3 >dump.sql

(root@xen-server - )j mysqldump - - single-t ransaction test >test_ backup.sql


(root@xen - server - l f cattest_backup. sql
MySQL dump 10.13 Distrib 5 . 5 .1-m2 , for unknown-linux-gnu (x 86_64)

localhost Oatabase: t e st

Server version 5.5.1-m2-1og

Table stru cture for table ' a '

DROP TABLE IF EXISTS 'a ' ;


/ * ! 4010 1 SET @saved cs client ; @@character set * /;
/ * !40101 SET character set client = utf8 * /;
CREATE TABLE 'a' (
'b ' int ( 1 1 ) NOT NULL OEFAULT ' 0' ,
PRlMARY KE Y (' b ' )
ENGI NE=InnoDB DEFAULT CHARSET-latinl;
1*!40101 SET character set client = @saved cs client * / ;

Dumping data fo r table 'a '

LOCK TABLES 'a ' WRITE;


/* ! 40000 AL TER T A8LE ' a ' DISABLE KEYS * /;
INSERT I NTO ' a ' VA LUES (1) , (2 ) , (4) , ( 5 );
/*!40000 ALTER TABLE ' a' ENABLE KEYS * / ;
UNLOCK TABLES;

Table structure for table 'z'

DROP TA8LE IF EXISTS ' z';


/ * !4010 1 SET @saved cs client = @@character_ set_ client */;
/ * !40101 SET character_ set_ client =
CREATE TABLE ' z ' (
uzd ?t;2%
'a ' int (ll) DEFAULT NU L L
) ENG I NE=Inn oD8 DEFAULT CHARSET=latin l ;
/ *!4 0 101 SET chara cter_ set_ cl i e n t = @sa ved _ cs_ clie n t * / ;

-- Dumpi n g d a t a for t abl e 'z'

LOCK TA8LES 'z' WRITE;


/ '! 40000 ALTER TA8 LE 'z' DIS A8LE ;
I NSERT INTO 'z' VAL UES (1) , (1) ;
/*! 40 00 0 A LTER TAB L E 'z' ENABL E KEY S * / ;
UNL OCK TABL ES ;

-- Dump comp l ete d on 2010- 0 8- 03 1 3 : 36 : 17

TABLE
..

(-x)

mysq1dump --sing1e-transaction --add-drop-database


--databases test >test_backup.sq1
[root@xen-server -li cat test_backup.sql
-- MySQL dump 10.13 Distrib 5.5.1 -m2 , for unknown- l inux-gnu (x86 64)

-- Current Database: 'test'

/*!40000 Daop DATABASE IF EXISTS 'teat'*/;

CREATE DATABASE /*!32312 IF NOT EXISTS*/ ' test' /*!40100 DEFAULT CHARACTER SET
latinl */;

USE 'test';

[=value]

CHANGE

[root@xen-server -)f mysqldump --sing1e-transaction --add-drop-database


--mas ter -da ta=1 --databases test >test_backup.sql
[root@xen-server -lf cat test_backup.sql
-- MySQL dump 10.13 Distrib 5.5.1-m2 , for unknown-linux-gnu (x86_64)

-- Host: loca l host Database: test

-- ----------…----------------------…----------------
-- Server version m
Rd Ed ''&
8.3

Position to start replication or pOint-in-time recovery from

CHANGE MASTER TO MASTER LOG FlLE='xen-server-bin.000006' , MASTER POS=8095;

(root@xen-server -)1 mysqldump --single-transaction --add-drop-database


--maste r -data=2 --databases test >test_backup.sql
(root@xen-server -)* cat test_backup.sql
MySQL dump 10.13 Distrib 5.5.1-m2 , for unknown-linux-gnu (x86_64)

Host: localhost Database: test

Server version 5.5.1-m2-1og

Position to start replication or point-in-time recovery from

Q …events

(root@xen-server -)' mysqldump --single-transaction --add-drop-database


--master-data=2 --no-autocommit --databases test3 >
(root@xen-server -)' cat test3_backup. sql
MySQL dump 10.13 Distrib 5.5.1-m2 , for unknown-linux-gnu (x86_64)
6. ftl.CA
358

Host: 1oca1host Database: test3


••
Server version 5 .5. 1-m2- 1og

LOCK T ABLES 'a' WRITE ;


/ *!4 0000 ALTER TABLE 'a ' DISABLE
setautocorrunit..O;
I NSERT INTO 'a' VALUES (Ox61000000000000000000);
/ * ! 40000 ALTER TABLE ' a' ENABLE KEYS * /;
UNLOCK TABLES;

1
(-T path)

--fields-enclosed-by=... , --fields-

(root@ x en-server testl' mysqldump -- single -transaction --add-drop-database


--tab- " / u sr/1oca1/mysq1/da ta /test " test
(root@ x en-server testl' 1s - lh
total 244K
-rw-rw---- 1 mysql mysql 8 .4K Jul 21 16:02 a.frm
-rw-rw---- 1 mysql mysq l 96K Ju1 22 17:18 a.ibd
- rw-r--r-- 1 root root 1 .3K Aug 3 15:36 a.sq1
-rw-rw-rw- 1 mysql mysql 8 Aug 3 15:36 a . txt
-rw-r w---- 1 mysql mysql 65 Jul 17 15:54 db.opt
- r w-rw---- 1 mysq1 mysql 8 . 4K Aug 2 17:22 z.frm
-rw-rw---- 1 mysql mysql 96K Aug 2 1 7 :22 z.ibd
-rw-r- - r-- 1 root root 1 .3K Aug 3 15:36 z.sql
- r w- r w-rw- 1 mysq1 my sql 4 Aug 3 1 5:36 Z.txt
-----------
-- Server v ersion 5 .5. 1 - m2-1og

/*!40101 SET @OLD CHARACTER SET CLIENT-@@CHARACTER SET CLIENT */;


/*!40101 SET @OLD CHARACTER SET RESULTS=@@CHARACTER SET RESULTS * /;
/ * !40101 SET @OLD COLLATION CONNECTION-@ @COLLATION CONNECTION * /;
/ * !40101 SET NAMES utf8 * /;
/ *!4 0103 SET @OLD TIME ZONE=@@TIME ZONE * / ;
/ .! 40103 SET TIME ZONE= '+OO:OO ' */ ;
/ * !40101 SET @OLD SQL MODE=@@SQL MODE , SQL MODE='
SET @OLD SQL NOTES=@@SQL NOTES , SQL
8.3

Table structur e for table 'a'

DROP TABLE IF EXISTS 'a';


1 - ! 40101 SET @saved_ cs_client = @@charac ter_ set_ client */;
1 *!401 01 SET charac ter set client = utf8 * 1;
CREATE TABLE 'a' (
' b' int(ll) NOT NULL DEFAULT '0 ' ,
PRIMARY KEY ( ' b' )
DEFAULT CHARSET=latinl;
1*!40101 SET character set client = @saved cs client *1;

1*!40103 SET TIME ZONE=@OLD TIME ZONE *1;

1*!40101 SET SQL MODE=@OLD SQL MODE *1;


1*!40101 SET CHARACTER SET CLIENT= @OLD CHARACTER SET CLIENT *1;
1 *!40 101 SET CHARACTER SET RESULTS=@OLD CHARACTER SET RES ULTS -1;
1 *!4 0101 SET COLLATION CONNECTION=@OLD COLLATION CONNECTION * 1;
SET SQL NOTES=@OLD SQL NOTES * 1 ;

-- Dump completed on 2010-08-03 15:36:56


(root@ x en-server testJ' c at a.txt
1
2
4
5

where_condition' condition' )

(root@x en-server bin)' mysqldump -- single-transact ion --where e 'b>2 ' test a >
a.sql

(roo t @x en - server b in) ' cat a.sql


MySQL dump 10.13 Distrib 5.5.1-m2 , for unknown-linux-gnu ( x 86_ 64 )

Host: l ocalhost Database: test


C...

Server version 5.5.1-m2-1og

Oumping data for table 'a'

-- b>2

LOCK TABLES 'a' WRITE;


/*!40000 ALTER TABLE 'a' OISABLE KEYS */;
INSERT INTO ' a' VALUES (4) , (5);
/*!40000 ALTER TABLE 'a' ENABLE KEYS */;
UNLOCK TABLES;
/*!40103 SET TIME ZONE=@OLO TIME ZONE */;

8.3.2 SELECT.. .l NTO OUTFILE

SELECT. ..INTO

SELECT [column 1) , [column 2)


INTO
OUTFILE 'file name'
[(FIELOS I COLUMNS)
[TERMINATEO BY 'string')
[[OPTIONALLY) ENCLOSEO BY 'char')
[ESCAPEO BY 'chor')

[LINES
[STARTING BY 'st ring ' )
[TERMINATEO BY 'string')

FROM TABLE WHERE

BY [[OPTIONALLY]
ENCLOSED BY 'char' [ESCAPED BY
BY 'string' TERMINATED BY

FIELOS TERMINATEO BY ' \ t ' ENCLOSEO BY " ESCAPEO BY ' \ \ '


LINES TERMINATEO BY '\n' STARTING BY "
mysql> select * into outfile '/root/a.txt' from a;
ERROR 1 (HYOOO): Can' t create/write to fil e '/root/ a .txt' (Errcode: 13)

mysql t est - e "select * into outfile ' /home /mysql /a .txt'


fields te r minated by ',' from a ";
ERROR 1086 (HYOOO) a t line 1: File '/home/mysql/a.txt' alreadyexists

mysql> select * into outfile '/home/mysql/a.txt ' from a;


Query OK , 3 row s affected (0.02 sec)

mysql> quit
Bye
[root@xen-server - ) . c at /home/mysql/a .txt
1 a
2 b
3 c

TERMINATED BY I string
mysql test - e " select * into out file ' / home/mysql/a.txt'
fields termina ted by ',' from a";
[root@xen-serve r ‘ ) , cat /home /mysql / a.txt
1,a
2, b
3, c

TERMINATED

[root@xen-servermy sql). mysql test -e "select * into outfile ' /home /mysql/a .txt '
fields te r minated by ', ' lines termina ted by from a";

[ root@xen-servermysq l) ' od -c a.txt


0000000 1 a \r \n 2 b \r \n 3 c \r \n
0000017
8 .3 .3

(root@xen- server - ] j mysql -uroot - p <test_ backup . sql


En t er password:

mysql> drop database test;


ERROR 1010 (HYOOO) : Error dropping database (can ' t rmdir './test' , errno: 39)

mysql> source Ihome/mysql/ test_backup.s ql;


Query OK , 0 rows affected (0.00 sec)

Query OK , 0 rows affected (0.00 sec)

Query OK , 0 rows affected (0.00 sec)

Query OK , 0 rows affected (0.00 sec)

8 .3 .4 LOAD DATA INFILE

INTO
DATA DATA

LOAD DATA INTO TABLE a IGNORE 1 LINES INFILE '/home/mysq l/a.txt'


8.3

[REPLACE I IGNORE]
I NTO TABLEtbl name
[CHARACTER SET charset_name]
[ (FIELDS I COLUMNS)
[ TERMINATED BY ' string ')
[[ OPTIONALLY] ENCLOSED BY 'char']
[ESCAPED BY ' char' ]

[LINES
[STARTING BY 'string' )
[TERMINATED BY ' string ']

[IGNORE number LINES]


[ (col_name_or_ user_ v ar , …) ]
[SET C01_ name= expr ,...)

DATA
INTO number L1NES
DATA

mysql> load data i nfile '/home/mysql/a.txt ' i nto tabl e a;


Qu ery OK , 3 rows affected (0.00 sec)
Records : 3 Dele t ed: 0 Sk ipped: 0 Warnings: 0

mysql>SET @@foreign_ key_ checksuO ;


Query OK , 0 rows affected (0.00 sec)

mysql>LOAD DATA I NFILE '/home/mysql/a. t x t ' I NTO TABLE a;


Query OK , 4 rows affected (0.00 sec)
Records : 4 Deleted : 0 Sk ipped: 0 Wa rnings: 0

mysql>SET @@foreign_ key_chec k s=l;


Query OK , 0 rows affected (0.00 sec)

mysql>CREATE TABLE b (
->a INT ,
- >b INT ,
->c I NT ,
.{)I.CA
--.

-> PRIMARY KEY(a)


->)ENGINE=InnoDB;
Query OK , 0 rows affected ( 0.0 1 sec)

mysql>LOAD DATA INFILE '/home/mysql/a.txt'


- > INTO TABLE b FIELDS TERMINATED BY ',' (a , b)
-> SET c=a+b;
Query OK , 4 rows affected (0.01 sec)
Records: 4 Deleted: 0 Skipped: 0 Wa rnin gs: 0

mysql>SELECT * FROM b;

I a I b I c
+ ---+------+------+
I 1 I 2 I 3 I
I 2 I 3 I 5 I
I 4 I 5 I 9 I
I 5 I 6 I 11 I
+---+ ------+------+
4 rows in set (0.00 sec)

8.3.5 mysqlimport

DATA DATA

shel l >mysqlimport [options] db_name textfile1 [textfile2 ...]

DATA

[roo t@ xen-ser ve rmysq l ]# mys q limport --use-threads=2 test /home/mysql/t.txt /


home/mysql/s.txt
test.s: Records: 5000000 Deleted: 0 Skipped: 0 Warnings: 0
test.t: Records: 5000000 De1eted: 0 Skipped: 0 Warnings: 0
8.3

mysql>SHOW fULL PROCESSLIST\G;


1. row
Id: 46
Use r : rep
Host: www.dao.com : 1028
db: NULL
Command: Bi n l og Dump
Time : 37651
State: Master has sent al l b i nlog to slave; waiting for binlog to be updated
Info: NULL
*************************** 2. row
Id: 77
User: root
Host: localhost
db: test
Command : Query
Time: 0
State: NULL
Info: show fu l l processlist
3. row
Id: 83
Use r : root
Host: localhost
db: test
Command: Query
Time: 73
State : NULL
1 n f 0 : LOAD DATA INFlLE' / ho_/mysql/ t . txt ' INTO TABLE ' t ' IGNORE 0 LlNES
4. row
I d: 84
User : root
Host: localhost
db: test
Command: Query
Time: 73
State: NULL
Info: LOAD DATA INFlLE' /ho_/mysql/s. txt' INTO TABLE ' s' IGNORE 0 LlNES
4 rows i n set (0.00 sec)

DTA
.{,I. CA

8.4

[mysqld]
log-bin=mysql-bin

d lns
s-co
libb
movdn
yqnn
qb-d
nl = mqp i--
s q -ba
vdo .• n=
X
lu =
rt -A
-

shell>mysqlbinlog [options] log_flle.. .

shell>mysql binlog binlog. 0000001 I mysql-uroot -p test

Shell>mysqlbinlog binlog . mysql -u root -p test

she ll>mysqlbinlog binlog.OOOOOl > /tmp/statements.sql


shell>mysqlbinlog binlog.000002 >> / tmp/ statements.sql
shell>mysql -u root - p -e "source /tmp/statements.sql"
8.5 367

shell>mysqlbin l og- - start-pos ition= 107856 binlog . 0000001 r mysq l -uroot -p test

8.5

8.5.1 ibbackup

SQL
368 . --.
'"

....

8 .5 .2 XtraBackup

xtrabackup--backup I - - prepare [OPT10NS l

(The defaults options should b e giv e n as the first argument)


--print-defaults Print s the program's argument list and exit .
--no-defaults t read the default options from any file.
--defaults-file'" Read the de f ault options from this file.
--de faul ts-extra -file; Read t his fil e after the global options files have been
read.
--target-dir E The destination directory for backups.
--backup Make a backup of a mysql instance.
--stats Calculate the statistic of the datadir (it is recommended
you take mysqld offiine).
--prepare Prepare a backup so you can start mysql server with your
restore.
--export Create files to import to another database after it has been
prepared.
--print-param Print the parameters of mysqld that you will need for a
forcopyback.
--use-memory" This value is used instead of bUffer_p ool_size.
--suspend-at-end Creates a fil e calle d x trabac kup_suspended and waits until
the user deletes that file at the e n d o f the b ackup .
--throttle- (use with --backup) Limits the 10 operations (pairs of reads
and writes) per second to the values set he re .
--log-stream outputs the contents of the to stdou t .
--incremental-lsn'" (use with --backup) Copy only .ibd pages newer than the
specified LSN high: low .
"ATTENT10N": checkpoint lsn used. Be Careful!
--incremental-basedir= (use with --backup) Copy only .ibd pages newer than
the existing b a ckup at the specified directory.
with --prepare) Apply .delta files and logfiles
located in the specified directory.
u 36?@

--tables=name Regular Expression list of table names to


--create-ib-logfile McumENTLY ib
a --prepare.
11111 If you want to create ib_logfile" only re-execute thìs
command using the same optìons. .111
--datadir=name Path to t he database root.
--tmpdir=name Path for temporary files. Several paths rnay be specified
as a colon (:) separated string.
If you specify multiple paths they are used round-robin.

, ./xtrabackup --backup
./xtrabackup Ver alpha-0.2 for 5.0.75 unknown-linux-gnu (x86_ 64)
>>loq 8canned up to (0 1009910580)
Copying ./ibdata1
to /horne/kinoyasu/xtrabackup_work/mysql-5.0.75/innobase/xtrabackup/tmp2/ibdata1
. .done
Copying ./tpcc/stock. i bd
to /home/kinoyasu/xtrabackup_work/rnysql-5.0.75/innobase/xtrabackup/trnp2/tpcc/
stock.ibd
. .done
Copying ./tpcc/new_orders.ibd
to /home/kinoyasu/xtrabackup_work/mysql-5.0.75/innobase/xtrabackup/trnp2/tpcc/
new orders.ibd
. .done
Copying ./tpcc/his tory .ibd
to /home/kinoyasu/xtrabackup_work/mysql-5.0.75/innobase/xtrabackup/tmp2/tpcc/
history.ibd
. . . done
Copying ./tpcc/custorner.ibd
to /horne/kinoyasu/xtrabackup_work/rnysql-5.0. 7 5/innobase/xtrabackup/trnp2/tpccl
customer.ibd
>>log scanned up to (0 1010561109)
. . .done
Copying ./tpcc/district.ibd
to Ihome/kinoyasu/xtrabackup_ work/mysql-5.0 . 75/innobase/xtrabackup/trnp2/tpccl
dìstrict.ibd
. . done
Copying ./tpcc/itern . ìbd
to /horne/k inoyasu/xtrabackup_work/mysql-5.0.75/innobase/xtrabackup/trnp2/tpcc/
item. ibd
dtopec
nc
.,
C p y .ln mHd
r , r deE
J
--nHe
24 -lb d
"9

o r deE line-ibd
>>10g scanned up to (0 1012047066)
. . . done
Copying ./tpcc/orders.ibd
to /home / k i noyasu/xtrabackup_work /mysq1 - 5.0.75/innobase/ x trabac kup/tmp2/tpcc/
orders.ibd
. . .done
Copying ./tpcc/warehouse.ibd
to /home/kinoyasu/ x trabac kup_ work/mysq1-5.0.7 5/innobase/x trabackup/tmp2/ tpcc/
warehouse.ibd
. . . done
>>10g scanne d up to (0 1014 592707)
Stopping 10g copying t hread..
Tr ans a c ti on 109 of lsn (0 1009910580) t o (0 1014592707) was cop i ed .

(0

8.5.3

( full backup)
I ./xtrabackup --backup -- target-dir: /backup/base

(incrementa1 backup)
I . --target-dir=/backup/d e1ta --incrementa1-basedi r : /
backup/base
MASS!;?%

••
(pre pare)
, ./xtrabackup - - prepare --target - dir;/backup/b ase

(app1y incrementa1 backup)


, . /xtrabackup --p repare -- target-dir= /backup/base --incrementa1 - dir= /backupl
de1ta

t ./xtrabackup - -backup
./xtrabackup Ver beta-0.4 for 5.0.75 (x86 64)
>>log scanned up to (0 378161500)

The 1atest check point (for increme nta1): ' 0:377883685 '
>>log scanned up t o (0 379294296)
Stopping 10g copying thread..
Transaction 10g of lsn (0 377883685) to (0 37 9294296) was copied.

(must do - - prepare before t he each incrementa1 backup)


• . /xtrabackup - - pre pare

, ./xtrabackup - - backup --i ncrementa1 =0 :377883685


incrementa1 backup f rom 0:377883 685 is enab1ed.
./xtrabackup Ver beta- 0.4 fo r 5.0.75 unknown-1inux -gnu (x86_64)
>>log scanned up to (0 379708047)
Cop ying ./ibdatal
to Ihome/kinoya su/x trabackup_ work/mysql-5 . 0.75/innobas e/x trabackup/tm p_diffl
ibdatal.de1ta
• . . done

The 1atest check point (for increme nta1): ' 0:379438233 '

>>log scanned up to (0 380663549)


Stopping 10g copying thread. .
Transaction 10g of lsn (0 379 438233) to (0 3806 63549) was copied.
C...

8.6

Volume
Manager ,

Logical Volumcs

Ivcreate

t
vgcreate
û L Volurne Group

pvcreàte Block Devices


8.6
m
-
-
physical disk 0 pIhysical disk I
P-
Idevlhda2 I Idevlhdb

VGO

-
(root@nh124 - 98 -)t vgdisplay
--- Volume group
VG Name rep
System 10
Format lvm2
Me tadata Areas 1
Metadata Sequence No 1873
VG Access read/wr ite
VG Status resizable
MAX LV 0
Cur LV 3
Open LV 1
Max PV 0
Cur PV 1
Act PV 1
VG Size 260.77 GB
PE Size 4.00 MB
To tal PE 66758
Al loc PE / Size 66560 / 260.00 GB
Free PE / Size 198 / 792.00 MB
VG UU10 MQJiye-j4NN-LbZG-F3CQ-UdTU-fo9D-RRfXD5

[root@nh124-98 -)1 lvdisplay


--- Logical volume
LV Name /dev/rep/repdata
VG Name rep
LV UU10 7tOlDt-seKZ-ChpY- QMXC-WaFD-zXAl-MRb ofK
LV Write Access read/write
L V snapshot status source of
/dev/rep/dho_datasnapsho t100 805143507 (active)
/dev/rep/dho_datasnapshot100805163504 [active)
LV Status available
* open 1
LV Size 100.00 GB
Cur z: ent LE 25600
Segments l
Allocation inherit
Read ahead sectors
- currently set 256
Block device 253:0

volume
LV Name /dev/rep/dho_ datasnapshot1 00805143507
VG Name rep
LV UUID fSSXzh-I BnZ-aZln-eP03 -b7pk- CPjN-5xUktE
LV Write Access read only
LV snapshot status active destination for /dev/rep/repd ata
LV Status available
, open
LV Size 100.00 GB
Current LE 25600
COW-table size 80.00 GB
COW-table LE 20480
Al located to snapshot 0.13%
Snapshot chunk size 4.00 KB
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:1

--- Logical volume


LV Name /dev/rep/dho_ datasnapshot1 00805163504
VG Name rep
LV UUID 3B9NP1-qWVG- pfJY-Bdgm-DldD- dUMu-s2L6qJ
LV Wri te Access read only
LV snapshot status active destination fo z: /dev/rep/repd ata
LV Status avai lab l e
, open 0
LV Size 100.00 GB
Cu z:z: ent LE 25600
COW-table size 80.00 GB
COW-table LE 20480
Allocated to snapshot
Snapshot chunk size 4.00 KB
Segments 1
Allocation inherit
Read ahead sectors
- currently set to 256
Block device 253:4

--permlsslon
[root@nhl19-215 data)' lvcreate --size l OOG --snapshot - -permission r -n
datasnapshot Idev/rep/repdata
Logica l vo l ume "datasnapshot" created

Allocated to
lvdisplay

--- Logical
LV Name Ide v /rep/dho_datasnapshotl00805163504
VG Name rep
.,
._'- -.
I. CA

LV UUI D 3B9NP1 - qWVG-pfJY-Bdgm- DldD-dUMu-s2L6qJ


LV Write Access read only
LV snapshot status act i ve destination for /dev/rep/repda t a
LV Status available
, open 0
LV S i ze 100 . 00 GB
Current LE 25600
size 80.00 GB
COW-table LE 20480
Allocated to snapshot 0.04%
Snapshot chunk size 4.00 KB
Segments 1
Al location inherit
duc
acoahed d se
ene
atves
ce so
o rt auto
r
rk - ye
256
l 253:4

8.7

8.7.1

replication

1
I. c.

...... ...

....
··\

mysql>SHOW FULL PROCESSLIST\G;


1 . row

Id : 1
User: system user
Host:
db: NULL
Connect
T i me: 6501
State: Waiting for master to send event
1nfo: NULL
2. row
1d: 2
User: system user
Host:
db: NULL
Command: Connect
T ime: 0
State: Has read all relay log; waiting for the slave 1/0 thread to update it
1nfo: NULL
3. row
1d: 206
User: root
Host: local host
db: NULL
Command: Query
Time: 0
State: NULL
1nfo: SHOW FULL PROCESSL1ST
3 rows in set (0.00 sec)

..

mysql>SHOW FULL PROCESSLIST\G ;

65. row
Id : 26541
User: rep
Host: 192.168.190.9 8 : 39549
db: NU LL
Command: Binlog Dump
Time: 6857
State: Has sent all binlog to slave; waiting for binlog to be updated
1nfo: NULL

SLAVE MASTER
mysq1>SHOW SLAVE STATUS\G;
1. row ********************* * *****
S1ave_IO_State: Waiting for master to send event
Master Host: 192.168.190.10
Master_User: rep
Master Port: 3306
Connect_Retry: 60
Master_ Log_Fi1e: mysq1-bin.000007
Read_Mas ter_Log_Pos : 555176471
Re1ay_ Log_Fi1e: gamedb-re1ay-bin.000048
Re1ay_Log_Pos: 224355889
Re1ay_Master_Log_Fi1e: mysq1-bin.000007
Slave_ IO_ Running: Yes
Slave_SQL_ Running: Yes
Rep1icate_Oo_OB:
Replicate_Ignore_OB:
Rep1icate 00 Tab1e:
Rep1icate_ Ignore_Tab1e :
Rep1icate_ Wi1d_ 00_Table:
Rep1icate_Wi1d_Ignore_Table:
Last Errno: 0
Last Error:
Skip_Counter: 0
ExeC_Master_Log_POS: 555176471
Relay_ Log_Space: 224356045
Until Condition: None
Until_Log_File:
Until_Log_ Pos: 0
Master SSL A110wed: No
Master SSL CA Fi1e:

Master SSL CA Path:


Master SSL Cert:
mc
ao
SMn sre
ess
tad CLd
S-h
LSL
Et- hea
eys
r--t
lp-KM
- - ··e
-e -sn
cue B z -- nu

Master_ SSL_ Verify_server_ Cert: No


Last IO Errno: 0
Last IO Error:
Last SQL Errno: 0
Last SQL Error:
1 row i n set (0.00 sec)

SLAVE
••

SHOW SLAVE

Slave 10 Statc

Mastcr_Log_F ilc
bin.OO0007

Read_ Master_ Log_ Pos

Relay
Relay_Log_F ile
Re1ay_Log_Pos
Slave_ 10_ Running
Slave_SQL_Running
Read_Master_Log_Pos - Exec_
Exec_ Master_Log_ Pos Master_ Log_Pos

MASTER
mysql>SHOW MASTER STATUS\G;
1. r ow
File: mysql-bin . 000007

BLOM
nqqqd-
Position : 606181078
Doe
- -
D-(
oEt nununu
l
nE
B1
o
-i
nse au- · ·
nu qu ec}

_Master_Log_

8 .7.2
381 9

--……---------------…- ---……- -- -
, ‘
', -
•• ••
,., T

....
J

•••••••••

;:/
,
;_-:- 7.... . .- ..... ‘ -
-;. ":'
382

••

(mysqld)
read-only

mysql>INSERT INTO z SELECT 2;


ERROR 1290 (HYOOO): The MySQL server is running with the --read-only option
it cannot e x ecute th is statement

8.8
.{)I.C...
-.

9.1

OLTP
(Online Transaction (Online Analytical
master

read io

9.2

InnoDB Buffer
‘ { , I.CA
9.2 385 - .

sysbench rows ( 18GB data)


‘ UJ
Y
..
A
2250

2000

1750

1500

æ
H
1250
B

RAID10
1000

750
C
500 - ,

250
ii
Buffer pool ( memory },GB

(Transaction Per

mysql>SHOW GLOBAL STAUTS LIKE


1. row
variable_ name: Innodb_buffer_pool_read_ahead
Value: 0
.{) I. CA
--.

2. row
Variable_name: Innodb_buffer_pool_read_ ahead_ evicted
Value: 0
3. row
Variable_name: In nodb_ buffer_pool_read_requests
Value : 16705131 3
4. row
Variable_name: I nnodb_ buffer_ pool_ reads
Va lue : 129236
5. row * * *************************
Variable_ name: I nnOdb_data_ pend ing_reads
Value: 0
6 . row
Variable data read
Value: 2135642112
7. row
Va ri able name: I nnodb data reads
Va1ue: 130309
*************************** 8. row
Variab1e_name: Innodb_pages_read
Value: 130215
9. row
Variable name: I nnodb rows read
Value: 17651085
9 rows in set (0.00 sec)

Innodb_buffer_p ool_reads
Innodb_buffer_p ool_read_ahead

Innodb _pool_read_ ahead_evicted

Innodb_buffer_p ool_read_requests
Innodb data read
Innodb data reads

Innodb_ buffer_pool_read_requests
_

Innodb_data_read
Innodb_data_reads
9.3

9.3.1

OOORPM ( rotate per

9.3.2
.() I. c...

Flash I Flash Memory


Memory
Host SRAM
I/F Controller l Memory
x32
Host

Flash
x16.1 Memory
Controller
C...

Random Access Time (ms)

Memory I 0.01

SSD3 Il o.05
DDHUAU
oaoa
nana 'ahu

- -
2 3 4

_ io_

Flash

9.4

9 .4 .1

RAID" (Redundant Array of Independent


. -. .ftl.CA

RAID
RAID 0
RAID

RAID RAIDO

150MB/so Disk 0 Disk 1

RAID 1

RAID I

1.

RAID
Striping Disk 0 Disk I

RAID

,
9 .4 391

RAID RAID5

01 : RAID

RAID Disk 0 Disk I Disk 2 Disk 3

RAID

RArD

RAID

RAID

RAID 10 RAID 01
RAIDO RAID 1

RAID 1 RAID 1 RAIDO RAIDO

RAID

RAID 50: RAID


C...
.,'-

RArDSO

RArDO

Disk 3 Disk 4 Disk 5 Disk 6 Disk 7

RAID

RAID

9 .4 .2 RAID Write

RAID Write

Write

Battery Backup
,,{, I.C.

Througho
Write

my sql>CREATE TABLE t ( a CHAR(2))Engine=!nnoDB;


Qu ery OK, 0 rows affe cted (0 .00 sec)

mysq l >DELI MI TER 1/


mysql>
my sql >CREATE PROCEDURE p ( )
->BEGIN
->OECLARE v INT;
->SET v mO;
->WHILE v<2 00000 00
->I NSERTINTO t VALUES( ' a a ' );
->SET v . v+ 1;
- >ENO WHILE;
- >END
- > 1/
Que r y OK , 0 rows a f fected (0.12 sec)

my s ql >DELI MIT ER ;

|
Write Back
Write Througb
Write Througb with innodb ftush l og
C...

_flush _Iog_at_trx_

9.4.3

[root@xen-server -]# /opt/MegaRAID/MegaCli/MegaCli64 -AdpAIIlnfo -aO

Adapter 10

Versions
================
Product Name : Meq aRAID SAS 8708ELP
Serial No : P012 233608
FW Package Build: 9.0.1-0030

HW Configuration
================
SAS Address 500605bOOOdle180
BBU : Present
Alarm : Present
9 .4

NVRAM Present
•••
Serial Debugger Present
Memory Present
Flash Present
Size 256MB
TPM Ab sent

Default 5ett i ngs

Phy Po larity : 0
PhyPolari tySplit : 240
Background Rate : 30
Stripe Size : 64kB
Flush Time : 4 seconds
Write Policy : WB
Read Policy : None
Cache Wh en BBU Bad : Diaabled
Cached IO : No
SMART Mode : Mode 6
Alarm Disable : Yes
Coercion Mode : 1GB

ZCR Con Ci g : Unknown


Dirty LED Show s Drive Activity : No
B105 Continue on Error No
5pin Down Mode None
Al lowed Device Type SA5/SATA Mix
Allow Mix in Enclosure Yes
All o w HDD 5A5/5ATA Mix in VD Yes
Allow 55D SA5/5ATA Mix in VD
Al low HDD/55D Mix in VD
Allow SATA in Cluster
Max Chain ed Enclosures 3
Disable Ctrl-R Yes
Enable Web B105 : Yes
Direct PD Mapping : No
B105 Enumera te VDs : Yes
Re store Hot 5pare on Insertion : No
Expose Enclosure Devices : Yes
PD Fail History : Yes
Disable Puncturing : No
Zero Based Enclosure Enumeratio n : No
PreBoot CL1 Enabled : Yes
LED Show Drive Acti vity :
Cluster Disable : Yes
6. ftl , CA
..'-

SAS Disable No
Auto Oetect BackPlane Enable SGPIO/i2c SEP
Use FOE Only No
Enabl e Led Header
Oelay during POST

SAS
( Write

[root@xen-server ‘ ) . /opt/MegaRAIO/MegaCli/MegaCli64 -POList -aALL

Adapter '0

Enclosure Oevice 10: 252


Slot Number: 0
Oevice Id: 8
Sequence Number: 2
Media Error Count: 0
Error Count: 0
Predictive Failure Count: 0
Last Predictive Failure Event Seq Number: 0
PO Type: SAS
Raw Size: 279.396 GB [Ox22ecb25c Sectors)
Non Coerced Size: 278.896 GB [Ox22dcb25c Sectors)
Coerced Size: 278.464 GB [Ox22ceeOOO Sectors)
Firmware state: Online
SAS Address(Ol: Ox5000c5000f363b55
SAS Address(l): OxO
Connected Port Number: o (pathO)
Inquiry Oata: SEAGATE ST3300655SS 00023LM5MGZZ
FOE Capable: Not Capable
FOE Enable: Oisable
Secured : Unsecured
Locked: Un l ocked
Foreign State: None
Oevice Speed: Unknown
Link Speed: Unknown
Media Type: Hard Oisk Oevice
C...

[root@xen-server -)lt /opt/MegaRAID/MegaCli/MegaCli64 -LDGetProp -Cache -LALL


-aALL

Adapter O-VD O(target id: 0) : Cache Policy:WriteSack , Rea dAheadNone , Direct , No


Write Cache i f bad BBU
Adapter O-VD 1 (target id: 1): Cache Policy:WriteSaek , Rea dAheadNone , Direct ,
Write Cache i f bad BBU

Exit Code: OxOO

lt/opt/MegaRAID/MegaCli/MegaCli64 -LDSetPropWB -LALL -aALL


t/opt/MegaRAID/MegaCli/MegaC1i64 -LDSetPropWT-LALL -aALL

9.5
C...

9.6

NTFS ,
9.7

9.7.1 sysbench

Microsoft SQL

[root@xen-server -J' sysbench


Missing required comrnand argument.
Usage:
sysbench [general-optionsJ .. . --test=<test-name> [test-options] ... command

General options:
--nurn-threads=N nurnber of threads to use [1]
--max-requests=N limit for total nurnbe r of requests [10000J
--max-time=N lirnit for total execution tirne in seconds [0)
--thread-stack-size=SIZE size of stack per thread [32KJ
C...

- - ini t -rng= [onlof f)


--test=STR1NG
initialize random number generator
test to run
[Offt __ A'"
--debug=[onloff) print more debugging info [off)
perform validation checks where possible [off)
--help=[on l off) print help and exit
--version=[onloff) print version and exi t

Compi1ed- in tests:
fileio - Fi1e 1/0 test
cpu - CPU performance t est
memory - Memory functions speed test
threads - Threads subsystem performance test
mutex - Mutex performance test
- OLTP test

Commands: prepare run c1eanup he1p version

See 'sysbench --test=<name> help' for a list of options for each test.

sysbench --test=fi1eio he1p


sysbench 0.4.10: multi- threaded system evaluation benchmark

fileio options:
--file-num=N number of files to create (128)
--fi1e-block-size=N block size to use in a l l 10 operations [16384)
--file-total-size=S1ZE total size of files to create [2G)
- -fi1e- te s t -mode=STR1NG test mode {seqwr , seqrewr , seqrd , rndrd , rndwr ,
rndrw}
- - fi 1e-io-mode- STR1NG . file operations mode Isync , async , fastmmap , s10wmmap}
[sync)
--file-extra-flags=STR1NG additional flags to use on opening files
Isync , dsync , direct} [)
--fi1e-fsync-freq=N do fsync() after this number of requests (0 -
don't use fsync()) [1001
--fi1e - fsync-a11 = [onloff) do fsync() after each write operation [off)
--fi1e-fsync-end=[onloff) do fsync() at the end of test [on)
--fi1e-fsync-mode-STRING which method to use for synchronization I fsync ,
fdatasync} [fsync)
--file- merged-requests=N merge at most this number of 10 requests if
possib1e (0 - don't merge) [0)
--fi1e-rw-ratio=N reads/writes ratio for combined test [1. 5 )
C...

o
seqrd

: 1.

[root@xen-server sSd]' sysbench --test=fileio - - file - num=16 --file-total-size=2G


prepare
sysbench 0.4.10: multi-threaded system evaluation benchmark

16 files , 131072Kb each , 2048Mb total


Creat i ng fi1es for the test...

[root@xen- server ssd ] i 1s -1h


tota1 2G
ee, a

402


- rw- ------ 1 r oo t root 128M Aug 12 10:42 t est_fi1e . 0
…-
-rw- - - 1 root
- rw---- --- 1 roo t
r oo t
root
128M Aug 1 2 10:42 t est_fi1e.1
128M Aug 12 10:42 test_fìle.10
-rw- --- - -- 1 root roo t 128M Aug 12 10: 42 test_file.11
- rw------- 1 root root 128M Aug 12 10:42 test_ file.12
- rw------ - 1 root root 128M Aug 12 10:42 test_file.13
-rw---- --- 1 root root 128M Aug 12 10:42 test_fi1e.14
-rw…---- 1 root
-rw------- 1 root
root
root
128M Aug 12 10: 42 test _file .15
1 28M Aug 12 10:42 test_ file.2
-rw--- ---- 1 root root 128M Aug 12 10:42 test fi1 e.3
-rw---- - -- 1 root root 128M Aug 12 10:42 test fi1 e.4
- rw------- 1 root root 128M Aug 1 2 10:42 test_fil e . 5
-rw- - -- - - - 1 root root 128M Aug 12 10:42 test_ fi1e . 6
-rw- ------ 1 roo t root 128M Aug 12 10:42 test fi1e.7
- rw - - - --- - 1 root root 128M Aug 1 2 10:42 test_ file.8
-rw- ------ 1 root r oot 128M Aug 12 10: 42 test_ file. 9

[ root@ x en- server ssd)' sysbench -- test = fileio --file- t otal-size=2G - - file-test-
mode = rndrd --max- time =180 --max-requests =100000000 --num-threads=16
--file-num-1 6 - - file - e xtra-Oags=direct - -fil e-fsync-freq=O - - file - block-size=16384 run

[root@xen-server ssd)' sysbench - - test= fileio - - file-total - size=2G - - file - test-


mode =rndrd --max-time= 180 --max- requests c l00000000 --num- threads = 16
--file- num=16 --file-extra- Oags=d i rect --file-fsync- freq=O - -file-block- s i ze=1 6384 run
sysbench 0.4.10: mu l ti - threaded system evaluat ion b enchmark

Running the t est with following options :


Number of th r eads: 16
1ni ti a l iz i ng random number generator from timer.

Extra file ope n flags: 16384


16 files , 1 28 Mb each
2Gb total file size
810ck size 1 6Kb
Number of random requests for random 10: 100000000
Read/Write ratio for combined random 10 test : 1.50
Calling fsync() at the end of test , Enabled .
Using synchronous 1/0 mode
Doing random r ead test
Threads started !
9.7

TtD ese
man
--E&O --m
,,‘ me ts
-LS xc er
eq dp
ee -da
ee'• ed qi
.Lnt
X -l'A -m
a e L e e s}

Operations performed: 619908 Read , 0 Wr i te , 0 Other = 619908 Total


Read 9. 4 59Gb Wri tten Ob Total t ra nsferred 9.459Gb (53.81 Mb /sec)
3443.8 5 Requests/ sec executed

Test e x ecution summary:


total time: 180.0044s
to t al number of events: 6 1 9908
total t ime ta ke n by event execution: 2878.0 750
per-reque st statis t ics:
0 .42ms
avg: 4.64ms
max : 27.30ms
approx. 95 percent i le: 8.13ms

Threads fa i rness:
events (avg/stddev): 38744.2500/10 2.69
execution time (avg/ stddev):

(root@xen-ser ver ssdJI sysbench --test=fileio --file-num=1 6 --file-total-s ize=2G


cleanup
sysbench 0.4.10: multi-threade d system eva l uation benchmark

Removing tes t files...

'!/bin/sh
set - u
set -x
set -e
f or size in 8G 64G;
for mode in seqrd seqrw rnd r d rndwr rndrw; do
for blksize in 4096 1638 4 ;
.{)I.CA
--.

sysbench --test=fileio --file-num= 64 --file-tot a l -size=$size prepare


for threads in 1 4 8 16 32;
Y‘ UJ
echo " ====== t esting $blksize in $threads threads"
echo PARAMS $s i ze $mode $threads $blksize>sysbench-size-$size-mode-$mode-
threads - $threads-blksz - $b1ksize
for i in 1 2 3 ;
sysbench --test=file i o --fi1 e - total - size=$size --file-test-mode-$mode\
--max-time- 180 --max-requests=lOOOOOOOO --num- threads=$threads --init-rng=on \
- - file - num=64 --fi1e - extra-flags=direct --file-fsync-freq=O --fi1e-block-
size=$blksize run \
I tee - a sysbench-size-$size- mode - $mode- threads - $threads- b l ksz-$blksize 2>&1
done
done
sysbench --test- fileio --file-total-size- $size cleanup
done
done
done

sbtest
[ root@xen-server -J'sysbench --test- oltp - -oltp-tab1e-size= 80000000 --db-
driver - mysq1 - -mysq1-socket- /tmp/mysql.sock --mysql-user=root prepare
sysbench 0.4.10: multi-threaded system evaluation benchmark

Creating tab1e 'sbtest'.


Creating 80000000 records in table 'sbtest'...

sysbench --test=oltp - - 01tp- table-size= 80000000 --oltp-read-on ly- off -- init-


rng =on --num-threads- 16 --max-requests-O --oltp-dist-type=uniform --max-time=3600
--mysql-user=root - -mysq1-socket=/tmp/mysq1.sock --db-driver=mysql run > res

sysbench 0.4.10: multi-threaded system eva 1 uation benchmark

WARNING: Preparing of "BEGIN" is unsupported , using emulation


(last message repeated 15 times)
Running the test with fol l owing options:
Number of threads: 16
I nitializing random number generator from timer.

Ooing OLTP test.


9.7

Running mixed OLTP test


Using Uniform distribution
Using " BEGIN" for starting transactions
Using auto_ inc on the id column
Threads started!
Time limit exceeded , exiting...
(last message repeated 15 times)
Done.

OLTP test statistics:


queries performed:
read: 6043324
write: 2158330
other: 863332
total: 9064986
transactions: 431666 (119.90 per sec.)
deadlocks: o (0.00 per sec.)
read/write requests: 8201654 (2278.07 per sec.)
other operations: 863332 (239.80 per sec.)

Test execution summary:


total time: 3600.2672s
tota l number of events: 431666
total time taken by event execution: 57598.5965
per-request statìstics:
6.84ms
avg: 133.43ms
max: 7155.61ms
approx. 95 percentile: 325.84ms

Threads fairness:
events (avg/stddev): 26979.1250 /64. 14
execution time (avg/stddev): 3599.9123/0.06.

9.7.2 mysql-tpcc

TPC CTransaction Processing Perfonnance


1.t:1I_

100

..

.) ,..

..
ó"__
ó ol_.. __
jd
..

per
https://code .launchpad.ne t/ ,...__

o
o tpcc_

(root@xen-server -)# tpcc_ load

*.... ###easy#.i TPC-C Data Loader *....

usage: tpcc_load [server) ( DB) [u ser ] (passj [warehousel


OR
tpcc_load (server) (DB] (user) (pass) [warehouse) [part] (min_wh] [ma x_whj

.. [part]: l = ITEMS 2=WAREHOUSE 3=CUSTOMER 4=ORDERS

[root@xen-server tpcc-mysql]# mysql tpcc<create_table.sql


(root@xen-server tpcc-mysqlj# mysql tpcc<add_fkey_idx.sql
[root@xen-server tpcc-mysql]# tpcc_load 127.0.0.1 tpcc2 root xxxxxx 1 00

#i#easy." TPC-C Data Loader ..**


11

nra r aI
ms
ee
J
ze
ev
-LT&sr
>}

. nv nu
[DBname): tpcc2
[u ser ): root ••
[pass) :
[warehouse): 100
TPCC Data Load Started...
Loading Item
………………… …………. . . . . . . . . . . . . . . . . 5000
……. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . .. 10000
………………… ……. . . . . . . . . . . . . . . . . . . . . . . 15000
.
...DATA LOADING COMPLETED SUCCESSFULLY.

[root@xen-server -)# tpcc_start

TPC-C Load Generator

usage: tpcc_start [server) [DB) [user) [pass) [warehouse) [connection) [rampup)


[measure)

o
o

[root@xen-server -)1 tpcc_start 127.0.0. 1 tpcc root xxxxxx 100 16 600 1200

*** "feasy'" TPC-C Load Generator ***

<Parameters>
[server): 127.0.0.1
[D8name): tpcc
[user): root
[ pass ): xxxxxx
[wa rehouse ) : 100
[connection]: 16
[rampup) : 600 (se c . )
[measure) : 1200 ( sec . )
1.

RAMP- UP TIME. ( 1 sec.)

MEASURING START .

10 , 624(0):0.4 , 624(0):0.2 , 62(0):0.2 , 63(0):0.6 , 62(0):0.8


20 , 990(0) :0.2 , 988(0) :0.2 , 98(0) :0.2 , 99(0) :0.4 , 98(0) :0.6
30 , 1435(0) :0.2 , 1436(0) :0.2 , 144 (0) :0.2 , 143(0):0.2 , 144(0):0.4
40 , 1736(0) :0.2 , 1739(0) :0 . 2 , 174(0) :0.2 , 174(0) :0.2 , 1 74(0) :0.4
50 , 2041(0):0.2 , 2044(0):0.2 , 204(0):0.2 , 204(0):0.2 , 207(0):0. 2
60 , 2195(0):0.2 , 2193(0):0.2 , 220(0):0.2 , 221(0):0.2 , 218(0):0.2
70 , 2332(0):0.2 , 2335(0):0.2 , 233(0):0.2 , 232(0):0.2 , 234(0):0.2
80 , 2408(0):0.2 , 2401(0):0.2 , 241(0):0.2 , 239(0):0.2 , 241(0):0.2
90 , 2473(0) :0.2 , 2476(0) :0.2 , 247(0) :0.2 , 250(0) :0.2 , 248(0) :0.2
100 , 2350(0) :0.2 , 2347(0) :0.2 , 235(0) :0.2 , 233(0) :0.2 , 235(0) :0.2

Order Per 10 Second

TPCC
1600
1400 .;
1200

800
-4RAID10

400
200
0
1 31 61 91 121151181211 241271301331361391421451481511541571601631661691

0 New Order Per )0 Second

Order Per 10
Order Per 10
t9 ..
-----
.
..

<Cons traint Check> (all must be [OK] )


[transaction percentage]
Payment: [OK]
Order-Status: 4.0%) [OK]
Delivery: [OK]
Stock- Level: [OK]
(response time (at least
New-Order:
Payment:
Order-Status:
Delivery: 100.00% [OK]
Stock-Level:

<TpmC>
1949 . 942 TpmC '

9.8
10.1

My5QL 5.1.49

.50"'<< Code

ltnl.uc (,ntl}rþri.." S4-...I'Vt:f" vcr. 1t


RP'W

H" I lÎUUl‘ :‘ S. l 'h'


P'ã Ól ðg..
; • f :":'

SuSC l1n ux ServQr 10 2l .0M ...


•.
......,.-v:, .s t

S. ..
M.PM
"'(.' !O

ShEn vo ct u,
H:Uøt5'
rE ut
5.: .'1 9 ..
A
H
,
p A
- ,a - " '-':C5'

tft>d or., ch: ;1 !o .L';'I


Hi6W'##'
':•• 1

rut.,rpri‘(1 Un UJic ::J

•. ..

,j u tlx 5. :'.11} 2:! .Wo'


"h' 'M'
,,
..\I.CA
412 .,'-

5.5.5

MySQL Community Server 5.5.5 m3

.8

I u lU lr ) .6 ( Ar ,-h il octur"
PtU:lcASI@
! t'I"W2
. ,t
SuSC 11 S.$. S .:1.1U, ifþl
AP M
: t'lr) :t;,,; s
O f'.,C:'" ;nHx !i
(
,1 " :.....r.:!
) !,

Slu S ‘ Unu)( ç .';'j .!1 M.'


MP M

R.ed ..nu a Otode CnteTpt1‘ e tln U)l: .. 7 1.OM '.J'! l!iMfti8


‘".‘qr.. " ,' 5. ,""

( ArchÏl e Cf \H1' 11. ft-" ...I M'.!I

""1.',:;, ð t1:' :.
10.2 411 ,
fsrael .4 I"T"

• J"15.. Japan HTTP F n>


• J'''.I( t'h}( lapafl lnc.. rT P
Qf Sc.onc cs. Pa last :j n FTP
:. : I( fU f"9 Gret...":), o( . 1Tr.>
r=- Lt<l., SinJðPO'" 14TIP rT"
iiI m"'CI. l w 1 C!(wan HTfP
unNorslty. I èJ ltflan FTP
T alwan Talwa n I-4 rrp n p
Sun 'V at - S en Talw .an I-4T TP FTP
Cp. n t m . Sh u . Te unlvorSlty ,
IH P Fr"

5.5
InnoDB

innodb

10.2

o dyn:
..{,I. CA
414

j.. 8UILD ,
j archive

cm"klt' t
., csv
CMake Fi les
þ cmd.line. util j,i fedefated
J, cpnfìg .t; heap
ti14:M
j" dbug ibmdb2i

e :>ctr"
J.. myisammfg
i: libmysql J"

1. libmysqld LJ Makefile.in
!; 'e

j; my.ys
j. ..

archive
Jt plugin
k blackhole

x l. csv
}. ..xample
l federated
.1: sql .bench jo heap

þ
j, ibmdb2i
l; innobase
l;i innodb_plugin

J. h my; sam

J.;
U Makefìle.am
é_: mysql_sto,age_engine.cmake

o
o include:
o lock:

o
C...
10.3 415 --'

o
o
o

o
o

10.3 MySQL

10.3.1

Studion

C:\workd ir>win\configure.j s options

o WITH INNOBASE STORAGE


o WITH PARTITION STORAGE

o
o
416

Studio Studio
Studio
2005 , build-vs8
Studio

-- Check for working C:/program Files/Microsoft Visual Studio 9.0/


VC/bin/cl.exe
-- Check for working C compiler: C:/program Files/Microsoft Visual Studio 9.0 /
VC/bin/cl.exe -- works
Detecting C ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: C: /program Files/Microsoft Visual Studio 9 .0 /
VC/bin/cl.exe
-- Chec k for working CXX compiler: C:/program Files/Microsoft Visual Studio 9.0/
VC/bin/cl.exe
Detecting CXX compiler ABI info
Detecting CXX compiler ABI info - done
Check size of void *
Check size of void * - done
SIZEOF VOIDP=4
Looking for include files HAVE_CXXABI_H
Looking for include files HAVE_CXXABI_H - not found. •
Looking for include files HAVE_NDIR_H
Looking for include files HAVE_NDIR_H - not found.
Looking for include files HAVE_ SYS_ NDIR_ H
Looking for include files HAVE_SYS_NDIR_H - no t found.
Looking for include fi1 es HAVE_ASM_ TERMBITS_H
Looking for include fil es HAVE_ASM_TERMBITS_ H - not found.
Looking for include files HAVE_TERMBITS_H
Looking for include files HAVE TERMBITS H - not found.
Looking for include files HAVE_VIS_H
Looking for include files HAVE VIS H - not found.
Looking for .include files HAVE WCHAR H
10.3 417 4b

Looking for include files HAVE WCHAR H - found


Looking for inc lude files HAVE WCTYPE H
Looking for include files HAVE WCTYPE H - found
Looking for
HAVE XFS XFS H include files
Looking for include files HAVE XFS XFS H - not found.
Looking for include files CMAKE HAVE PTHREAD H
Loo k ing for include files CMAKE HAVE PTHREAD H - not found.
Found Threads: TRUE
Looking for pthread_rwlockattr_ setkind_ np
Looking for pthread_ rwloc kattr_setkind_ np - not found
Performing Test HAVE_ SOCKADDR_ IN_ SIN_ LEN
Performing Test HAVE_ SOCKADDR_ IN_SIN_LEN - Failed
Performing Test HAVE_ SOCKADDR_ I N6_ SIN6_ LEN
Performing Test HAVE_SOCKADDR_I N6_SIN6_ LEN - Failed
Cannot find wi x 3 , installer project wil l not be gene r ated
Configuring done
Generating done
Build files have been written to: D:/Project/mysql-5.5.5-m3

... .!.t -:&11 tt'‘cf; ......:r

.....

• .... .'
_x

4·i
'''' l
,-

;. ,'1:.... ;:.
:'.".f if---
;.. :.:
.:u l""'.I. ........
-
,.1 . .." . • ,, .‘‘ ..
<1 ., ....t.;..
• Io
'1. a....,)O...-.;....
• ;.,..,
.
.. '" 100...
‘ ...
.. ...... ‘……………
., ,.. …………………..'
• .•… ‘
.
:;00 .... ...,........… .....h ‘
,;

........
-
a

:.',

.;. .
..toJ
. .r .!
j;
••

... "*' .!... ..


.",- .-
..

.……
…If.
""".- . .-..-...-
...•.,...
.. ‘
i.i
...
..
;
I

.'-!
.-.1;C

f
. . ..
"‘ ....Ul
.,....... ..
ou

thread
418

10.3.2

lDE for

[root@xen-server mysql-5.5.5-m3)# BUILD/compile-amd64-debug-max-no-ndb -c

5elect _ wlzerd
Create a new C++ prOject

11l( l zarCl s:
...… . . ø . '. '. ....… .....…'" .‘…..…. . .‘

: þ t,è} General

:V ê CIC++
Project

þ .::.' CV5

. .…_.…..…. . .. . ' •
6ft
10.3

i … -. - . • )( ,
C++ ProJect
f
Create C+ + or

ø Use gef aun

t. " ..

1'1
U"'_.' .. __ ". - - -

:J
81 '.‘ ; eg j

u -m
w… .du
· t Z
·n n M

hR

M
… • ……
mm …? ,
• …
.‘

;1: 14 …..…_.... .... ......‘…...... .. ... .... ---_ .... ..


6.{,I. CA
420

Fold.r
Creale a new folder resource

Enler or parent folder:

… .J
..………-…….._…...,_....…

ll--

o;1
:
11':'

'
;;2
1
:;'

In the fde system

=:J
10.3 InnoDB

"t'.
@

. . .. ...‘'‘….......... :
t
CJC++ ...

Resource
Builders

Þ c /c.. General

:i
Þ Task
Run/O ebug
Hlstory
Selllngs ii… type: 1..
i

..

Ðo. Þ ---

i
.. -. -

;:;t; Prol.(1: bÞWItf J:l


: z!
..

; ; i

! 4 . i
11

l
i
il
.. ,
422

..-…. _. , ..-_. • .••. • i

-
:, . ".
:

+ Attath 10 :: : i

.... lðUnCh
.
! ;; :: I
!‘
process 'nput : ;

Uslng 5tandard
mðttMd 5 or 5 .tems
.‘-...-‘.., ‘.......
(1)

Y •

Cr.... manag . ,. . nd run

-……__. ....…‘ . . . .- _. _.…… … … …...… ……. ...••.• . -_... •


.. _. _•
• r' . :'_ _...... •• • • ... .. ..
>-..... ,.......

……
-…..
! :V ………_.. ... . '.. .• ‘ • • .i i
!! j
:! j: !I ì: i :
d- ·- cc
e + A' r
o n M em oe
hu u e, ;
5
!I ì:} ;
SÞ- La\lnc h j
a ; .1
jjj ji j ;;
:; : L-... ,.,...………......_._.._..……… _ø' , .__- ._-_- - :
;
•• .' ……·…... .... ..øO :

;f j \V o r1<:in9 dlrectory' . ‘ . . • •.•• '"..‘....... .


.. ..…
.. … ……….. .. …}
:;! ;::
•• .• ….‘_.,. ,, •.••

&

.............. ,
,
i: I ": : !
... :
..

:j; i ; :
i Ftner matCn('d or 5 ftems
..., ....._.....1
eJ
10.4

Souru "-f.-c\lJt 54. ,( 1\ eun


;;!) o. s ..,. ;.. .

.,; ,,>>.
'."'
j: (' ,... ]')ØOI
i
!f-:;:
þ l' lhI U Cl U 8 J ...:
(1 91

..
" --.-.-. ……~…-…~……_... .. • -

: r
… …

l
i

..‘C'x_t• .l‘,..
,
"
',01..... .1': " ‘ , ,-
!;i …
; 1. u
ii
I

. .ct_op_1I1 t o .. • J 1.
1: :

I 1: i
…'_st.t. ...P-"94‘_I# ,….'ft
:;
I
j U
i:
.. ..H' .) ,‘ ..
õ c_...
.. U ",,_.a‘ tl"Ul_COUftt;
!
;\

"
!.i i" u nwn
J." i -. -----; . '

,--
…. …
…-……-…...
1l1l'I 008: C ".Y
.1"
l 00e 16 16: 03: 28
1'
1.1. 1 '"‘.. ..
1.6: 81: 1] 'v,,"‘ SUI*O;! \e":

' \ .$ . ‘O( ...t : so.,.ce


-r -
-"""---.--_-
C …- ,. .:1
2SS :32

10-1 8

10.4

cd mysql-xxx
mi<dir bld
cd b ld
c ma i< e .. -G Xcode
424

'

.. , !IiI A .. . " . 1
M tdl""

_....tA
r.: $ AI ", t - i m; .. ,. I
..
J a .....
a. f
1
;
;

\OOp:
Ð 1 i r.O!! I .. •

..d.,m.n ! "",' p therC IS dal Sb,l Se ðCti\'ìty by


1rn.t, k
• 01( Thtud ) í
i V_: ke rnel mutex";

• !i ThfUd 5
; JT..
; lT;a l
7: LP f
.n i n
ua
e-
t t
- eu
vbr
1 .t ‘ .t

Auu = lpc
1a&
-
"u. - . t
,$3su
sUwwc
nt
.mus i ‘ A+ a' ‘ • 2a.•.‘ . . ,, r eadu
: r.w:
ga-

‘ f ••
nr M
t --
e
7 w
'
,' mt
V

enz
,
!PHi
;Ð I VA
w ‘ , .;
,
• )! Th,tþd 7 : I
1'h r..d. inui t he IIser ðc t :vi ty counter
s ,- v_activ 1t y_ oUO\ ; , s. tð n o f ttllS
1 1"I'tS:
9
:
• Thtud 10
s.a:& 1f ( srv_10rce_recovery _ f

12 9010 suspend_thread;
1 17' a!
T""...14
i
:
::>,: lr /- We r<JfI
)s
t l> r folto.•lln9 \0"0 Ðpp r OX.I9> ð. e ly once per secor,a
Od l Jb. r. e ð CtlV l ty _/
:
•n s rv _ \115 1..\09_ nuSh_1 U o: e(NUL l);

: : / . S l\: ep fo r 1 00 t o.e t O( 1)<> \ 0>1 f í rst t)ll'o (!. '-1


next_1 t r _time = ut_ ti.ne_II\S () .. 1090;
I-nvr fn-r I t = A.t .L .,c' _1n: . __…-_.._-.-
,. ':;) i ,M • "0
,.
; ' • II .....
! : - (ulin t) 0

: •

;
=
Mac OS

10.5

You might also like