Slides da palestra realizada no Dia da Liberdade do Software 2016. Esta palestra mostra algumas maneiras de se obter mais performance com o banco de dados MySQL.
IEDR Blogueiro - blog.marceloaltmann.com Oracle ACE Associate - MySQL Oracle Certified Professional , MySQL 5.6 Database Administrator Oracle Certified Professional , MySQL 5 Database Administrator
FROM city WHERE CountryCode='BRA' AND Population > 1000000\G *************************** 1. row *************************** id: 1 select_type: SIMPLE table: city type: ALL possible_keys: NULL key: NULL key_len: NULL ref: NULL rows: 4188 Extra: Using where 1 row in set (0.00 sec)
FROM city WHERE CountryCode='BRA' AND Population > 1000000\G *************************** 1. row *************************** id: 1 select_type: SIMPLE table: city type: ALL possible_keys: NULL key: NULL key_len: NULL ref: NULL rows: 4188 Extra: Using where 1 row in set (0.00 sec)
leftMost(CountryCode, Population, District); mysql> EXPLAIN SELECT Name, Population FROM city WHERE CountryCode='BRA' AND Population > 1000000\G *************************** 1. row *************************** id: 1 select_type: SIMPLE table: city type: range possible_keys: leftMost key: leftMost key_len: 7 ref: NULL rows: 13 Extra: Using index condition 1 row in set (0.00 sec)
leftMost(CountryCode, Population, District); mysql> EXPLAIN SELECT Name, Population FROM city WHERE CountryCode='BRA' AND Population > 1000000\G *************************** 1. row *************************** id: 1 select_type: SIMPLE table: city type: range possible_keys: leftMost key: leftMost key_len: 7 ref: NULL rows: 13 Extra: Using index condition 1 row in set (0.00 sec)
(CountryCode, Population, Name); mysql> EXPLAIN SELECT Name, Population FROM city WHERE CountryCode='BRA' AND Population > 1000000\G *************************** 1. row *************************** id: 1 select_type: SIMPLE table: city type: range possible_keys: covered key: covered key_len: 7 ref: NULL rows: 13 Extra: Using where; Using index 1 row in set (0.00 sec)
(CountryCode, Population, Name); mysql> EXPLAIN SELECT Name, Population FROM city WHERE CountryCode='BRA' AND Population > 1000000\G *************************** 1. row *************************** id: 1 select_type: SIMPLE table: city type: range possible_keys: covered key: covered key_len: 7 ref: NULL rows: 13 Extra: Using where; Using index 1 row in set (0.00 sec)
temporária • max_head_table_size - tamanho máximo de uma tabela que utiliza memory engine • Tamanho tabela temporária: ◦ Maior que tmp_table_size ou max_head_table_size = criada em disco • Monitorar: mysql> SHOW GLOBAL STATUS LIKE 'Created\_tmp%tables'; +-------------------------+-------+ | Variable_name | Value | +-------------------------+-------+ | Created_tmp_disk_tables | 3 | | Created_tmp_tables | 17 | +-------------------------+-------+
e seus status mysql> show processlist; +----+------+-----------+-----------+---------+------+----------------+----------------------------------------------------------------------------------------- ---------+ | Id | User | Host | db | Command | Time | State | Info | +----+------+-----------+-----------+---------+------+----------------+----------------------------------------------------------------------------------------- ---------+ | 8 | root | localhost | employees | Query | 4 | Writing to net | SELECT MAX(salary) FROM salaries JOIN employees USING (emp_no) WHERE gender = 'M' GROUP BY emp_no | | 9 | root | localhost | employees | Query | 0 | init | show processlist | +----+------+-----------+-----------+---------+------+----------------+----------------------------------------------------------------------------------------- ---------+ 2 rows in set (0.00 sec)
# User@Host: root[root] @ localhost [] Id: 2 # Query_time: 1.127100 Lock_time: 0.000852 Rows_sent: 1000 Rows_examined: 297918 use employees; SET timestamp=1473370681; SELECT d.dept_name AS 'Dept', CONCAT(em.last_name, ' ', em.first_name) AS 'Manager last, first', CONCAT(e.last_name,' ', e.first_name, ' ', t.title) AS 'Employee last, first (title)' FROM dept_manager AS dm LEFT JOIN dept_emp AS de ON de.dept_no = dm.dept_no LEFT JOIN departments AS d ON d.dept_no = dm.dept_no LEFT JOIN employees AS e ON e.emp_no = de.emp_no LEFT JOIN employees AS em ON em.emp_no = dm.emp_no LEFT JOIN titles AS t ON t.emp_no = e.emp_no WHERE dm.emp_no = e.emp_no AND dept_name = 'Sales' OR dept_name = 'Marketing' AND dm.to_date >= '2012-05-07' AND t.to_date > '2012-05-07' AND de.to_date > '2012-05-07' ORDER BY e.last_name, e.first_name limit 1000; # Time: 160908 17:38:27 # User@Host: root[root] @ localhost [] Id: 2 # Query_time: 4.236115 Lock_time: 0.000377 Rows_sent: 179973 Rows_examined: 2366291 SET timestamp=1473370707; SELECT MAX(salary) FROM salaries JOIN employees USING(emp_no) WHERE gender = 'M' GROUP BY emp_no; # Time: 160908 17:38:33 # User@Host: root[root] @ localhost [] Id: 2 # Query_time: 3.268998 Lock_time: 0.000349 Rows_sent: 179973 Rows_examined: 2366291 SET timestamp=1473370713; SELECT MAX(salary) FROM salaries JOIN employees USING(emp_no) WHERE gender = 'M' GROUP BY emp_no;
desde a versão 5.6.6 • DIGEST: ◦ SELECT Name, Population FROM city WHERE CountryCode='BRA' AND Population > 1000000; ◦ SELECT Name, Population FROM city WHERE CountryCode='USA' AND Population > 5000000; ◦ SELECT Name , Population FROM city WHERE CountryCode = ? AND Population > ?; • Possibilita identificar queries : ◦ Não utilizam index: SELECT DIGEST_TEXT FROM events_statements_summary_by_digest WHERE SUM_NO_INDEX_USED > 0; ◦ Usam tabelas temporárias no disco: SELECT DIGEST_TEXT FROM events_statements_summary_by_digest WHERE SUM_CREATED_TMP_DISK_TABLES > 0;