mysql refresher

How to optimize SQL?

A:
(1) Choosing the right storage engine
with MySQL, for example, there are two storage engines including MyISAM and InnoDB, both advantages and disadvantages for each engine.
MyISAM suitable for some need a lot of queries applications, but for a large number of writes is not very good. Even if you only need to update a field, the entire table will be locked up, and another process, even if the process can not be read until the read operation is completed. Further, MyISAM for SELECT COUNT (*) is calculated such ultrafast extremely.

InnoDB trend would be a very complicated storage engine, for some small applications, it will be slower than MyISAM. But it supports "row lock", so the write operation when more and more, will be better. And he also supports more advanced applications, such as : the transaction .

 

(2) optimization of the data type field

As a rule, the smaller the column sooner . If you only have a few columns of a table Bale (such as dictionary table, the configuration table), then we have no reason to call the shots using the INT key, use MEDIUMINT, SMALLINT TINYINT or less will be more economical. If you do not record time, using DATETIME better than DATE. Of course, you also need to leave enough enough room for expansion.

 

(3) add an index for the search field

The index is not necessarily a primary key or unique field. If your table, you always have to have a field often used to do a search, then it is better indexing it, unless you want to search the fields are large text fields, it should be full-text indexed.

 

(4) Avoid using Select * read more data from the database, then the query becomes slower. And, if your database server and WEB server are two separate servers, then it will increase the load on the network. Even if all the fields you want to query the data table, but also try not to use the * wildcard, use the built-in field provides troubleshooting definitions may give more convenience.

 

(5) use ENUM instead of VARCHAR

ENUM type is very fast and compact. In fact, its preservation is TINYINT, but its appearance on the show as a string. As a result, use this field to do some of the options list becomes quite perfect. For example, the values ​​of these fields like gender, ethnicity, and state departments of limited and fixed, then you should use ENUM instead of VARCHAR.

 

(6) NOT NULL use as much as possible

Unless you have a very special reason to use a NULL value, you should always get your fields to keep NULL the NOT . NULL actually need extra space, and when you compare, your program will be more complex. Of course, this does not mean that you can not use NULL, the reality is very complex, still in some cases, you need to use a NULL value.

 

(7) the fixed length table will be faster

If all the fields in the table are "fixed length", the entire table is considered a "static" or "fixed-length". For example, the following table is no type of field: VARCHAR, TEXT, BLOB. As long as you include one of these fields, then the table is not a "fixed-length static table", so, MySQL engine will use another method to deal with.

 

Fixed length table will improve performance, because MySQL search will get faster, because the fixed length is easy to calculate the next offset in the data, it will naturally be read quickly. And if the field is not a fixed length, so every time to find the next one, then the program needs to find the primary key.

 

And, fixed length tables are also more likely to be cached and reconstruction. However, the only side effect is that fixed-length fields will waste some space, because the fixed-length field whether you use do not, he is to be allocated so much space.

 

Practice how to optimize MySQL

I was in the following four sequential answer, they affect a maximum four, getting smaller and smaller from behind the first effect.

① optimizing SQL statements and index

② database table structure optimization

③ configuration system optimization

④ hardware optimization

 

Optimization of the database

  1.  Select the most suitable field attribute definition field width to minimize, as far as possible field NOTNULL provided, for example, 'Department', 'Sex' best applicable ENUM
  2.  Using the connection (the JOIN) instead subquery
  3.  Apply to the United (UNION) instead of manually create the temporary table
  4.  Transaction Processing
  5.  Lock the table, optimized transaction processing
  6.  Applicable foreign key, optimistic locking table
  7.  Indexing
  8.  Query optimization

The reason SQL injection produced: program development process, specification writing sql statements and do not pay attention to the special character filtering, leading to some of the client can submit a properly executed sql statement by the global variables POST and GET.

For relational databases, the index is a very important concept, please answer a few questions about indexes:

a), what is the purpose of the index?
Specific information quickly access data in the table to improve the retrieval speed

Create a unique index to ensure the uniqueness of each row of data in a database table.

The connection between tables and table acceleration

When using packet data retrieval and sorting clause, can significantly reduce the query time grouping and ordering

