Optimization, analysis Mysql table read and write, index and other operations efficiency optimization problems sql statement

Why Optimization:

With the start of the actual project, the database after a period of operation, the initial database settings, and there will be some difference between the actual performance of the database is running, then we need to do an optimized adjustment.

This large database optimization problem can be divided into four categories:

"Host Performance
" Memory Performance
"network transmission performance
" SQL statement execution performance [software engineer]
Here are some SQL database optimization:

(01) to select the most efficient sequence table name (often written test)

In the case of the parser database processed in the order from right to left of the table name in the FROM clause, the FROM clause written in the final table will be processed first, it contains more than one table in the FROM clause, you must select the least number of records on the final table, if there are more than 3 tables join query, then you need to select the table referenced by other tables at the end.

For example: query employee number, name, salary, salary grade, department name

select emp.empno,emp.ename,emp.sal,salgrade.grade,dept.dname
from salgrade,dept,emp
where (emp.deptno = dept.deptno) and (emp.sal between salgrade.losal and salgrade.hisal)

 

1) If the three tables are completely unrelated, it will record and minimal table column names, written in the final, and then so on
2) If three tables are related, it will most cited table, placed at the end, then And so on

(02) connected sequentially in the WHERE clause (often written test)

Database using from right to left in order to resolve the WHERE clause, according to this principle, the connection between tables must be written in other Zhizuo WHERE conditions, those conditions can filter out the maximum number of records must be written in the WHERE clause of the right hand.

For example: query employee number, name, salary, department name

select emp.empno,emp.ename,emp.sal,dept.dname
from emp,dept
where (emp.deptno = dept.deptno) and (emp.sal > 1500)

(03) in the SELECT clause to avoid the use of an asterisk

In the process of parsing database, the * will in turn be converted into all the column names, this work is done by querying the data dictionary, which means more time-consuming

select empno,ename from emp;
(04)用TRUNCATE替代DELETE

(05) as much as possible the use of COMMIT

Because COMMIT will release rollback point

(06) replaced with a WHERE clause HAVING clause

WHERE first execution after execution HAVING

(07) multi-use internal functions to improve the efficiency of SQL

Alias ​​(08) using a table

salgrade s

(09) using the column alias

ename e

In short, database optimization problem is not the day, you have to work in the long-term practice, repeated testing and summary, we hope the students a good understanding of the future

Today we share some sql statement analysis mysql table read and write, indexes, etc. operations.

Much gossip that directly on the code:

Reading and writing pressure is reflected Table

SELECT file_name AS file,
count_read,
sum_number_of_bytes_read AS total_read,
count_write,
sum_number_of_bytes_write AS total_written,
(sum_number_of_bytes_read + sum_number_of_bytes_write) AS total
FROM performance_schema.file_summary_by_instance
ORDER BY sum_number_of_bytes_read+ sum_number_of_bytes_write DESC;

Delay reflects file

SELECT (file_name) AS file,
count_star AS total,
CONCAT(ROUND(sum_timer_wait / 3600000000000000, 2), 'h') AS total_latency,
count_read,
CONCAT(ROUND(sum_timer_read / 1000000000000, 2), 's') AS read_latency,
count_write,
CONCAT(ROUND(sum_timer_write / 3600000000000000, 2), 'h')AS write_latency
FROM performance_schema.file_summary_by_instance
ORDER BY sum_timer_wait DESC;

table read and write delay

SELECT object_schema AS table_schema,
object_name AS table_name,
count_star AS total,
CONCAT(ROUND(sum_timer_wait / 3600000000000000, 2), 'h') as total_latency,
CONCAT(ROUND((sum_timer_wait / count_star) / 1000000, 2), 'us') AS avg_latency,
CONCAT(ROUND(max_timer_wait / 1000000000, 2), 'ms') AS max_latency
FROM performance_schema.objects_summary_global_by_type
ORDER BY sum_timer_wait DESC;

View table operation frequency

