Mysql Optimization - Database Structure

The following is intended only for Mysql InnoDB engine. Mainly from Mysql 5.6 version of the Reference Manual.

Data Structure Design Optimization 1

Careful design for the table structure is necessary, because the compact data structure design can significantly reduce the amount of data needed to interact in a disk exchange, the required memory space. And, accordingly can reduce the space occupied by the index. In short, the data structure can be lifted from the optimized design of both speed and space, so as to enhance performance.

1.1 Design

  • In case of needs, only to meet the needs of data types (as small as possible) a. For example, the type MEDIUMTEXT occupies 3 bytes (-8388608-8388607), the type INT 4 bytes of storage space (-2147483648-2147483647), 25% relative save storage space.
  • If possible, the column is declared as NOT NULL. Benefiting from two aspects: to build the non-null columns in the index, mysql is possible to avoid using the index detecting whether the column is empty additional consumption, to improve performance; for each row of data, 1 bit can be saved by to indicate whether this row is empty storage space.

1.2 Row Format Design

In version 5.6, InnoDB storage engine the default line format COMPACT. COMPACT, DYNAMIC and COMPRESSED are of the same series of compact row storage format, this type of storage format can reduce data storage space in the disk, the negative impact that cause high CPU consumption in some operations. If Mysql performance is limited by the speed or disk cache hit rate, this type of format that can enhance performance. And if Mysql has reached a CPU bottleneck, this format may result in performance more investigation.

1.3 Index Design

  • The primary key (primary key) length occupied should be as short as possible. Speed, the shorter the primary key makes it possible to quickly determine the corresponding row of the table. The space for the InnoDB, each secondary index (secondary index) will save a copy of the primary key, so that a shorter primary key can significantly reduce the storage space.
  • We must only create indexes to improve query performance. Because the index even though it can improve query performance, but will result in slower insert and update.
  • If multiple columns by restrictions on table query, create conditions for multiple columns composite index , rather than creating a separate index on each column.
  • If the lookup table for a plurality of columns is usually the query conditions, the combination index column having a first column should be repeated a maximum value, in order to obtain a better index compression. (// TODO)

1.4 join queries (Join)

  • In some cases, the query is converted to a higher frequency table two tables of joint queries to improve performance.
  • For a table of the joint inquiry, the join condition column type and size is exactly the same statement .
  • To keep column names simple to be able to use the same column name at the time of the joint multi-table queries. For example, for the customer table (customer) Name column declared as name instead customer_name.

1.5 paradigm

Generally, it should follow a predetermined paradigm, redundant data is kept low, for the column's long length stored separately and associated pick-id. However, in case of need to get better query performance, you can enhance performance through data redundancy.

Optimization Data Type 2

2.1 Numeric

For a unique ID number or character or situation can be represented using a numeric type as possible. Because the digital type usually occupy less storage space, and faster when compared to the type and size conversion.

2.2 Character

  • Binary (binary) collation, sorting and comparison to when the operation speed faster.
  • When data needs to be compared for the different columns, the columns of the same declared type of coding type and collation. Two types of conversion to avoid outside.
  • For character data is less than 8KB, the use of type VARCHAR, BLOB field types do not use class. Generated when the temporary table and ORDER BY GROUP BY, when the original table has no class BLOB field types can be employed MEMORY engine. (For utf8 encoding format, VARCHAR type capable of storing about 2.7K characters)
  • When there is a long list of character data fields such as name, address, and drop a query on the table usually does not need to return these fields, you can consider the establishment of a separate table to store these fields. This is mainly because, Mysql data when reading a column in a row, all columns will be the line where (may also include adjacent columns) are read. In this manner sub-table storage, can significantly reduce disk I / O and memory usage at the time of data reading.
  • In InnoDB engine, using a randomly generated value as the primary key column using as an ordered value (e.g., time or date) as a prefix to the time that the data is saved as compact as possible, so that the reading or writing mysql faster.

2.3 BLOB type

  • For text using BLOB storage should first be considered for compression.
  • Similarly the character, when the query returns BLOB fields normally not considered part table storage .
  • Consider the use of separate storage devices (SSD) or a database instance be stored.
  • Do not for very long columns are equal comparisons can be stored and indexed in its better performance to get the content of the results HASH alone , get better performance by HASH listed in the comparison, then use comparing the original data sequence to obtain a final comparison result.

2.4 PROCEDURE ANALYSE analyzed

ANALYSE by analyzing the specified query and returns the data type of the proposed optimization results. By using the sentence:

SELECT … FROM … WHERE … PROCEDURE ANALYSE(max_elements, [max_memory])

max_elements (default is 256), the number of independent values ​​for each line of each row is specified analysis.

MAX_MEMORY (default is 8192), designated for each column of the allocated memory size analysis.

3 internal temporary table

In some cases, mysql will produce a temporary table, but the user can not make any operation control permissions on these tables.

Generating a temporary table common scenarios are: sub-queries, GROUP BY ... ORDRER BY DISTINCT ... ORDER BY combination or combinations as a result of a temporary table. When Msysql create a temporary table, Created_tmp_table state variables corresponding to the increase, when the temporary table to disk temporary tables, Create_tmp_disk_table correspondence will increase.

Case with disk temporary tables are:

  • BLOB or TEXT columns
  • Chaoguo 512 byte (binary character) or 512 characters (non-binary) is now listed after GROUP BY or DISTINCT
  • Using UNION or UNION ALL, while the query column column contains over 512 byte (binary string) or 512 characters (non-binary) of

For in-memory temporary table that stores the engine is MEMORY; disk for temporary tables, which storage engine is MyISAM.

【Reference】

MySQL 5.6 Reference Manual

Guess you like

Origin www.cnblogs.com/luojiahu/p/11074794.html