Optimization of database query speed index

The role of (a) an index of
the index in terms of the equivalent of the popular book directory, according to the time when we query terms, there is no index, we need a full table scan, a small amount of data can be, once more data than millions or even millions, a sql query execution often requires tens of seconds or more, more than five seconds on the already unbearable.

One direction improve query speed upgrade hardware (memory, cpu, hard drive), the second is optimization software (plus index, optimize sql).

(Ii) MySQL index type:
MySQL index five kinds: primary key index, the general index, a unique index, full-text index, the index of polymerization (multi-column index).

1) primary key index: the primary key index is added to the index on the primary key, set the primary key (primary key) when it is a special unique index, does not allow nulls. Generally in the construction of the table when at the same time create a primary key index;

2) general index: create indexes on non-primary key column, which is the most basic index;

3 unique index): The value of the index columns must be unique, but allow free value.

4) clustered index: create indexes on multiple columns.

Grammar (C) index:
view index a particular table: show index from table name;

Create a regular index: alter table table add index index name (indexed column) 

Create an aggregate index: alter table add index table index name (indexed column 1, column 2 indexed) 

Deleting a table of the index: drop index index name on table;

(D) on how to interview, said database index optimization capabilities through
three skill requirements databases, including:

  First, the basic CRUD, stored procedures and other skills, will use some group by, having, high point of the statement is distinct, exist, in, with, etc.

  Second, there is no experience design table,

  The third is also a key point in the database optimization, if you have relevant experience.

  The index is a database optimization tools necessary, ask the general question of the following two aspects:

① Index What is the cost? Under what scenario you need to build the index? Or conversely sometimes ask, do not recommend indexed under what scenario.

After ② built index, how to make the most efficient use of the index? Or vice versa asked, say that can not be effectively used to build the index case.

  From a structural point of view, is like a B-tree index (or also called B * B +), assuming the student name and ID list only two students, the student table 1000 students, student number from 1 to 1000, respectively, If the index for the ID, a configuration substantially as shown in FIG.

 

If we're looking for students with ID 111, the database system will take the index, according to the guidelines of the root node, you will find a second layer of a second data block from left to right, and so on, will be in the fourth floor ID 111 of a physical address, and find the data directly from the hard disk. Conversely, if not built Index, a database system may have to a large extent from locating in one by one to find, not so high efficiency.

At what cost?

1 index needs to occupy hard drive space, this is the price of space.
2 Once the new data is inserted, we need to re-build the index, which is the cost of time.
A scene, the data table is small (a few thousand lines), even without indexing, query return time is not long, then the significance of building the index would be small. "Cost" is not high.

Scene two, a merchandise table has millions of product information, and it will at one point in time, to which about one hundred thousand updates about the product information every day, and now with where specific merchandise inquiry statement (such as wherename = 'XXX ')Very slow. In order to improve query efficiency can be indexed, but when the data is updated daily, will rebuild the index, which is to be time-consuming. Then we need to consider, or even delete the index before the update, the update and then rebuild.

Scene three, can be seen from the figure above, since the ID is different in the data table value, the index can play a large role. Conversely, if a field repetition rate is high, such as gender field, or a field is empty most of the value (null), then the field is not recommended to build the index.

  Please keep in mind that there must be some business requirements will build the index. For example, in a commodity table, we often have to do a query based on name, if there is no index, query speed will be very slow, then we need to build the index. But in project development, if not always in accordance with product number inquiry, then there is no need to build an index number.

Way to do that?

① a statement: select name from merchandise table. We will not use the index, because there is no where clause.

② two statements: select * from goods table wherename = 'Java book', will use the index, if the project was frequently used to query the name, and the amount of data product table, and a repetition rate of name value is not high, it is recommended to build the index.

③ statement three: select * from when the product table where name like 'Java%' This is a fuzzy query will use the index, fuzzy queries like, if the first one is vague sign, such as where name like '% java ', then do not take the index in the query. In other cases, as long as% does not appear in the first position, you can use the index.

  Student achievement table has two fields: name and achievements. Now indexed on the results of the Integer type field.

① the first case, when a numeric field experience Nonequijoins operator, can not use the index.

select name from the table where student achievement scores> 95
, the event is greater than the symbol, you can not use the index, in order to use the index, we should change it SQL statement in the where clause:

where scores in (96,97,98,99,100)
② second case, if the index field value of the operation were some left, so can not use the index.

  Can be used to index the wording:

select name from the table where student achievement results = 60
  can not use the wording of the index:

select name from student achievement scores table where 40 = 100 +
③ the third case, if the index fields as a function of the operation, so can not use the index.

  For example, the SQL statement:

select * from merchandise table where substr (name) = 'J '
we want to query trade name initials J is a record, but once for the name using the function, even if there is an index on the name field can not be used.

 

(E) database optimization problem
(1) according to the service levels: mysql performance optimization configuration parameters;

(2) from the system level to enhance performance of mysql: optimize the data table structure, field types, field index, sub-table, sub-libraries, separate read and write and the like.

(3) enhance the level of performance from the database: SQL optimization, rational use of index fields.

(4) increasing the level of performance from the code: the use of cache and stored NoSQL databases, such as MongoDB / Memcached / Redis to relieve stress at high concurrent database queries.

(5) reduce the number of database operations, database access methods make use of batch driven.

(6) migrate infrequently used data backup, each time to avoid massive data retrieval.

(7) to enhance the database server hardware configuration or build a database cluster.

(8) program means to prevent SQL injection: the use of JDBC PreparedStatement bit insertion or inquiries; regular expression filtering (Filter string
unlawful); ----------------
Disclaimer: This article is CSDN bloggers original article "aogogogo", and follow CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
Original link: https: //blog.csdn.net/zhouboke/article/details/80414787

Guess you like

Origin www.cnblogs.com/justuntil/p/12174437.html