b), any negative impact on the index is a database system?
Negative effects:
create indexes and index maintenance takes time, this time with the increase in the amount of data increases; the index needs to take up physical space, not just the data tables need to occupy space, each index also need to take up physical space; when the table add, delete, change, when the index should be dynamic maintenance, thus reducing the speed of data maintenance.

c), the principle of indexing the data table what?
In the index on the field to narrow your search of the most frequently used.

In the index, the need to sort frequently used fields

d), under what circumstances should not be indexed?
For the columns in the query rarely involved in repeat or more of the value of the column, not indexed.

For some special data types, not indexed, such as a text field (text), etc.

 

 

 MySQL connection outer explained, the difference between self-connected connector

Let me talk about what is the cross-connect: cross-connect also known as Cartesian product, it means without any conditions, directly to all records and all records of a table to another table in eleven matches.

En is the only cross-connect conditions, screened matching records based on some conditions do not meet the conditions of the record does not appear in the result set, that is, the connecting line only connects the match.
As a result, the outer connection lines that focus not only meet the join condition, but also including the left table, right table or two tables
all rows of data, the three cases referred to turn left outer join, right outer join, and the whole outer joins.

Left outer join, also known as left connection, left the table the primary table, all records will be left of the table appear in the result set, for those records in the right table does not match, still have to show that those field values ​​corresponding to the right NULL to fill. Right outer join, also known as the right connection, the right table the primary table, all records in the right table in the result set. Left and right connections are interchangeable, MySQL currently does not support full outer joins.

 

char and varchar difference?


A: is a type of fixed length, varchar type is a variable length, the difference between them:  

char (M) types of data in a column, each value occupies M bytes, if M is less than a certain length, the MySQL will make up by a space character to its right. (Those filling out the space character in the search operation is to be removed) in varchar (M) types of data in a column, each value takes up only just enough bytes plus one byte for recording its length ( i.e., a total length of L + 1 byte).  

varchar get Applicable scene:

String columns have a maximum length of the string 2 is much larger than the average length rarely updated, prone to fragmentation 3. Multi-byte character sets stored string

Char was the scene:

    Store has an approximate length was (md5 value, ID, phone number), was relatively short length of string (varchar because of the need for additional space to record the string length), was better suited to regularly update string does not occur when the update was split page case, avoid fragmentation, better performance io

 

 

What methods have to talk SQL statement optimization? (Select few)


(1) Where clause: where the connection between the tables must be written before other Where conditions, those conditions can filter out the maximum number of records to be written at the end of last .HAVING Where clause.

(2) Alternatively EXISTS IN, NOT IN alternatively by NOT EXISTS.

(3) avoid the use of columns in the index calculation

(4) avoid the use of IS NULL and IS NOT NULL index columns

(5) the query optimization, should try to avoid full table scan, you should first consider indexing by the column involved in where and order.

(6) should be avoided to a null value is determined fields in the where clause, will cause the engine to give up using the index and a full table scan

(7) should be avoided for operating fields in the where clause expression, which will cause the engine to give up using the index and full table scan

 

1, the index

index is a table of contents, you can find the index position in the directory before looking for content, in order to quickly locate the query data. For the index, it will be saved in an additional file.
2, the index is a database designed to help users quickly find a data structure of the data. The dictionary-like directory, you can find content from a catalog when searching the dictionary data storage location, and direct access to.
1.1, select the type of index

    1, the smaller the data type is usually better to: the type of data typically smaller disks, memory, and CPU cache requires less space, are faster.

    2, simple data types better: integer data than character, processing overhead is smaller, because of the relatively more complicated strings.

    3, try to avoid NULL: to be designated as NOT nuLL, in MySQL, the column contains a null value is difficult to query optimization, because they make the index, the index statistics and the comparison operation is more complex
 

 

1.2 What scene does not create an index for

    the first, for those who rarely or columns referenced in the query should not create the index. This is because, since these columns rarely used, so there is no index or index does not improve query speed. On the contrary, due to the increase in the index, but reduces maintenance and increases the speed of the system space requirements.
    Second, for those few data values in a column index should not be increased. Because originally the result set is the equivalent of a full table queries, so there is no need. This is because the values of these columns is small, such as gender column personnel table, in the results of the query, the result set of data rows in a large proportion of rows of data tables that need to search for data in the table a large proportion of the line. Increase in the index, did not significantly speed up the retrieval speed.
    Third, for those defined as text, image and bit data types of columns should not increase the index. This is because the amount of data in those columns either a large or small value.
    Fourth, when modifying performance is far greater than the retrieval performance, should not create the index. This is because, retrieval performance and performance modifications are conflicting. When the increase in the index will increase the retrieval performance, but reduces performance modifications. When the reduction of the index will be modified to improve performance and reduce retrieval performance. Therefore, when modifying the performance is far greater than the retrieval performance, should not create the index.
    Fifth, do not appear in the fields where the conditions should not be indexed.

 

