Mysql database optimization

Source: Internet
Author: User
Tags lock queue mysql functions
I have made up my mind to start learning oracle. I have been using mysql for a short time. today I will write down these statements to explain my journey to mysql. the following is my personal experience in using mysql. There may be many mistakes and errors. please correct me !! First, in order to make a system faster, the most important part is the basic design, but some of them are currently MySQL optimization.

I have made up my mind to start learning oracle. I have been using mysql for a short time. today I will write down these statements to explain my journey to mysql. the following is my personal experience in using mysql. There may be many mistakes and errors. please correct me !!

First, in order to make a system faster, the most important part is the basic design, but some things are insurmountable in the current situation, such as common bottlenecks of the system.

What I can think:

1: High-speed hard drive (7200 rpm). In theory, 7200 channels are searched per second. there is no way to change this. the optimization method is to use multiple hard disks or store data separately.

2: Hard disk read/write speed, which is very fast (limited by my knowledge, only know dozens or even hundreds of MB per second ). this is easier to solve-parallel read/write from multiple hard disks.

3: the cpu. the cpu processes data in the memory. this is the most common limiting factor when there is a table with a relatively small memory.

4: memory limit. when the cpu needs to exceed the data suitable for the cpu cache, the cache bandwidth becomes a bottleneck in the memory. However, the memory size is surprisingly large, and this problem will not occur.

Step 2:

(I am using the linux platform of the school website (Linux 2.4.3-19mdk ))

1: adjust server parameters

Use the shell> mysqld-help command to output the following information:

Possible variables for option -- set-variable (-o) are:

Back_log current value: 5 // The number of connections that mysql can have. back_log indicates how many connection requests can be stored in the stack during the time when mysql suspends the connection.

Connect_timeout current value: 5 // mysql server waits for a connection time before responding with bad handshake

Delayed_insert_timeout current value: 200 // The time for an insert delayed to wait for the insert before termination

Delayed_insert_limit current value: 50 // The insert delayed processor checks whether any select statements are not executed. If yes, run these statements before resuming.

Delayed_queue_size current value: 1000 // the size of the queue allocated for insert delayed

Flush_time current value: 0 // if it is set to a non-0 value, all tables are closed for each flush_time.

Interactive_timeout current value: 28800 // time the server waits for a foreign interactive connection before closing it

Join_buffer_size current value: 131072 // The buffer size connected to all

Key_buffer_size current value: 1048540 // The buffer size of the clause index block. you can increase it to better process the index.

Lower_case_table_names current value: 0 //

Long_query_time current value: 10 // if a query takes more time than this time, the slow_queried count will increase

Max_allowed_packet current value: 1048576 // size of a package

Max_connections current value: 300 // number of concurrent connections allowed

Max_connect_errors current value: 10 // if there are more than this number of interrupted connections, further connections will be blocked. you can use flush hosts to solve this problem.

Max_delayed_threads current value: 15 // Number of insert delayed processes that can be started

Max_heap_table_size current value: 16777216 //

Max_join_size current value: 4294967295 // number of connections that can be read

Max_sort_length current value: 1024 // Number of bytes used for sorting blob or text

Max_tmp_tables current value: 32 // number of temporary tables simultaneously opened by a connection

Max_write_lock_count current value: 4294967295 // specify a value (usually very small) to start mysqld, so that the read lock occurs after a certain number of write locks.

Net_buffer_length current value: 16384 // communication buffer size -- reset to this size during query

Query_buffer_size current value: 0 // buffer size during query

Record_buffer current value: 131072 // size of the buffer allocated to each table scanned by connections for each sequential scan

Sort_buffer current value: 2097116 // size of the buffer allocated for each sort connection

Table_cache current value: 64 // number of tables opened for all connections

Thread_concurrency current value: 10 //

Tmp_table_size current value: 1048576 // temporary table size

Thread_stack current value: 131072 // the size of each thread

Wait_timeout current value: 28800 // time the server waits for a connection before closing it 3

Configuring the above information as needed will help you.


1: If you create a large number of tables in a database, the open and close operations will be slow.

2: mysql memory usage

A: The keyword cache zone (key_buffer_size) is shared by all threads.

