7.4 optimize database structure 7.4.1 design select MySQL to store record data and index data in different files respectively. Many other (almost all) databases store this record and index data in the same file. We believe that MySQL is more suitable for a wider range of systems. Another way to store record data is to preserve the information of each field
7.4 optimize database structure 7.4.1 design select MySQL to store record data and index data in different files respectively. Many other (almost all) databases store this record and index data in the same file. We believe that MySQL is more suitable for a wider range of systems. Another way to store record data is to preserve the information of each field
7.4 optimize database structure 7.4.1 Design Selection
MySQL stores record data and index data in different files respectively. Many other (almost all) databases store this record and index data in the same file. We believe that MySQL is more suitable for a wider range of systems.
Another way to store record data is to store the information of each field in an independent region (such as SDBM and Focus ). This will hit performance when each query needs to access more than one field. Because the more fields accessed, the worse the performance is, we think this mode is not suitable for normal databases.
In more cases, the indexes and data are stored together (for example, Oracle/Sysbase ). In this way, the record information can be found on the leaf page of the index. The advantage of this layout is that, because the index is cached well, it can save disk reading, but it also has the following Disadvantages:
- Because you need to read the index to obtain data, scanning the table is slower.
- Data can only be obtained based on the index during query.
- More disk space is required because the index must be copied from the node (records cannot be saved in the node ).
- Deleting a table slows down (because the index in the node is not updated at the time of deletion ).
- It is difficult to cache index data only.
7.4.2 make data smaller and more flexible
One of the most basic principles of optimization is to design data tables to occupy less disk space. This can greatly improve the performance, because the disk reads faster, and smaller tables only need less primary memory when processing the query content. Indexing on small fields requires less resource load.
MySQL supports many different table types and record formats. You can decide which storage engine/index method to use for each table. Selecting appropriate Table Styles based on different applications can greatly improve performance. For details, see "15 MySQL Storage Engines and Table Types ".
You can use the following methods to improve table performance and save storage space:
- Use the most effective (minimum) data type as much as possible. MySQL has several specific types that can save disk and memory.
- Use a smaller Integer type as much as possible. For example,
MEDIUMINTGenerally betterINT.
- Define the field type as follows:
NOT NULL. This will run faster, and each field will save 1 bit. If you do need to useNULLThen it is explicitly specified. However, to avoid all fields, the default value isNULL.
- In
MyISAMIf no variable length field (VARCHAR,TEXT, OrBLOBField), the record format is fixed. This is faster, but it may waste some space. For details, see "15.1.3MyISAMTable Storage Formats ".
- The primary index of the table should be as short as possible. In this way, each record has a name ID and is more efficient.
- Only the required indexes are created. Indexing is conducive to record retrieval, but is not conducive to quick record storage. If you always need to search on the combined fields of the table, create an index on these fields. The first part of the index must be the most commonly used field. If you need to use many fields, you should first copy these fields to make the index more compressed.
- A field may have different numbers of characters at the beginning, so it is more appropriate to index these characters. MySQL supports indexing on the leftmost part of a field. The shorter the index, the faster it is, not only because it occupies less disk space, but also because it increases the index cache hit rate, thus reducing disk search. For details, see "7.5.2Tuning Server Parameters ".
- In some cases, it is more advantageous to split a frequently scanned table into two. When scanning a dynamic table to obtain relevant records, it may use a smaller static table.
7.4.3 field Index
All MySQL field types can be indexed. Index on related fields to improveSELECTStatement performance is the most effective.
The maximum index length and maximum number of indexes for each table are defined by the storage engine. For details, see "15 MySQL Storage Engines and Table Types ". All storage engines Support at least 16 indexes for each table, and the minimum index length is 256 bytes. Most storage engines have higher restrictions.
Used in index formatcol_name(length)Syntax, you can onlyCHAROrVARCHARTheLengthIndex. Similarly, indexing only the prefix of a field can make the index file smaller.
MyISAMAndInnoDB(Starting from MySQL 4.0.14)BLOBAndTEXTThe field is indexed,RequiredSpecify the index prefix length, for example:
CREATE TABLE test (blob_col BLOB, INDEX(blob_col(10)));
The prefix length can be up to 255 bytes (starting from MySQL 4.1.2,MyISAMAndInnoDBThe table supports 1000 bytes ). Note that the prefix length limit is measured by the number of bytes. HoweverCREATE TABLEThe prefix length in the statement is interpreted as the number of characters. Therefore, when specifying the length of the field index prefix, you must consider the use of multi-Byte Character Set fields.
From MySQL 3.23.23, you can createFULLTEXTIndex, they use full-text search. OnlyMyISAMTable supportCHAR,VARCHARAndTEXTField CreationFULLTEXTIndex. Only valid for the entire field search. Partial (prefix) search is not supported. For details, see "13.6 Full-Text Search Functions ".
From MySQL 4.1.0, you can also index spatial fields. Currently, onlyMyISAMThe storage engine supports space types. Spatial indexes use the R tree index.
MEMORY(HEAPThe storage engine supports hash indexes. From MySQL 4.1.0, it also supports B-tree indexes.
7.4.4 multi-field Index
MySQL can create an index on multiple fields, which can contain up to 15 fields. You can also use prefix Indexes for specific field types (for details, see "7.4.3 Column Indexes ").
A multi-field index can be considered as an array composed of the values of the index fields and sorted.
MySQL uses multi-field indexes as follows:WHEREThe clause specifies the first field of a known number of indexes, and the query is very fast. You do not need to specify the values of other fields.
Assume that a table has the following structure:
CREATE TABLE test (
id INT NOT NULL,
last_name CHAR(30) NOT NULL,
first_name CHAR(30) NOT NULL,
PRIMARY KEY (id),
INDEX name (last_name,first_name));
IndexnameOverwritelast_nameAndfirst_nameField. This index is in the fieldlast_nameUp orlast_nameAndfirst_nameIt can be used to query within a specified range. Therefore, this index is used in the following queries:
SELECT * FROM test WHERE last_name='Widenius';
SELECT * FROM test
WHERE last_name='Widenius' AND first_name='Michael';
SELECT * FROM test
WHERE last_name='Widenius'
AND (first_name='Michael' OR first_name='Monty');
SELECT * FROM test
WHERE last_name='Widenius'
AND first_name >='M' AND first_name < 'N';
However, IndexnameThe following queries are not used:
SELECT * FROM test WHERE first_name='Michael';
SELECT * FROM test
WHERE last_name='Widenius' OR first_name='Michael';
How MySQL uses indexes to improve query performance is discussed in the next chapter.
Optimize MySQL (5) upgrade MySQL (7 )›