A Mysql Optimization

Mysql Optimization

First, the storage engine

1. Check all engine

SHOW ENGINES;

2. View the default storage engine

show VARIABLES LIKE '%STORAGE_engine%';

3.MyISAM and InnoDB

Comparison items MyISAM InnoDB
Primary foreign key not support stand by
Affairs not support stand by
Table row lock Table lock, even if a recording operation will lock the entire table, not suitable for high concurrency of operations Row lock, the lock operation only a row, no other line impact for highly concurrent operation
Cache Only cache index, not the actual data cache Not only cache index also true data cache, higher memory requirements and memory size have a decisive impact on its performance
Table space small Big
focus point performance Affairs
The default installation YES YES

Two, SQL execution load order

From left_table
on join_condition
<join_type> join right_table
where where_condition
group by group_condition
having having_condition
select
distinct select_list
order by orderBy_condtion
limit limit_num

Third, the index

1. Definitions

Mysql Index is to help efficiently get the data structure of the data. -> nature: data structures.

Its purpose is to improve query efficiency, analogous to the dictionary, it can be simply understood as sorted quickly find data structure .

Addition to the data itself, the database also maintains a data structure to meet specific search algorithm, these data point to data structures in some way, so that you can achieve the advanced search algorithm on the basis of these data structure, this data structure is indexed.

Generally speaking, if the index is not specified, that are B-tree (multiple search tree, not necessarily a binary) index structure of the organization. Wherein the aggregation index, secondary index, covering index, a composite index, prefix index, the default is to use a unique index B + tree index, the index collectively. Of course, in addition to the B + tree index of this type, as well as other hash index

The index will affect the searching and sorting

General index also great, can not all be stored in memory, so the index tends to index files are stored on disk.

2. Features

Benefits: improve data retrieval efficiency and reduce the cost of IO database.

Sort the data by an index, reducing the cost of sorting data, reducing the CPU usage.

Disadvantages: in fact, the index is also a table that holds the primary key and index fields, and points to record a solid table, so the index also occupy space.

Although it greatly increase the query speed, while it will reduce the speed of updating the table. Because not only update the table to save the data, but also save about index file is updated each time adding a field index column will adjust because the index information of the key changes brought about by the update.

Index only increases the efficiency of a factor, if you have a large number of database tables, you need to take the time to study the establishment of the best index, or optimize the query.

3. Category

  • Single-value index

    Index contains only a single column, a table can have multiple single-valued index.

  • The only index

    The value of the index columns must be unique, but allow free value

  • Composite index

    An index consists of multiple columns

4. Basic syntax

Created: UNIQUE is a unique index, columnname is only a single index value, there is more of a composite index

CREATE [UNIQUE] INDEX indexname on tablename(columnname1, columnname2...)
or
ALERT tablename add [UNIQUE] INDEX [indexname] on (columnname1, columnname2...)

delete:

DROP INDEX [indexname] on tablename

View

SHOW INDEX FROM tablename

The case for indexing

  1. Primary key automatically create a unique index
  2. Frequently as a query field should create an index
  3. Query tables and other related fields, foreign key relationships indexes
  4. Sort field query, if the sort field to access through an index, will greatly improve the sorting speed
  5. Speed ​​selection / combination index (high concurrency for creating composite index)
  6. Statistical query or group of fields

6. The situation is not suitable for creating an index

  1. Frequently updated fields

  2. where conditions are less than in the field

  3. Too little table records

  4. The average data duplication and distribution of table fields, and therefore should only be indexed for the most frequent queries and sort data most often

    = Selectivity index of a different number / total number

Guess you like

Origin www.cnblogs.com/ys1109/p/11792565.html