JAVA interview database articles

Table of contents

1. Optimization

1. In MYSQL, how to locate slow queries?

2. The execution of the SQL statement is slow, how to analyze it?

3. Index

Know about indexing? (what is an index)

Do you understand the underlying data structure of the index?

What is the difference between B-tree and B+ tree?

What is a clustered index and what is a non-clustered index? / What is a clustered index and what is a secondary index (non-clustered index)? What is a return form?

Do you know what a covering index is?

How to deal with MYSQL super large paging?

What are the principles of index creation?

Under what circumstances will the index become invalid?


1. Optimization

1. In MYSQL, how to locate slow queries?

  • aggregation query

  • multi-table query

  • Table data volume is too large query

  • Deep paging query

Appearance: The page loads too slowly, and the response time of the interface pressure test is too long (more than 1s)

Option 1: Open source tools

  • Debugging tool: Arthas

  • Operation and maintenance tools: Prometheus, Skywalking (check the execution status and time of the interface)

Solution 2: MySQL comes with a slow log

The slow query log records all SQL statement logs executed beyond the specified parameter (long_querey_time, in seconds, default 10 seconds).

  • To enable the slow query log, you need to configure the following information in the MySQL configuration file (/etc/my.cnf):

#Enable slow query log query switch 
slow_query_log=1 
#Set the time of slow log to 2 seconds, if the SQL statement exceeds two seconds, the slow log will be recorded 
long_query_time=2

After the configuration is complete, restart the mysql server for testing, and you can check the statement execution information by querying the Localhost-slow.log file.

answer:

When testing the interface, it was found to be very slow, and the result of the pressure test was about 5 seconds;

Our system used an operation and maintenance tool (skywalking) at that time to detect which interface, and finally because it was a SQL problem;

The slow log query is enabled in Mysql, and the time we set is 2 seconds. Once the sql exceeds 2 seconds, it will be recorded in the log (debugging stage).

2. The execution of the SQL statement is slow, how to analyze it?

According to the above query methods, you can use the analysis tool DESC or EXPLAIN that comes with MySQL to analyze.

  • Check whether the index is hit (that is, whether the index itself is invalid) by key and key_len (that is, the index actually hit by sql and the size occupied by the index)

  • Use the type field to check whether SQL has further optimization space, whether there is full index scan or full disk scan (ie index and all)

  • Use the extra suggestion to judge whether there is a using index condition. If so, you can try to add an index or modify the returned field to reply.

3. Index

Know about indexing? (what is an index)

  • An index is an ordered data structure that helps MySQL efficiently retrieve data

  • Indexing can improve the efficiency of data retrieval and reduce the IO cost of the database (no full table scan is required)

  • Sort data through indexes, reduce the cost of data sorting, and reduce CPU consumption

Do you understand the underlying data structure of the index?

The MySQL search engine, that is, the InnoDB engine, adopts the B+ tree structure storage index by default.

  • On the one hand, it has more orders and shorter paths (B tree is 5 orders, and each node stores up to 4 Keys)

  • On the other hand, the disk read and write costs of the B+ tree are lower, non-leaf nodes only store pointers, and leaf nodes store data

  • Finally, the B+ tree is convenient for database scanning and interval query, and the leaf node is a doubly linked list

What is the difference between B-tree and B+ tree?

  • First: In the B-tree, both non-leaf nodes and leaf nodes will store data, while all the data of the B+ tree will appear in the leaf nodes. When querying, the search efficiency of the B+ tree is more stable

  • Second: B+ trees are more efficient when performing range queries, because B+ trees are stored in leaf nodes, and leaf nodes are a doubly linked list

What is a clustered index and what is a non-clustered index? / What is a clustered index and what is a secondary index (non-clustered index)? What is a return form?

  • The clustered index data and the index are put together, and the leaf nodes in the B+ tree store the data of the entire row. There is only one, which is generally the primary key of the table.

  • The secondary index is to store the data and the index separately, and the corresponding primary key is stored in the leaf node of the B+ tree, and there can be multiple

Returning to the table refers to finding the corresponding primary key through the secondary index, and then finding the entire row of data through the clustered index according to the primary key.

Do you know what a covering index is?

A covering index means that the returned columns are all included by the queried index.

  • For example, a typical query based on the primary key id is a covering index, and the leaf node contains the entire row of data

  • At the same time, we should avoid the need to create indexes for the returned columns, because this may trigger the table back and reduce efficiency, so we should avoid using select *

How to deal with MYSQL super large paging?

MySQL super-large pagination generally occurs when the limit statement is used to process and sort a large amount of data. The further to the back, the lower the query efficiency.

We can improve query efficiency by covering index + subquery.

The specific method is to use the id query as the result of the subquery first, and then join the query with the original table based on the id, because the id query is a covering index, so the efficiency will be much faster.

原语句:select * from user limit 9000000,10;
优化:selct * form user u,(select id from user order by id limit 9000000,10) t where u.id=t.id

What are the principles of index creation?

  • When the amount of data is relatively large and the table is frequently queried, it is suitable for indexing (more than 100,000)

  • Fields that are often used as query conditions, sorting, and grouping

  • Field content is highly differentiated

  • Long content, use prefix index

  • Use joint indexes as much as possible

  • Control the number of indexes

  • If the index column cannot store NULL values, please use the NOT NULL constraint when creating the table

Well, there are many situations like this, but there is a big premise, that is, we will create an index only if the data in the table exceeds 100,000, and the field to add the index is a field that is frequently queried, and it is generally used as a query condition. Sort fields or group by fields these. In addition, when we usually create an index, we use a composite index to create it. For the return value of an sgl, try to use a covering index. If the field is not highly differentiated, we will also put it in the field behind the composite index. . If the content of a certain field is long, we will consider using a prefix index. Of course, not all fields need to be indexed, and the number of this index should also be controlled, because adding an index will also slow down the speed of new additions and changes. .

Under what circumstances will the index become invalid?

There are many situations where the index fails. I can say some that I have encountered. Don’t open your mouth and have to say a bunch of memorized interview questions. Think about it properly, recall it, it’s more real)

When using a joint index

  1. Violation of the leftmost prefix rule

  2. Or use % at the beginning for fuzzy query

  3. Or the string does not add single quotes, and a type conversion occurs at this time

  4. Or perform operations on indexed columns, such as substring

  5. or in the column to the right of the range query

Well, this situation is quite common, I will talk about my own experience, I have encountered it before

For example, the index does not follow the leftmost matching rule when it is used. The second one is fuzzy query. If the % sign is in front, the index will also fail. If an operation or type conversion is performed on the field to which the index is added, the index will also be invalidated.

We have also encountered one before that if a composite index is used and a range query is used in the middle, the conditional index on the right will also fail. Therefore, under normal circumstances, if you want to determine whether this sql has an index failure, you can use explain execution plan to analyze

Guess you like

Origin blog.csdn.net/steven_bingo/article/details/132288037