Mysql Query optimization

Source: Internet
Author: User
Mysql Query: filter out the results of a certain field packageId (not the primary key) num, whether to use notin directly or filter out the result set after the query is completed. After the query, you need to traverse the result set anyway.

For mysql Query, filter out the results of a certain field packageId (not the primary key) = num, whether to directly use not in or filter out the results when the query is completed traversing the result set. After the query, you need to traverse the result set anyway.

Reply content:

For mysql Query, filter out the results of a certain field packageId (not the primary key) = num, whether to directly use not in or filter out the results when the query is completed traversing the result set. After the query, you need to traverse the result set anyway.

The subject added a question in the comment.

This is because the index is taken into consideration. <> this type of query condition does not go through the index, and the full table query speed will be slow. However, the data volume of this task is large. This function is used to send SMS reminders to some package users on the website. It is not equal to 7 because the package is free of charge and is out of consideration. Therefore, it must be removed.

For full table traversal of large data volumes, this full table fetch method is not available first. you can consider performing queries in the form of primary key id segmentation. for example, you first query 200 records with IDS> 0, then record the maximum id, and then query> 200 records with the current maximum id, and so on.

In combination with this id segmentation scenario, you can safely put your packageId into the query condition, because it must be the primary key id, so mysql filters up to 200 data records, so the problem is not big (200 is just an example. you can control it by yourself based on the actual situation and service pressure)

In addition, you can observe the progress of the current task in real time (you can create a log after sending 200 users, and you can also know where to start if the task is interrupted)

==== The following is the original answer ====

There is no paging for your query. Here we assume that your data volume is actually very small. then, because there is no paging, there will inevitably be a full table scan, which will be done directly during SQL queries. there is no big difference
In addition, it is simply not equal to a value.!=More intuitive

If your query involves a small amount of data or pages (the demo code is not written here ), therefore, you should consider whether there are other query conditions with indexes to help you query your data. if not, you need to consider whether to add an index for this packageId (if the distribution of packageId in this table is scattered enough)

select * from userPackage where packageId<>7

Direct use <>

First, faster.
Mysql Query speed is much faster than the traversal speed. The smaller the number of loops, the better.

It must be the first speed. never extract all the data... Mysql cannot stand

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.