Java Interview Questions - Database

database related

  1. MySQL index usage

    By default there will be a primary key index.

    Index classification: single value index, composite index, unique index

    detailed article

  2. MySQL explain analysis

    MySQL explainanalyzes the execution plan of SQL through keywords. (Oracle passed EXPLAIN PLAN FOR sql)

    ID SELECT_TYPE TABLE PARTITIONS TYPE POSSIBLE_KEYS KEY KEY_LEN REF ROWS FILTERED EXTRA
    • ID: The execution order is from top to bottom, and the higher the ID value, the earlier the execution.
    • SELECT_TYPE: The type of query.
    • TABLE: The table that outputs the result set.
    • PARTITIONS: partitions hit by the partition table, and empty for non-partition tables.
    • TYPE : One of the important indicators, access type. Performance from good -> poor connection type: system -> const -> eq_ref -> ref -> ref_or_null -> index_merge -> index_subquery -> range -> index -> all.
    • POSSIBLE_KEYS: Indexes that may be used.
    • KEY: The index actually used.
    • KEY_LEN: The length of KEY, the shorter the better.
    • REF: Matching conditions for table joins. const: constant equivalent query; func: expression or function or internal implicit conversion.
    • ROWS: The number of scan rows. (estimated)
    • FILTERED: After the condition is filtered, compare the percentage of the total.
    • EXTRA: Description and description of the implementation.

    detailed article

  3. Talk about anti-pattern design

    The database paradigm can make the design of database tables more standardized, but it will lead to too many tables involved in the business model, and one query requires multiple associations, resulting in poor performance. For the protection of performance, an anti-pattern design is proposed. The core idea is: space for time , data redundancy to avoid problems caused by too many table associations.

  4. Talk about sub-database and sub-table design

    Generally, the table data is greater than 5 million rows or the capacity of a single table exceeds 2GB.

    Sub-database sub-table strategy (fragmentation strategy): 1. Date 2. Hash 3. Range 4. Tax rate

    Database middleware: MyCat, ShadingJDBC

    Query logic after sub-database sub-table:

    • Carry fragmentation fields: perform paging queries on specific tables.
    • Does not carry fragmentation fields: perform page-by-page query on each table, and then integrate the second page through the middleware and return to the client.
  5. Distributed dilemma and countermeasures brought by sub-database and sub-table

    The generated problems include: cross-database JOIN problem, sorting and paging problem, distributed ID problem

    Countermeasures for cross-database JOIN:

    1) Create a global table to store fields that all modules may depend on, but only fields that are rarely modified.

    2) Field redundancy design, that is, anti-pattern design

    Countermeasures for sorting and pagination:

    1) Increase PageSize

    2) Skip page query is prohibited, only the next page is allowed

    Distributed ID countermeasures:

    1)UUID

    2) Database self-increment

    3) Number segment mode

    4) Similar to snowflake algorithm

  6. Talk about the way of SQL optimization

    1) Locating slow SQL

    2) Analyze the execution plan

    3) Add indexes or modify SQL

    detailed article

  7. Deadlock problem encountered by MySQL

    1. The number of database connections I have experienced leads to production deadlocks

    The normal logic is: A requests B data -> requests C data -> releases the connection.

    The number of database connections is 8, which coincides with 8 requests occupying the number of connections, and the connection has not been released; at this time, new requests need to be connected, waiting for the resource to be released, but because the number of B->C connections is full, it cannot wait, resulting in a deadlock .

  8. Storage engines MyISAM and InnoDB

    MyISAM does not support transactions .

    Since most systems require database transaction support, MyISAM does not support transactions, so most of the existing storage engines are InnoDB.

    detailed article

  9. The principle of database indexing

    Mysql uses B+ tree as the data structure, stores the index data through the tree structure, and stores the index value in the leaf node.

  10. Why use B-trees

    The query efficiency is high.

  11. The difference between clustered index and non-clustered index

    Clustered index: There can only be one for a table. Occupies less space than nonclustered indexes.

  12. limit 20000 loading is very slow, how to deal with it

    The low performance of MySQL is because the database has to scan N + M records, and then discards the previous N records, which is very expensive.

    1. Optimize the cache

    2. Delayed association: first search the index field through limit, and then obtain the required data through the association between the original table and the index field

  13. Choose an Appropriate Distributed Primary Key Scheme

    Analyze according to each scenario:

    1)UUID

    Simple, too long, has no specific business meaning, and is not conducive to indexing .

    2) Database self-increment

    2.1 Single node: Create a table, insert one piece of data each time, and use the returned auto-increment primary key as the distributed primary key. A single node is at risk of downtime .

    2.2 Clusters: Set the initial value and step size of auto-increment for different clusters. Conflicts need to be considered during later expansion.

    3) Number segment mode

    The primary key ID of a batch number segment, apply again when it is used up.

    4) Similar to snowflake algorithm

    For 64-bit IDs, major manufacturers have their own open source frameworks, check them yourself.

Guess you like

Origin blog.csdn.net/qq_36986510/article/details/129004512