Click here to load reader

5 古雷my sql源碼與資料庫規範

  • View
    1.129

  • Download
    0

Embed Size (px)

Text of 5 古雷my sql源碼與資料庫規範

  1. 1. MYSQL CNTV MYSQL DBA
  2. 2. CMUGMYSQL CNTVMYSQL MySQL DBA CNTV MySQL MySQL MySQL(CMUG)
  3. 3. database data column, field 11one bit 11one byte file temporary file memory character network
  4. 4. MySQL InnoDB page SQLcost
  5. 5.
  6. 6. FILESORT SQL MySQLORDER BYfilesort filesort SQL filesort
  7. 7. FILESORT SELECT6 select * select id,name address varchar(255) addressNULL SELECT61.19VS 0.20
  8. 8. mysql> flush status; mysql> select * from testsort order by name; mysql> select * from information_schema.session_status where variable_name in ('created_tmp_files','sort_merge_passes'); +-------------------+----------------+ | VARIABLE_NAME | VARIABLE_VALUE | +-------------------+----------------+ | CREATED_TMP_FILES | 3 | | SORT_MERGE_PASSES | 3 | +-------------------+----------------+ mysql> flush status; mysql> select id,name from testsort order by name; mysql> select * from information_schema.session_status where variable_name in ('created_tmp_files','sort_merge_passes'); +-------------------+----------------+ | VARIABLE_NAME | VARIABLE_VALUE | +-------------------+----------------+ | CREATED_TMP_FILES | 0 | | SORT_MERGE_PASSES | 0 | +-------------------+----------------+
  9. 9. FILESORT select id,name,address from testsort order by name; order by namename varchar(50) utf8 50utf850*32(150*2+2)/3=100 name1101 select id,name,address id int4name50*3+1+1=1521150255 nameaddress1bit char*8sort buffer 101+4+152+769+1+8=1035
  10. 10. mysql> set sort_buffer_size=1035*122881; flush status; mysql> select * into outfile '/data/dump/sort.txt' from testsort order by name; Query OK, 122881 rows affected (0.56 sec) mysql> select * from information_schema.session_status where variable_name in ('created_tmp_files','sort_merge_passes'); +-------------------+----------------+ | VARIABLE_NAME | VARIABLE_VALUE | +-------------------+----------------+ | CREATED_TMP_FILES | 0 | | SORT_MERGE_PASSES | 0 | +-------------------+----------------+ mysql> set sort_buffer_size=1035*122880; flush status; mysql> select * into outfile '/data/dump/sort.txt' from testsort order by name; Query OK, 122881 rows affected (0.91 sec) mysql> select * from information_schema.session_status where variable_name in ('created_tmp_files','sort_merge_passes'); +-------------------+----------------+ | VARIABLE_NAME | VARIABLE_VALUE | +-------------------+----------------+ | CREATED_TMP_FILES | 2 | | SORT_MERGE_PASSES | 1 | +-------------------+----------------+
  11. 11. FILESORT mysql> set max_length_for_sort_data=1024; //filesort1035 mysql> set sort_buffer_size=117*122881; //filesort mysql> flush status; // KEY88 mysql> show global status like 'innodb_rows_read'; +------------------+---------+ | Variable_name | Value | +------------------+---------+ | Innodb_rows_read | 1720348 | +------------------+---------+ mysql> select * into outfile '/data/dump/sort.txt' from testsort order by name; Query OK, 122881 rows affected (0.66 sec) mysql> select * from information_schema.session_status where variable_name in ('created_tmp_files','sort_merge_passes','innodb_rows_read'); +-------------------+----------------+ | VARIABLE_NAME | VARIABLE_VALUE | +-------------------+----------------+ | CREATED_TMP_FILES | 0 | //sort_buffer_size | INNODB_ROWS_READ | 1966110 | //1966110 - 1720348 = 245762 | SORT_MERGE_PASSES | 0 | //122881 +-------------------+----------------+
  12. 12. FILESORT SELECT * SELECT VARCHAR(n)n SQLWHEREORDER BYGROUP BYTEXT VARCHAR
  13. 13. SORT_MERGE_PASSES mysql> desc tab; +---------+--------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +---------+--------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | name | varchar(15) | YES | | NULL | | | address | varchar(200) | YES | | NULL | | +---------+--------------+------+-----+---------+----------------+ mysql> select * into outfile '/media/psf/Home/workspace/dump/sort.txt' from tab order by name; Query OK, 63584 rows affected (3.70 sec) mysql> select * from information_schema.session_status where variable_name in ('created_tmp_files','sort_merge_passes'); +-------------------+----------------+ | VARIABLE_NAME | VARIABLE_VALUE | +-------------------+----------------+ | CREATED_TMP_FILES | 3 | | SORT_MERGE_PASSES | 28 | +-------------------+----------------+
  14. 14. SYSTEMTAP [email protected]:~# stap calls_count.stp /usr/local/mysql56debug/bin/mysqld make_sortkey 63584 write_keys 169 merge_buffers 28 create_temp_file 3 filesort_free_buffers 3 filesort 1 trace_filesort_information 1 init_for_filesort 1 merge_many_buff 1 merge_index 1
  15. 15. SYSTEMTAP [email protected]:~# cat calls_count.stp global calls probe process(@1).function("*write_keys*").call , process(@1).function("*filesort*").call , process(@1).function("*merge_buffers*").call , process(@1).function("*create_temp_file*").call , process(@1).function("*make_sortkey*").call , process(@1).function("*merge_many_buff*").call , process(@1).function(*merge_index*").call { calls[ppfunc()] ++ } probe timer.s(10) { foreach (name in calls- limit 30) printf ("%st%dn", name, calls[name]) print("n") }
  16. 16. SORT_MERGE_PASSES 1169=7*24+124237 8 224=7*3+332710 331 24+3+1=28merge_bufferssort_merge_passes make_sortkey 65384 keyname write_keys 169 sort buffer 169169 merge_buffers 28 merge sort7 create_temp_file 3 3 16*sort_buffer_size
  17. 17. SYSTEMTAP [email protected]:~# stap iotime.stp 19120 ## (mysqld) access /tmp/MY2h0vCA read: 43682208 write: 43682208 (mysqld) access /tmp/MYfjNKjs read: 87364416 write: 87364416 (mysqld) access /tmp/MYXM65AV read: 41711104 write: 41711104 (mysqld) access /media/psf/Home/workspace/dump/sort.txt read: 0 write: 36424243 id int 4name varchar(15) utf8 45+1+1address varchar(200) utf8 600+2+21656 nameKEY(45*2+2)/3=30131 (656+31)*63584=43682208; 43682208*2=87364416 41711104/63584=656 169/tmp/MYfjNKjs/ tmp/MY2h0vCA/tmp/MYfjNKjs /tmp/MYXM65AV/tmp/MYXM65AV
  18. 18. SYSTEMTAPIOTIME.STP [email protected]:~# cat iotime.stp #! /usr/bin/env stap global start global time_io function timestamp:long() { return gettimeofday_us() - start } function proc:string() { return sprintf("%d (%s)", pid(), execname()) } probe begin { start = gettimeofday_us() } global filehandles, fileread, filewrite probe syscall.open.return { if( pid() == strtol(@1,10) ) { filename = user_string($filename) if ($return != -1) { filehandles[pid(), $return] = filename } else { printf("%d %s access %s failn", timestamp(), proc(), filename) } } }
  19. 19. IOTIME.STP probe syscall.read.return ,syscall.pread.return { #MySQLpread if( pid() == strtol(@1,10) ) { p = pid() fd = $fd bytes = $return time = gettimeofday_us() - @entry(gettimeofday_us()) if (bytes > 0) fileread[p, fd] += bytes time_io[p, fd]

Search related