Java Interview Questions on Optimization of MySQL Database

Optimization problems of mysql database:

1. Select the most applicable field attribute:

Mysql can well support the access of large amounts of data, but generally speaking, the smaller the database table, the faster the query executed on it. Therefore, when creating the table, in order to obtain better performance, we can set the width of the fields in the table as small as possible.

2. Use JOIN instead of Sub-Queries

MySQL has supported SQL subqueries since 4.1. This technique can use the SELECT statement to store the results of a singleton query between rows, and then use this result as a filter condition in another query. For example, if we want to delete customers who do not have any orders in the customer basic information table, we can use a subquery to first fetch all the customer IDs that issued orders from the sales information table, and then pass the results to the main query.

3. Use UNION instead of manually created temporary tables

MySQL has supported union queries since version 4.0. It can combine two or more select queries that require temporary tables into one query. At the end of the client's query session, the temporary table will be automatically deleted to ensure that the database is tidy and efficient. When using union to create a query, we only need to use union as a key to connect multiple select statements. Note that the number of fields in all select statements must be the same.

4. Affairs

Another important function of transaction is that when multiple users use the same data source at the same time, it can use the method of locking the database to provide users with a safe access method, so as to ensure that the user's operation is not interfered by other users. .

5. Lock the table

Although the transaction is a very good way to maintain the integrity of the database, but because of its exclusivity, sometimes affect the performance of the database, especially in a large application system. Since the database will be locked during the execution of the transaction, other user requests can only temporarily wait until the end of the transaction. If a database system is only used by a few users, the impact of transactions will not become a big problem; but if there are thousands of users accessing a database system at the same time, such as an e-commerce website, it will More serious response delay.

6. Use foreign keys

The method of locking the table can maintain the integrity of the data, but it cannot guarantee the relevance of the data. At this time we can use foreign keys.

7, use the index

Indexing is a common method to improve database performance. It can enable the database server to retrieve specific rows much faster than without an index, especially when the query contains MAX(), MIN() and ORDERBY commands. The performance improvement is more obvious.

8. Optimized query statement

In most cases, the use of indexes can improve the speed of queries, but if the SQL statement is not used properly, the index will not be able to play its due role.

Guess you like

Origin blog.csdn.net/weixin_44296929/article/details/108345168