B: each connection uses some specific thread space. one stack (64 k by default, the variable thread_stack), one connection buffer (the variable net_buffer_length) and one result buffer (net_buffer_length ). under certain circumstances, the connection buffer and result buffer are dynamically extended to max_allowed_packet.

C: All threads share a base memory.

D: No Memory shot

E: each request for sequential scan is allocated a read buffer (record_buffer)

F: All joins are completed once, and most joins do not even need a temporary table. the most temporary table is a memory-based (heap) table.

G: one sort buffer and two temporary tables are allocated for sorting requests.

H: all syntax analysis and computation are completed in one local memory.

I: each index file is opened only once, and the data file is opened once for each concurrent thread.

J: for tables in each blob column, a buffer is dynamically expanded to read blob values.

K: The table processors of all tables in use are saved in a buffer and managed as a fifo.

L: a mysqladmin flush-tables command to close all tables that are not in use and mark all tables in use at the end of the currently executed thread to be closed.

3: mysql lock table

All locks in mysql will not be deadlocked.

Wirte lock:

Mysql locking Principle: a: If the table is not locked, it will be locked; B otherwise, it will put the lock request into the write lock queue.

Read lock:

Mysql locking Principle: a: If the table is not locked, it will be locked; B otherwise, it will put the lock request into the read lock queue.

Sometimes many select and insert operations are performed in a table. you can insert rows in a temporary table and occasionally update the real table with the records of the temporary table.

A: Use the low_priority attribute to give a specific insert, update, or delete with a lower priority.

B: max_write_lock_count specifies a value (usually very small) to start mysqld, so that the read lock occurs after a certain number of write locks.

C: By using set SQL _low_priority_updates = 1, you can specify all the changes from a specific thread to be completed with a lower priority.

D: use high_priority to specify a select

E: if an error occurs when using insert... select..., use myisam table ------ because it supports concurrent select and insert

4: The most basic optimization is to minimize the space occupied by data on the hard disk. if the index is on the smallest column, the index is also the smallest. implementation method:

A: use the smallest possible data type.

B: If possible, the declared table column is NOT NULL.

C: If possible, use a data type such as varchar (but the speed may be affected)

D: each table should have the primary index as short as possible.

E: Create required indexes

F: if an index has a unique prefix on the first few characters, then only the index prefix-mysql supports the index on a part of a character column.

G: If a table is frequently scanned, try to split it into more tables.

Step 4

1: When using indexes, the importance of indexes will not be mentioned, and functions will not be mentioned.

First, we need to clarify that all mysql indexes (primary, unique, index) are stored in tree B. The primary terms of indexes are as follows:

A: quickly locate the record with the where specified condition

B: When the join is executed, the row is retrieved from other tables.

C: Find max () and min () values for specific index columns

D: If sorting or grouping is prefixed at the beginning of an available key, sorting or grouping a table

E: a query may be used to optimize the search value without accessing the data file. if the columns in some tables are numeric and exactly the prefix of a column, you can retrieve the values from the index tree for faster speed.

2: query speed for storing or updating data

Grant execution will slightly reduce the efficiency.

Mysql functions should be highly optimized. you can use benchmark (loop_count, expression) to find out if there is a problem with the query.

Select query speed: If you want to make a select... where... faster. All I can think of is creating an index. you can run myisamchk -- analyze on a table to optimize the query. you can use myisamchk -- sort-index -- sort-records = 1 to sort an index and data.

3: mysql optimizes the where clause

3.1: Remove unnecessary parentheses:

(A AND B) AND c OR (a AND B) AND (a AND d)> (a AND B AND c) OR (a AND B AND c AND d)

3.2: constant

