Master experience: Ways to improve MySQL performance (1) (2) _php tutorial

Source: Internet
Author: User
5. Not

We often use logical expressions in the WHERE clause when querying, such as greater than, less than, equal to, and not equal to, and can also use and (with), or (or), and not (non). Not can be used to negate any logical operation symbol. The following is an example of a NOT clause:

... where not (status = ' VALID ')

If you want to use not, you should precede the phrase with parentheses and precede the phrase with the NOT operator. The NOT operator is included in another logical operator, which is the not equal to (<>;) operator. In other words, the not is still in the operator, even if the not word is not explicitly added to the query where clause, see the following example:

.. where status <>; ' INVALID '

Let's look at the following example:

SELECT * FROM Employee where salary<>;3000;

For this query, it 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 scenario is faster than the first query scenario. The second query allows Oracle to use indexes on salary columns, while the first query cannot use indexes.

6. In and Exists

Sometimes a column is compared to a series of values. The simplest approach is to use subqueries in the WHERE clause. You can use a two-format subquery in the WHERE clause.

The first format is the use of the in operator:

... where column in (SELECT * from ...);

The second format is the use of the exist operator:

... where exists (select ' X ' from ...);

I believe most people will use the first format because it is easier to write, whereas the second format is much more efficient than the first. In Oracle, you can overwrite almost all in-operator queries to subqueries that use exists.

In the second format, the subquery begins with ' select ' X '. Use the EXISTS clause no matter what data the subquery extracts from the table it only looks at the WHERE clause. This way, the optimizer does not have to traverse the entire table and can do its work only by indexing (this assumes that the column used in the WHERE statement has an index). As opposed to the in clause, exists uses a concatenated subquery, which is more difficult to construct than in subqueries.

http://www.bkjia.com/PHPjc/631005.html www.bkjia.com true http://www.bkjia.com/PHPjc/631005.html techarticle 5. Not we often use some logical expressions in the WHERE clause when querying, such as greater than, less than, equal to, and not equal to, and can also use and (with), or (or), and not (non ...) .

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.