MySQL interview other questions

How is a SQL statement executed in MySQL?

How is a SQL statement executed

What is a stored procedure? What are the advantages and disadvantages?

A stored procedure is a pre-compiled SQL statement, which has the advantage of allowing a modular design, that is, it only needs to be created once and can be called multiple times in the program later. If a certain operation needs to execute SQL multiple times, using stored procedures is faster than pure SQL statement execution.

advantage:

  • The stored procedure is pre-compiled, and the execution efficiency is high.

  • The code of the stored procedure is directly stored in the database and is directly called by the name of the stored procedure to reduce network communication.

  • High security, users with certain permissions are required to execute stored procedures.

  • Stored procedures can be reused, reducing the workload of database developers.

Disadvantages:

  • Debugging is troublesome, but debugging with PL/SQL Developer is very convenient! Make up for this shortcoming.

  • For porting issues, the database-side code is of course related to the database. But if it is an engineering project, there is basically no migration problem.

  • Recompilation problem, because the back-end code is compiled before running, if the object with the reference relationship changes, the affected stored procedures and packages will need to be recompiled (but it can also be set to automatically compile at runtime).

  • If a large number of stored procedures are used in a program system, the data structure will change with the increase of user demand when the program is delivered to use, and then there are related problems of the system. Finally, if the user wants to maintain the system, it can be said to be very It is difficult, and the price is unprecedented, and it is more troublesome to maintain.

What to do if the MySQL database cpu soars to 500%?

When the cpu soars to 500%, first use the operating system command top command to observe whether it is caused by mysqld occupancy. If not, find out the high occupancy process and perform related processing.

If it is caused by mysqld, show processlist and check the status of the session running inside to see if there is SQL running that consumes resources. Find out the SQL that consumes high, and see if the execution plan is accurate, if the index is missing, or if it is caused by too much data.

Generally speaking, these threads must be killed (while observing whether the cpu usage rate drops), and after corresponding adjustments (such as adding indexes, changing sql, and changing memory parameters), run these SQL again.

It is also possible that each SQL consumes not much resources, but suddenly, a large number of sessions are connected to cause a soaring cpu. In this case, you need to analyze with the application why the number of connections will increase sharply, and then make corresponding adjustments. For example, limit the number of connections, etc.

Explain what the pooling design idea is. What is a database connection pool? Why do you need a database connection pool?

Chihua design should not be a new term. Our common ones such as java thread pool, jdbc connection pool, redis connection pool, etc. are representative implementations of this type of design. This design will initially preset resources, and the problem solved is to offset the consumption of resources each time, such as the overhead of creating threads and the overhead of acquiring remote connections. It's like you go to a canteen to serve food. The aunt who serves a meal will first put several portions of the rice there. When you come, you can just add the food directly with the lunch box. There is no need to serve and serve the food temporarily, and the efficiency will be higher. In addition to initializing resources, the pooling design also includes the following characteristics: the initial value of the pool, the active value of the pool, the maximum value of the pool, etc. These characteristics can be directly mapped to the member properties of the java thread pool and the database connection pool. ——This article is a good introduction to the pooling design idea , just copy it directly to avoid re-creating wheels.

The database connection is essentially a socket connection. The database server also maintains some caches and user permission information, which occupies some memory. We can think of the database connection pool as a cache of maintained database connections, so that these connections can be reused when future requests to the database are needed. Opening and maintaining database connections for each user, especially requests for dynamic database-driven website applications, is expensive and wasteful of resources. In the connection pool, after creating a connection, place it in the pool and use it again, so there is no need to establish a new connection . If all connections are used, a new connection is established and added to the pool. The connection pool also reduces the time that users must wait to establish a connection to the database.

What should I do if the MySQL auto-increment primary key runs out?

[Original] What should I do if I run out of auto-increment primary keys?

What are the reasons for the slow execution of a SQL statement?

https://www.cnblogs.com/kubidemanong/p/10734045.html

Can MySQL set multiple primary keys when creating a table?

There can only be one primary key. However, if you want to use multiple unique indexes, you can have multiple ones. The role of the primary key is to ensure the uniqueness and integrity of the data, and at the same time through the primary key to retrieve the table can increase the retrieval speed. The so-called multiple primary keys of a table, we call it a joint primary key. Multiple columns can form a joint primary key, but there can only be one primary key!

Can MySQL not specify a primary key when creating a table?

If it is mysql, no primary key is defined, the system will automatically create a 6-digit hidden primary key rowId.

The life cycle of SQL?

  1. The application server establishes a connection with the database server

  2. The database process gets the request sql

  3. Analyze and generate an execution plan, execute

  4. Read data to memory and perform logic processing

  5. Send the result to the client through the connection in step one

  6. Close the connection and release resources
    Insert picture description here

Steps for Java to connect to MySQL?

  1. Load the driver
  2. Get database connection
  3. Create statement object
  4. Execute sql statement
  5. Get results
  6. Release the connection
public class DemoJdbc {
    
    
    public static void main(String[] args) {
    
    
        save();
    }

    public static void save() {
    
    
        String sql = "insert into t_student (name, age) values (?, ?)";
        Student stu = new Student("zhangsan", 15);
        Connection connection = null;
        Statement statement = null;
        try {
    
    
            // 1 加载驱动
            Class.forName("com.mysql.jdbc.Driver");
            // 2 获取数据库连接
            connection = DriverManager.getConnection("jdbc:mysql:///designmodel", "root", "root");
            // 3 创建语句对象
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setObject(1, stu.getName());
            ps.setObject(2, stu.getAge());
            // 4 执行sql语句
            ps.executeUpdate();
            // 5 释放连接
        } catch (Exception e) {
    
    
            e.printStackTrace();
        } finally {
    
    
            try {
    
    
                if (statement != null) {
    
    
                    statement.close();
                }
            } catch (SQLException e) {
    
    
                e.printStackTrace();
            } finally {
    
    
                try {
    
    
                    if (connection != null) {
    
    
                        connection.close();
                    }
                } catch (Exception e) {
    
    
                    e.printStackTrace();
                }
            }
        }
    }
}

Guess you like

Origin blog.csdn.net/weixin_44533129/article/details/112990125