SELECT object_schema AS table_schema,
object_name AS table_name,
count_star AS rows_io_total,
count_read AS rows_read,
count_write AS rows_write,
count_fetch AS rows_fetchs,
count_insert AS rows_inserts,
count_update AS rows_updates,
count_delete AS rows_deletes,
CONCAT(ROUND(sum_timer_fetch / 3600000000000000, 2), 'h') AS fetch_latency,
CONCAT(ROUND(sum_timer_insert / 3600000000000000, 2), 'h') AS insert_latency,
CONCAT(ROUND(sum_timer_update / 3600000000000000, 2), 'h') AS update_latency,
CONCAT(ROUND(sum_timer_delete / 3600000000000000, 2), 'h') AS delete_latency
FROM performance_schema.table_io_waits_summary_by_table
ORDER BY sum_timer_wait DESC ;

Index status

SELECT OBJECT_SCHEMA AS table_schema,
OBJECT_NAME AS table_name,
INDEX_NAME as index_name,
COUNT_FETCH AS rows_fetched,
CONCAT(ROUND(SUM_TIMER_FETCH / 3600000000000000, 2), 'h') AS select_latency,
COUNT_INSERT AS rows_inserted,
CONCAT(ROUND(SUM_TIMER_INSERT / 3600000000000000, 2), 'h') AS insert_latency,
COUNT_UPDATE AS rows_updated,
CONCAT(ROUND(SUM_TIMER_UPDATE / 3600000000000000, 2), 'h') AS update_latency,
COUNT_DELETE AS rows_deleted,
CONCAT(ROUND(SUM_TIMER_DELETE / 3600000000000000, 2), 'h')AS delete_latency
FROM performance_schema.table_io_waits_summary_by_index_usage
WHERE index_name IS NOT NULL 
ORDER  BY sum_timer_wait ASC ;

Full table scan case

SELECT object_schema,
object_name,
count_read AS rows_full_scanned
FROM performance_schema.table_io_waits_summary_by_index_usage
WHERE index_name IS NULL
AND count_read > 0
ORDER BY count_read DESC;
没有使用的index

SELECT object_schema,
object_name,
index_name
FROM performance_schema.table_io_waits_summary_by_index_usage
WHERE index_name IS NOT NULL
AND count_star = 0
AND object_schema not in ('mysql','v_monitor')
AND index_name <> 'PRIMARY'
ORDER BY object_schema, object_name;

Bad sql Problem Summary

SELECT (DIGEST_TEXT) AS query,
SCHEMA_NAME AS db,
IF(SUM_NO_GOOD_INDEX_USED > 0 OR SUM_NO_INDEX_USED > 0, '*', '') AS full_scan,
COUNT_STAR AS exec_count,
SUM_ERRORS AS err_count,
SUM_WARNINGS AS warn_count,
(SUM_TIMER_WAIT) AS total_latency,
(MAX_TIMER_WAIT) AS max_latency,
(AVG_TIMER_WAIT) AS avg_latency,
(SUM_LOCK_TIME) AS lock_latency,
format(SUM_ROWS_SENT,0) AS rows_sent,
ROUND(IFNULL(SUM_ROWS_SENT / NULLIF(COUNT_STAR, 0), 0)) AS rows_sent_avg,
SUM_ROWS_EXAMINED AS rows_examined,
ROUND(IFNULL(SUM_ROWS_EXAMINED / NULLIF(COUNT_STAR, 0), 0)) AS rows_examined_avg,
SUM_CREATED_TMP_TABLES AS tmp_tables,
SUM_CREATED_TMP_DISK_TABLES AS tmp_disk_tables,
SUM_SORT_ROWS AS rows_sorted,
SUM_SORT_MERGE_PASSES AS sort_merge_passes,
DIGEST AS digest,
FIRST_SEEN AS first_seen,
LAST_SEEN as last_seen
FROM performance_schema.events_statements_summary_by_digest d
where d
ORDER BY SUM_TIMER_WAIT DESC
limit 20;

 

Master these sql, you can easily know there is a problem with your library that table, and then consider how to optimize.

to sum up

That's all for this article, I hope the contents of this paper has some reference value of learning for all of us to learn or work, thank you for the support scripts House. If you want to know more details, please see the related links below

Guess you like

Origin www.cnblogs.com/hnsongbiao/p/11070014.html