- MySQL indexes are used to quickly find records with specific values, and all MySQL indexes are saved as B-trees
- If the query is not indexed, MySQL must start the whole table scan from the first record, knowing that the query to the required records, the larger the record, the higher the cost of time
- If an index has been created on a column as a search condition, MySQL can quickly get to the location of the target record without scanning any records.
- For example, the table has 1000 records, the index lookup record is at least 100 times times faster than the sequential scan record
Common index types
- Normal General Index
- Unique unique index, does not allow duplicate indexes, the field information is guaranteed not to repeat, such as a social security number
- Full text index is used to search for a long article, the best effect, if used in relatively short text, one or two lines of words, the normal index can also
Summarize:
The category of the index is determined by the indexed field content attribute, which is usually the most common form of normal
Create an index
ALTER TABLE tabname Add index idxname (column)
ALTER TABLE tabname add unique (column)
ALTER TABLE TabName add primary key (column)
Create INDEX Idxname on tabname (column)
Create unique index idxname on tabname (name)
Delete Index
Drop Index idxname on tabname
ALTER TABLE tabname DROP INDEX Idxname
MySQL Slow query
" %slow% " ; 2 | more than 2 seconds are defined as slow queries. | Slow_query_log | OFF | slow query off state. | Slow_query_log_file | /data/mysql/var/db-test2-slow.log | The file that queries the log slowly.
Set global slow_query_log=on;[ Mysqld]log
1
Log-queries-not-using-indexes #表示记录下没有使用索引的查询.
- Mysqldumpslow Analysis Log
Mysqldumpslow, easy to use, parameters available-help view -ar--/data/ Mysql/var/db-test2-slow.log
Efficient optimization:
Three-point configuration, seven-point SQL statement optimization
- Optimized for configuration
- System kernel Optimization
- MY.CNF configuration file
- Optimization of SQL statements
- Optimization of table structure
- Optimization of indexes
Optimization of hardware
- Increased memory and disk read and write speed, can improve the MySQL database query, update speed
- Using a disk array
Optimization of parameters
- Buffer in memory for MySQL reserved portions
- Buffers can increase the speed of MySQL
- The buffer size is set in the MY.CNF configuration file.
A few important memory parameters
MySQL transcription 4: Indexing, slow query, optimization