SQL statement optimization for java interview

  1. Establish appropriate indexes and make full use of indexes to avoid full table scans of tables with large data volumes

    Index is a conceptual part of the table, used to improve the efficiency of retrieving data. ORACLE uses a complex self-balancing B-tree structure. Generally, querying data through an index is faster than a full table scan. When ORACLE finds out, execute query and Update The ORACLE optimizer will use the index when the best path of the statement is. The same can improve efficiency when joining multiple tables. Another advantage of using an index is that it provides verification of the uniqueness of the primary key. In general, using indexes in large tables is particularly effective. Of course, you will also find that using indexes can also improve efficiency when scanning small tables. Although using indexes can improve query efficiency, we must also pay attention to its cost The index needs space for storage and regular maintenance. Whenever a record is added or decreased in the table or the index column is modified, the index itself will also be modified. This means that the INSERT, DELETE, and UPDATE of each record will do this Pay 4 or 5 times more disk I/O. Because indexes require additional storage space and processing, those unnecessary indexes will slow down query response time. Regular index reconstruction is necessary.

  2. Reasonable use of temporary tables

3**. Avoid writing complex SQL (which will reduce the speed of SQL queries), sometimes you can divide a few SQL to complete a problem**

  1. Reduce the granularity of transactions without affecting the business (granularity explanation: https://blog.csdn.net/weixin_40654252/article/details/85232914, https://blog.csdn.net/zdplife/article/details/ 48035837)

    The granularity of locking in Sql server2000 includes resources such as rows, pages, extents, tables, and libraries.

  2. IS NULL 与IS NOT NULL

    As long as any SQL statement adds is null or is not null after the where statement, the oracl optimizer will no longer use the index.

  3. Use statements with wildcards (%)

    Give two examples to illustrate the problem:

    Query the service number of phone_no with 10 in the ur_user_info table

    例子1:Select *from ur_user_info where phone_no like ‘%10%’;

    例子2:Select *from ur_user_info where phone_no like ‘10%’;

    Since the wildcard (%) appears at the beginning of the search term in Example 1, the oracle system does not use the phone_no index. The wildcard will reduce the efficiency of the query, but when the wildcard does not appear first, the index can be used again, as shown in Example 2.

  4. The connection order in the where clause

    Oracle's analysis is parsed from top to bottom, so the connection between tables must be written at the top of the where condition

  5. Use truncate instead of delete

    When deleting a table, use delete to perform an operation, and the rollback terminal is used to store recoverable information. When a transaction is not committed, a rollback transaction is executed, and the data will be restored to before the delete operation is executed. When using truncate, rollback The end will not store recoverable information, reducing resource calls.

  6. Replace the HAVING clause with the where clause

    Avoid using the HAVING clause, HAVING will only filter the result set after all the records have been retrieved. This processing requires sorting, totaling, etc. If you can limit the number of records through the WHERE clause, it can reduce this overhead .

  7. Reduce queries to tables

    When performing a where statement for table-connected sub-query, minimize the number of queries to the table

    Inefficient:

    Select tab_name from tables where tab_name = ( select

    tab_name from tab_columns where version = 604) and db_ver=

    ( select db_ver from tab_columns where version = 604)

    Efficient:

    select tab_name from tables where (tab_name,db_ver) =

    ( select tab_name,db_ver from tab_columns where version =604)

  8. Use in instead of or

    Inefficient

    Select… from location where loc_id = 10 or loc_id = 20 or loc_id = 30

    Efficient

    Select…from location where loc_in in (10,20,30);

  9. If the index is built on multiple columns, the optimizer will choose to use the index only when its first column (leading column) is referenced by the where clause. When only the second column of the index is referenced, optimize The server used a full table scan and ignored the index.

  10. Avoid using functions on index columns

  11. When comparing data of different data types, ORACLE automatically performs simple type conversion on the column. When performing the conversion, Oracle will not use the index

  12. Use >= instead of>

  13. Avoid using NOT commands by using >=, <=, etc.

    example:

    select * from employee where salary <> 3000;

    This query can be rewritten to not use NOT:

    select * from employee where salary<3000 or salary>3000;

    ​ Although the results of these two queries are the same, the second query plan will be faster than the first query plan. The second query allows Oracle to use an index on the salary column, while the first query cannot use an index.

Guess you like

Origin blog.csdn.net/weixin_39040527/article/details/105383436