( B> 5 AND B = c AND a = 5

3.3: Delete a constant condition

(B> = 5 AND B = 5) OR (B = 6 AND 5 = 5) OR (B = 100 AND 2 = 3)> B = 5 OR B = 6

3.4: the constant expression used by the index is calculated only once.

3.5: in a table, no where count (*) directly retrieves information from the table

3.6: all constants are read before any other table in the query.

3.7: The best join combination for external join tables is to try to find all possibilities.

3.8: if an order by clause and a different group by clause, order by, or group by clause contain columns not from the first joined table, create a temporary table.

3.9: if SQL _small_result is used, msyql uses a table in the memory.

3.10: Indexes of each table are used for queries and indexes that span less than 30% rows.

3.11 skip rows that do not match the having clause before each record is output

4: optimize left join

In mysql, a left join B is implemented as follows:

A: Table B depends on Table.

B: Table a depends on all tables used in the left join condition (except for B)

C: all left join conditions are moved to the where clause.

D: perform all join optimization. except a table, it is always read after all the tables it depends on. if there is a circular dependency, an error will occur.

E: perform all standard where optimization

F: if a row matches the where clause, but B does not have any matching left join conditions, all rows generated in B are set to NULL.

G: if left join IS used to locate rows that do NOT exist in some tables and column_name is null in the where clause (column_name is not null ). after mysql finds a row that matches the left join condition, it will stop searching for more rows.

5: Optimized limit

A: If you use limit to select only one row, when mysql needs to scan the entire table, it serves as an index.

B: If limit # and order by are used, if mysql finds row #, the sorting will end, instead of sorting the entire table.

C: When limit # and distinct are combined, if mysql finds row #, it stops

D: as long as mysql has sent the first line to the customer, mysql will discard the query

E: limit 0 always returns an empty set quickly.

F: use limit for the temporary table size # calculate the amount of space required to solve the query

6: optimize insert

Insert a record consists of the following:

A: Connection (3)

B: Send a query to the server (2)

C: analysis query (2)

D: insert record (1 * Record size)

E: insert an index (1 * Index)

F: disable (1)

The preceding figure is proportional to the total time.

Some methods to improve the insert speed:

6.1: If many rows are inserted from a connection at the same time, insert with multiple values is faster than using multiple statements.

6.2: If many rows are inserted from different connections, the insert delayed statement is faster.

6.3: myisam is used. If no row is deleted in the table, the row can be inserted while select: s is running.

6.4: when loading a table from a text file, load data infile is usually 20 faster than insert.


6.5: The table can be locked and inserted. The major speed difference is that after all the insert statements are completed, the index buffer is saved to the hard disk only once. it is generally faster to store data as multiple insert statements. if you can use a single statement to insert all rows, locking is not required. locking also reduces the overall connection time. however, the maximum waiting time for some threads will rise. for example:

Thread 1 does 1000 inserts

Thread 2, 3 and 4 does 1 insert

Thread 5 does 1000 inserts

If no lock is used, 2, 3, 4 will be completed before 1 and 5. if locked, 2, 3, 4, it may be completed after 1 and 5. however, the overall time should be 40% faster. because the insert, update, and delete operations are fast in mysql, you can achieve better overall performance by locking more than five consecutive inserts or updates of a row. if you insert many rows, you can create a lock tables, and occasionally create an unlock tables (about every 1000 rows) to allow other threads to access the table. this will still lead to good performance. load data infile is still very fast for loading data.

To speed up load data infile and insert, expand the keyword buffer.

7. optimize the update speed

The speed depends on the size of the updated data and the number of updated indexes.

Another way to make update faster is to delay modification, and then make a lot of modifications on one row and one row. if the table is locked, make a lot of modifications on one row and one row faster than one at a time.

8. optimize the delete speed

The time for deleting a record is proportional to the number of indexes. to delete the record more quickly, you can increase the index cache size.

Deleting all rows from a table is much faster than deleting most of the tables.

Step 5

1: Select a table type

1.1 Static myisam

This format is the simplest and safest format. it is the fastest disk format. the speed comes from the difficulty that data can be found on the disk. when locking an index or static format, it is very simple, just multiply the length of the row by the number. in addition, when scanning a table, it is easy to read constant records from a disk each time. security comes from the fact that if the computer is down when a static myisam file is written, myisamchk can easily point out where each row starts and ends. Therefore, it can usually retrieve all records, except for some written records. all indexes in mysql can always be rebuilt.

1.2 Dynamic myisam

In this format, each line must have a header indicating how long it is. when a record changes for a long time, it can end at more than one position. you can use optimize tablename or myisamchk to organize a table. if you want to access/change static data as some varchar or blob columns in the same table, move the dynamic column into another table to avoid fragmentation.

1.2.1 compress myisam and use the optional myisampack tool to generate

1.2.2 memory

This format is useful for small/medium-sized tables. copying/creating a common query table to a foreign heap table may speed up the joining of multiple tables. Using the same data may take several times faster.

Select tablename. a, tablename2.a from tablename, tablanem2, tablename3 where

Tablaneme. a = tablename2.a and tablename2.a = tablename3.a and tablename2.c! = 0;

To accelerate it, you can use tablename2 and tablename3 to create a temporary table, because you can use the same column (tablename1.a) to search for it.

Create temporary table test TYPE = HEAP


Tablename2.a as a2, tablename3.a as a3


Tablenam2, tablename3


Tablename2.a = tablename3.a and c = 0;

SELECT tablename. a, test. a3 from tablename, test where tablename. a = test. a1;

SELECT tablename. a, test, a3, from tablename, test where tablename. a = test. a1 and ....;

1.3 static table features

1.3.1 default format. used when the table does not contain varchar, blob, and text columns

1.3.2 fill all char, numeric, and decimal columns with column width

1.3.3 fast

1.3.4 easy Buffering

1.3.5 it is easy to re-create after being down because the record is in a fixed position

1.3.6 do not need to be reorganized (with myisamchk) unless a large number of records are deleted and the storage size is optimized

1.3.7 generally requires more storage space than dynamic tables

1.4 characteristics of dynamic tables

1.4.1 if the table contains any varchar, blob, and text columns, use this format

1.4.2 all string columns are dynamic

1.4.3 each record is preceded by a single digit.

1.4.4 usually requires more disk space than a fixed-length table

1.4.5 each record only uses the required space. if a record becomes large, it is divided into many segments as needed, resulting in record fragmentation.

1.4.6 if the row is updated with information that exceeds the length of the row, the row is segmented.

1.4.7 it is difficult to recreate the table after the system is down, because a record can be multiple segments

1.4.8 The expected row length for dynamic size records is 3 + (number of columns + 7)/8 + (number

Of char columns) + packed size of numeric columns + length of strings + (number

NULL columns + 7)/8

Penalty of 6 bytes for each connection. A dynamic record is connected whenever the change causes the record to become larger. each new connection has at least 20 bytes, so the next change may be in the same connection. if not, there will be another connection. you can use myisamchk-to check the number of connections. all connections can be deleted using myisamchk-r.

1.5 features of a compressed table

1.5.1 a read-only table created using the myisampack utility.

1.5.2 The decompression code is stored in all mysql distributions so that connections without myisampack can read tables compressed with myisampack.

1.5.3 occupies a small disk space

1.5.4 each record is compressed separately. the header of a record is a fixed length (1 ~~ 3 bytes) depending on the maximum table record. each column is compressed in different ways. some common compression types are:

A: Usually there is a different table for each column.

B: suffix blank compression

C: prefix blank compression

D: use a number with a value of 0 for 1-bit storage.

E: if the value of an integer column has a small range, the column uses the smallest possible type for storage. for example, if all values are between 0 and 255, a bigint can be stored as a tinyint.

G: If a column has only one small set of possible values, the column type is converted to enum.

H: the combination of the preceding compression methods can be used for columns.

1.5.5 records of fixed or dynamic length can be processed, and blob or text columns cannot be processed.

1.5.6 use myisamchk for decompression

Mysql supports different index types, but the general type is isam. this is a B-tree index and can roughly calculate the size of the index file as (key_length + 4) * 0.67, the sum of all keys.

String indexes are empty compressed. If the first index is a string, it can compress the prefix. if the string column contains many trailing spaces or a varchar column with the full length of the primary path, empty compression will make the index file smaller. if many strings have the same prefix.

1.6 features of memory tables

The heap table in mysql uses the 100% dynamic hash for every occasional overflow and there are no deletion problems. you can only use an index in the heap table to access things using equations (usually using the '=' operator)

The disadvantages of heap tables are:

1.6.1 all heap tables to be used at the same time require sufficient additional memory

1.6.2 cannot be searched in one part of the index

1.6.3 The next entry cannot be searched in sequence (that is, an order by index is used)

1.6.4mysql cannot calculate the approximate number of rows between two values. this is used by the optimizer to determine which index to use, but on the other hand, it does not even need disk seek.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.