11. Query Handler?? ???? Message #1
sql_select.cc
handle_query()
sql_optimizer.cc
JOIN::optimize()
sql_execuor.cc
JOIN::exec()
init
executing
sending data
end
optimizing
statistics
preparing
SELECT
INSERT ¡ SELECT
Multi Table Update
Multi Table Delete
lock.cc
mysql_lock_tables()
system lock
11
MySQL State Message ??
12. Query Handler?? ???? Message #2
sql_updatet.cc
mysql_update()
sql_delete.cc
mysql_delete()
sql_insert.cc
mysql_insert()
init
update
Single Update
Single Delete
Insert
end
init
updating
end
init
searching rows
for update
end
lock.cc
mysql_lock_tables()
system lock
updating
12
MySQL State Message ??
14. State Message? ??
Fencepost? ??
? ?) LOAD DATA ??? ???? handler(sql_load.cc)??? State ??? ???? ??
mysql> LOAD DATA INFILE £§/MYSQL/test.txt£§ INTO TABLE my_test_table;
Query OK, 1000000 rows affected (35.42 sec)
Records: 1000000 Deleted: 0 Skipped: 0 Warnings: 0
mysql> show profile for query 1;
+------------------------------+-----------+
| Status | Duration |
+------------------------------+-----------+
| starting | 0.000250 |
| checking permissions | 0.000019 |
| Opening tables | 0.000044 |
| System lock | 35.417423 | ? ??? ???? ????
| query end | 0.000012 | 1) System Lock(external table lock) ??? 35? ???
| closing tables | 0.000016 | 2) System lock ?? ?? ???? query end ?? ??
| freeing items | 0.000035 | ???? 35? ??? (?? ??? ?? ??)
| cleaning up | 0.000009 |
+------------------------------+-----------+
14
MySQL State Message ??
15. State Message? ??
MySQL Reference Manual? ??? ??? ?? ???? ??
? system lock
The thread is going to request or is waiting for an internal or external system lock for the table. If this
state is being caused by requests for external locks and you are not using multiple mysqld servers that are
accessing the same MyISAM tables, you can disable external system locks with the --skip-external-locking
option. However, external locking is disabled by default, so it is likely that this option will have no
effect. For SHOW PROFILE, this state means the thread is requesting the lock (not waiting for it).
? InnoDB? external locking ?? ??? ?????, MySQL? Table ?? ??? external locking? ???
Storage Engine?? MySQL?? ???? interface? ?? ???? ??, InnoDB ?? ??? ??
mysql_lock_tables() => table_handler->external_lock(thd,locktype)
? ?? external locking ??? ?? ??? ?? Storage Engine?? ????, ???? ?? bypass ??
(?) Memory(Heap) Storage Engine ¨C ha_heap.cc
15
MySQL State Message ??
16. State Message? ??
MySQL Reference Manual? ??? ??? ?? ???? ??
? ??? External locking ??? Transaction-aware Storage Engine?? ?? ??? ???
=> ?? ??? ???? ??,
MySQL??? system lock ???
???
16
MySQL State Message ??
17. ???? DML Query? State ?? Review
starting opening tables init system lock
optimizing
statistics
preparing
executing
sending data
searching rows
for update
update
end
updating
query endclosing tablesfreeing items
17
MySQL State Message ??
cleaning up
18. ???? DML Query? State ?? Review
starting opening tables init system lock
optimizing
statistics
preparing
executing
sending data
searching rows
for update
update
end
updating
query endclosing tables
?? ??
query id ??
Parser ?? ??? ??
query ?? ?? ??
?? status ?? ?? (Questions)
Query cache ???? ?? ??
Statement ?? ? handler ??
18
MySQL State Message ??
freeing items
cleaning up
The thread has processed one command and is preparing
to free memory and reset certain state variables.
======================================================
?? ?? (?? ??)
DB ??? ???? query ?? ?? ?? ? ??? ??
DB ?? ?? Sleep?? ??
The thread has executed a command. Some freeing of items done
during this state involves the query cache. This state is
usually followed by cleaning up.
===========================================================
?? ?? (Parser ??)
Parser ?? ?? ??
¡ù query cache ?? ??? ??? ??
19. ???? DML Query? State ?? Review
starting opening tables init system lock
optimizing
statistics
preparing
executing
sending data
searching rows
for update
update
end
updating
query endclosing tables
19
MySQL State Message ??
freeing items
cleaning up
The thread is flushing the changed table data to
disk and closing the used tables. This should be a
fast operation. If not, verify that you do not have
a full disk and that the disk is not in very heavy
use.
====================================================
??? ?? ? ???
table closing ? table open cache? ??
metadata lock ??
¡ù closing tables ??? ??? ???? ??
Lock_open mutex ?? ??? ??
¡ù ?? ??? flushing? ???? ??
The thread is trying to open a table. This is should be very
fast procedure, unless something prevents opening. For example,
an ALTER TABLE or a LOCK TABLE statement can prevent opening a
table until the statement is finished. It is also worth
checking that your table_open_cache value is large enough.
==============================================================
??? ??? ? ??? ??
Lock Mode ?? (DDL, Lock Table? ?? Metadata Lock ?? ??)
Metadata version ?? ? table definition cache ??
table open
¡ù Opening tables ??? ??? ???? ??
MySQL 5.5 ???? Lock_open mutex? table open/close? ??
table open cache ??? ??? ?? ??, ?? ?????
?? ??
20. ???? DML Query? State ?? Review
starting opening tables init system lock
optimizing
statistics
preparing
executing
sending data
searching rows
for update
update
end
updating
query endclosing tables
20
MySQL State Message ??
freeing items
cleaning up
This state occurs after processing a query but
before the freeing items state.
==============================================
?? ?? ??
?? ?? ????? ???? ???? ?? ??
¡ù ?? ???? ?? ? ???? ???? ??
??? query end ??? ???. ?? ???? ?
??? ?? ???? ????, ?? ?? ???
SHOW ENGINE INNODB STATUS ??? undo log record
? ??? ?? ??
21. ???? DML Query? State ?? Review
starting opening tables init system lock
optimizing
statistics
preparing
executing
sending data
searching rows
for update
update
end
updating
query endclosing tables
21
MySQL State Message ??
This occurs at the end but before the cleanup
of ALTER TABLE, CREATE VIEW, DELETE, INSERT,
SELECT, or UPDATE statements.
=============================================
?? ???? ?? ??
Statement ?? ???
- query cache invalidation
- binlog writing
¡ù 5.7 code ???? insert ??? ?? query
cache invalidation ? binlog writing? end
stage ?? ?? ??? update ???? ???
(feature?? bug?? ?? ??)
freeing items
cleaning up
This occurs before the initialization of ALTER TABLE, DELETE, INSERT,
SELECT, or UPDATE statements. Actions taken by the server in this state
include flushing the binary log, the InnoDB log, and some query cache
cleanup operations.
For the end state, the following operations could be happening:
- Removing query cache entries after data in a table is changed
- Writing an event to the binary log
- Freeing memory buffers, including for blobs
==========================================================================
?? ???? ?? ??
?? ?? ??? ??? ??
?) ??? ???? ??, ???? ?? ???? ?? (updatable view)
¡ù ?? Flushing ?? ??? ???? ??
22. ???? DML Query? State ?? Review
starting opening tables init system lock
optimizing
statistics
preparing
executing
sending data
searching rows
for update
update
end
updating
query endclosing tables
22
MySQL State Message ??
freeing items
cleaning up
The thread is going to request or is waiting for an internal or external system
lock for the table. If this state is being caused by requests for external locks
and you are not using multiple mysqld servers that are accessing the same MyISAM
tables, you can disable external system locks with the --skip-external-locking
option. However, external locking is disabled by default, so it is likely that
this option will have no effect. For SHOW PROFILE, this state means the thread is
requesting the lock (not waiting for it).
================================================================================
? ???? external locking ??
?, Transactional ????? ?? ?? ???
- ???? ??? statement? ?????? ??
- savepoint ?? ?? ?
23. ???? DML Query? State ?? Review
starting opening tables init system lock
optimizing
statistics
preparing
executing
sending data
searching rows
for update
update
end
updating
query endclosing tables
23
# Logical transformations:
- Outer to inner joins transformation.
- Equality/constant propagation.
- Partition pruning.
- COUNT(*), MIN(), MAX() constant substitution
in case of implicit grouping.
- ORDER BY optimization.
# Perform cost-based optimization of table order
and access path selection.
# Post-join order optimization:
- Create optimal table conditions from the
where clause and the join conditions.
- Inject outer-join guarding conditions.
- Adjust data access methods after determining
table condition
- Optimize ORDER BY/DISTINCT.
# Code generation
- Set data access functions.
- Try to optimize away sorting/distinct.
- Setup temporary table usage for grouping
and/or sorting.
MySQL State Message ??
freeing items
cleaning up
The server is performing initial optimizations for a
query.
The server is calculating statistics to develop a
query execution plan. If a thread is in this state
for a long time, the server is probably disk-bound
performing other work.
This state occurs during query optimization.
==================================================
?? Rewrite(Transformation) ? ???
Join ? access path ??
¡ù ??? ??? ???? SQL? statistics ????
??? ?????, ?? ?? ?? ???? optimizer
? ??? ???? ??? ??? ? ? ??.
24. ???? DML Query? State ?? Review
starting opening tables init system lock
optimizing
statistics
preparing
executing
sending data
searching rows
for update
update
end
updating
query endclosing tables
24
MySQL State Message ??
The thread has begun executing a statement.
freeing items
cleaning up
The thread is reading and processing rows for a
SELECT statement, and sending data to the client.
Because operations occurring during this state
tend to perform large amounts of disk access
(reads), it is often the longest-running state
over the lifetime of a given query.
================================================
?? ??? Client?? ??? ?? ??
Storage Engine?? ?? ? Filter ?? ? ???
? ?? ???? ??? ??
25. ???? DML Query? State ?? Review
starting opening tables init system lock
optimizing
statistics
preparing
executing
sending data
searching rows
for update
update
end
updating
query endclosing tables
25
MySQL State Message ??
freeing items
cleaning up
The thread is getting ready to start updating the
table.
==================================================
INSERT ??? ?? ??? ??
update state? ?? INSERT ?? ????? ??
26. ???? DML Query? State ?? Review
starting opening tables init system lock
optimizing
statistics
preparing
executing
sending data
searching rows
for update
update
end
updating
query endclosing tables
26
The thread is doing a first phase to find all
matching rows before updating them. This has to be
done if the UPDATE is changing the index that is
used to find the involved rows.
==================================================
¡ù searching rows for update state ??? ????
?, ??? ?? ??? ??? ???? ?? ???
????.
The thread is searching for rows to update and is
updating them.
==================================================
Update ?? Delete ?? ??? ?? ??? ??
MySQL State Message ??
freeing items
cleaning up