You cannot make up for it ~ Some Optimization Measures of the count command in SQL (more than one million data is obvious)

Source: Internet
Author: User
In SQL, the count aggregate command is used to calculate the total number of table records. This command gives us the feeling that it is faster than the general query. However, when your data table has many records, for example, if there are millions or tens of millions of records, it is not so fast for count. We need to master some skills to optimize this count. Some people say: selectcount (1)

In SQL, the count aggregate command is used to calculate the total number of table records. This command gives us the feeling that it is faster than the general query. However, when your data table has many records, for example, if there are millions or tens of millions of records, it is not so fast for count. We need to master some skills to optimize this count. Some people say: select count (1)

In SQL, the count aggregate command is used to calculate the total number of table records. This command gives us the feeling that it is faster than the general query. However, when your data table has many records, for example, if there are millions or tens of millions of records, it is not so fast for count. We need to master some skills to optimize this count.

Some people say:

Select count (1) from table

Select count (primarykey) from table

It's faster. You must never use it.

Select count (*) from table

But what I want to say is that count (*) is faster. Why? What does count (*) mean? In fact, the true meaning of this field is to find an index field with the smallest space occupied, and then count it. Do not think it is "Big" as soon as you see *. In the count command, it refers to "any".

For a large table, if your field has a bit type, such as a gender field, which indicates a field with a true/false relationship, we need to add an index for it, our count speed is dozens of times faster, really, haha

First, add the index IsSync to the bit type field to add the clustered index.

If the data table is too large, run

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.