First by company_id, moneys order to create a composite index, as follows:

mysql> create index ind_sales2_companyid_moneys on sales2(company_id,moneys);
Query OK, 1000 rows affected (0.03 sec)
Records: 1000 Duplicates: 0 Warnings: 0
 

Then press company_id query a table, as follows:

mysql> explain select * from sales2 where company_id = 2006\G;
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: sales2
type: ref
possible_keys: ind_sales2_companyid_moneys
208key: ind_sales2_companyid_moneys
key_len: 5
ref: const
rows: 1
Extra: Using where
1 row in set (0.00 sec)
 

Company_id combination of conditions can be found even where the conditions of the moneys are not used, the index is still able to, which is the index of the prefix properties.

4, but only if the conditions according to moneys lookup table, then the index will not be used, as follows:

mysql> explain select * from sales2 where moneys = 1\G;
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: sales2
type: ALL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: 1000
Extra: Using where
1 row in set (0.00 sec)

3.2, for use like query back if it is constant and only% number is not the first character, the index may only be used:

1, the first example can be found without using the index, and the second example will be able to use the index
2, the difference lies in the different "%" position, the former "%" on the first place can not use the index, then those who did not on the first place on the use of the index.

mysql> explain select * from company2 where name like '%3'\G;
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: company2
type: ALL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: 1000
Extra: Using where
1 row in set (0.00 sec)
 
 
mysql> explain select * from company2 where name like '3%'\G;
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: company2
type: range
209possible_keys: ind_company2_name
key: ind_company2_name
key_len: 11
ref: NULL
rows: 103
Extra: Using where
1 row in set (0.00 sec)

3.3, if the column name, remember the name of the column is indexed, use column_name is null will use the index.

mysql> explain select * from company2 where name is null\G;
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: company2
type: ref
possible_keys: ind_company2_name
key: ind_company2_name
key_len: 11
ref: const
rows: 1
Extra: Using where
1 row in set (0.00 sec)

3.4 If you search on large text, full-text indexing without using like '% ...%'.

4, there is an index, but do not use

4.1 If MySQL estimated using the index more slowly than the full table scan, index no use. For example, if the column key_part1 uniformly distributed between 1 and 100, the following query is not a good use of the index:

 

 

SELECT * FROM table_name where key_part1 > 1 and key_part1 < 90;

4.3, with or separated condition, if the conditions listed before or in the index, while the back row is not an index, the index will not be related to the use, for example:, or both before and after the index can be It is used, and must be indexed separately.

mysql> show index from sales\G;
*************************** 1. row ***************************
Table: sales
Non_unique: 1
Key_name: ind_sales_year
Seq_in_index: 1
Column_name: year
210Collation: A
Cardinality: NULL
Sub_part: NULL
Packed: NULL
Null:
Index_type: BTREE
Comment:
1 row in set (0.00 sec)

4.4, if the column is passed ,, character is a number, not a 'not use the index

mysql> explain select * from company2 where name = 294\G;
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: company2
type: ALL
possible_keys: ind_company2_name
key: NULL
key_len: NULL
ref: NULL
rows: 1000
Extra: Using where
1 row in set (0.00 sec)
 
 
mysql> explain select * from company2 where name = '294'\G;
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: company2
type: ref
possible_keys: ind_company2_name
key: ind_company2_name
key_len: 23
ref: const
rows: 1
Extra: Using where
1 row in set (0.00 sec)

4.1.1.6, to create a full-text index

   Full-text index can be used for full-text search, but only MyISAM storage engine supports FULLTEXT indexes and only for CHAR, VARCHAR and TEXT columns services. The index is always for the entire column, does not support prefix index,

 

 

JOIN Usage

 

 

Guess you like

Origin www.cnblogs.com/wujifu/p/11576631.html