SQL optimization --select

Optimization embodiment MYSQL: https://www.cnblogs.com/clsn/p/8214048.html

  1. The establishment of a field often used when querying the index, such as those containing more than you can build composite index, after the index point to note is that the format and design to ensure that the query field is the same, such as

    SELECT * FROM TABLE WHERE USER_ID = 12345

    Here USER_ID is of type NUMBER, and the actual design table if the type is VARCHAR when USER_ID, this index will be less than, resulting in a full table scan. Found query time is too long, you can use

    explain SELECT * FROM TABLE WHERE USER_ID = 12345

    View details

  2. When the table is too large, old data, historical data can be archived (provided these data did not have to), reducing the amount of data! !

  3. Frequently used queries, or more complex query (union query multiple tables) can create a view.

    View of the advantages and disadvantages:

    advantage:

    • [] Provide an abstract view of the table. You can easily add in the view / remove fields without modifying infrastructure.
    • [] Can be modeled easily view complex connection. .
    • [] View can hide specific content of the database for you. For example, if you need to use Oracles sys some checks _ context functions or many other things
    • [] You can easily view on the direct management of your authorization, not an actual table. If you know that a user can only access a view, easier to manage.
    • [] View can help you achieve backward compatibility. You can change the infrastructure, but the view can hide these facts to the end of a customer. (More common database application is the campus student table there classes, credits, tuition, fees and other data, but for the teacher's office in the applications, not let them see tuition fees, accounting fees and the like terms data, then do not give them access to this table, and give them a single crab over view)

    Disadvantages:

    • [] The loss of information about the relationship of (primary key, foreign key)
    • [] Unclear whether the insert / update view, because you hide the view of its underlying connection
  4. Try not free to determine the sentence, empty judgment will lead to a full table scan, to determine the empty case, consider creating a database default values ​​for this column.

  5. Do not try to equal conditions, so easily lead to a full table scan, try to use range queries.

  6. Make use exists instead of In, instead of or in connection with an external

  7. Do not try or conditions will result in a full table scan, you can query and then separate union all

  8. Sub-library sub-table

  9. Try not to use fuzzy queries left, will cause a full table scan

  10. In theory, as much as possible the use of multi-table queries (left), less use of subqueries

  11. WHERE clause join order. :
    ORACLE order of bottom-up parsing WHERE clause, according to this principle, those conditions can filter out the maximum number of records must be written at the end of the WHERE clause.

  12. Optimization group by, more efficient GROUP BY statement, you can filter out unwanted records before the GROUP BY.

Guess you like

Origin www.cnblogs.com/vitasoy/p/11873051.html