mysql-- ten million Big Data SQL query optimization few experience

The main contents:

1: query optimization when or where clause Note

2: like statement to note when using

3: in the statement instead of the statement

4: Use index or create note

Suppose the user table has one million subscribers. Is the primary key is 1000000.num

1: query optimization, should try to avoid full table scans, you should first consider creating an index by the column involved in where and order.

Because: the index has a crucial impact on the speed of the query.

2: Try to avoid to a null value determined fields in the where clause. Otherwise it will cause the engine to give up using the index and full table scan.

For example: select id from user where num is null. num can be set this field to the default value 0. make sure the table is not a null value, then performing queries.

sql如下:select id from user where num=0;

(Consider the following case, assume a database table 10 6 records, DBMS page size is 4K, and store 100 records. Without an index, the query will scan the entire table, the worst case, if all the data page is not in memory, required reads 10 four pages, if the 10 four pages randomly distributed on the disk, required for 10 4 I / O, assuming each disk I / O time is 10ms (ignoring data transmission time) , the total of 100s (but actually a lot better). If the establishment of the B-Tree index, only need to be log100 (10 ^ 6) = 3 times a page read, time-consuming 30ms in the worst case. this is index bring results, a lot of times, when your application SQL query is slow, you should think about whether you can build the index)

3: should be avoided in the where clause = or <> symbols operation!. Otherwise the engine will abandon the use of the index, and then a full table scan.

4: should try to avoid or use to connect to the conditions in the where clause, or lead to give up using the index and full table scan. Or union can be used instead of a union all.

For example: select id from user where num = 10 or num = 20 This statement King num cause the engine to give up the index, but to a full table scan to be processed.

Or union can be used instead of a union all. as follows:

select id from user where num = 10;

union all

select id from user where num =20;

(Nuion all of the difference between union and not go into here)

5: in should be used with caution and not in, otherwise it will lead to a full table scan.

For continuous in an array, you can use between ... and. instead.

E.g:

select id from user where num in (1,2,3);

Like continuous can use between ... and ... instead of. as follows:

select id from user where num between 1 and 3;

6: like should be paid for

The following query will also lead to a full-table query:

select id from user where name like '%三';

If you want to improve efficiency, taking into account the full-text search. For example solr or luncene

And you use the following query to the index:

select id from user where name like '张%';

7: where clause parameter should pay attention to when

If you use a parameter in the where clause would lead to a full table scan. Because sql will only resolve local variables at runtime. But when the optimizer can not defer the choice of access plan to run; you must choose at compile time. If, however, establish access plan at compile time, a large value of the variable is unknown, and therefore can not be selected as an index entry.

Such as the following statement will perform full table scan:

select id from user where num = @num

Optimization, we know num is the primary key. It is the index.

So instead force the query can use the index:

select from user where (index (Index Name)) where num = @num id;

8: Try to avoid the operation of the fields in the where clause expression, which will cause the engine to give up using the index and full table scan.

例如:select id from user where num/2=100

Shall be amended as follows:

select id from user where num = 100*2;

9: Try to avoid love where clause of fields function operation, which will cause the engine to give up the index, and full-table scans.

E.g:

select id from user substring (name, 1,3) = 'abc', the meaning of the phrase is actually sql query name begins with abc user id

(Note: substring (field, start, end) of this function is taken mysql)

Shall be amended as follows:

select id from user where name like 'abc%';

10: Do not function in where clause "=" left, arithmetic operations, or other expressions, or the system may not work properly index

11: composite index query Note

As a condition of using the index field in time, if the index is a composite index, you must use the index to the first field as a condition of using this time to ensure the system so, otherwise the index will not be used, and should as far as possible let consistent field and indexed sequential order.

12: Do not write meaningless queries.

For example: the need to generate an empty table structure and configuration as the user table (Note: same table structure newly generated new table and the old table structure cousin)

select col1,col2,col3.....into newTable from user where 1=0

After executing the above line sql does not return any result set, but consumes system resources.

Shall be amended as follows:

create table newTable (....) this statement.

13: It is a good choice in many cases instead of using exists.

such as:

select num from user where num in(select num from newTable);

You can use the following statement instead:

select num from user a where exists(select num from newTable b where b.num = a.num );

14: Not all indexes are valid query, sql query optimization is based on data in the table, when the index lie (indexed fields) have a lot of duplicate data, sql queries may not go to use the index. If a table field sex, male, female almost each half. Even if the index is created on the sex of the query efficiency that much effect.

15: index creation should be noted

Not the index is created, the better. Although the index can improve the appropriate query efficiency, but also reduce the efficiency of insert and update. Because it is possible to insert or update the index when it will rebuild or modify the index. So we need to carefully consider how to create the index, as the case may be. A table so the number is best not more than six. If too much, you need to consider some of the less commonly used to create column indexes if necessary.

Reproduced in: https: //www.jianshu.com/p/d7f04786ac5e

Guess you like

Origin blog.csdn.net/weixin_34000916/article/details/91132931