Summary of methods to improve mysql count

Summary of methods to improve mysql count

I believe many programmers are familiar with MySQL. Many people are struggling with the usage of count and how to get the best query results. Let’s talk about some of my opinions today, for reference only.

1. Let's first build a table and prepare test data to facilitate subsequent testing steps

Take the InnoDB engine table as an example

The table creation statement is as follows

CREATE TABLE test.test

(

    a VARCHAR(50) NOT NULL COMMENT 'ddfdf',

    b VARCHAR(15) NOT NULL COMMENT 'fds',

    c VARCHAR(20) NOT NULL COMMENT 'asda',

    d VARCHAR(8) NOT NULL COMMENT 'ads',

    e longblob NOT NULL COMMENT 'asda',

    f VARCHAR(2000) COMMENT 'ads',

    g VARCHAR(8) NOT NULL COMMENT 'assd',

    h DATE NOT NULL COMMENT 'adsad',

    z VARCHAR(10) NOT NULL COMMENT 'adsd'

)

ENGINE=InnoDB DEFAULT CHARSET=utf8;

2. Log in to MySQL and change the database

Execute the table creation statement as shown below

3. Then prepare the test data and simply check whether there is data, as shown in the figure below

4. Next, start testing

In the absence of a where condition

Some people think count(*) is faster than count(field), while others think count(field) is faster than count(*)?

So which one is faster? Let's try it and see. Please see the picture below.

According to the results in the figure, count(field) is obviously faster.

5. What about the case where there is a where condition ? Which is faster, count(*) or count(field)?

Please see the execution effect in the figure below

count(*) is faster, but the data size may be too small to see obvious results.

6. Of course, you can analyze it by viewing the execution plan

Just add desc or explain before the executed SQL, as shown in the following figure

Finally, let's summarize 1. If there is no where condition, it is recommended to count(field) 2. If there is a where condition, it is recommended to count(*) Finally, let's summarize 1. If there is no where condition, it is recommended to count(field) 2. If there is a where condition, it is recommended to count(*)

You may also be interested in:
  • Optimized implementation of count() for large MySQL tables
  • Usage and performance optimization techniques of aggregate function count in MySQL
  • Sharing on count optimization issues in innodb in mysql
  • Let's talk about the performance of MySQL's COUNT(*)
  • Detailed explanation of the correct use of the count function in MySQL
  • A brief discussion on MySQL count of rows
  • Detailed explanation of count without filter conditions in MySQL
  • Summary of the differences between count(*), count(1) and count(col) in MySQL
  • Detailed explanation of mySQL count data examples in multiple tables
  • Use and optimization of MySQL COUNT function

<<:  WeChat Mini Program to Implement Electronic Signature

>>:  Docker Compose network settings explained

Recommend

Steps to build MHA architecture deployment in MySQL

Table of contents MAH 1. Introduction to MAH Arch...

A brief analysis of SQL examples for finding uncommitted transactions in MySQL

A long time ago, I summarized a blog post titled ...

Briefly understand the two common methods of creating files in Linux terminal

We all know that we can use the mkdir command to ...

VMware12.0 installation Ubuntu14.04 LTS tutorial

I have installed various images under virtual mac...

Solution to Docker pull timeout

Recently, Docker image pull is very unstable. It ...

Introduction to Sublime Text 2, a web front-end tool

Sublime Text 2 is a lightweight, simple, efficien...

jQuery implements the drop-down box for selecting the place of residence

The specific code for using jQuery to implement t...

Win2008 Server Security Check Steps Guide (Daily Maintenance Instructions)

The document has been written for a while, but I ...

Take you to a thorough understanding of the prototype object in JavaScript

Table of contents 1. What is a prototype? 1.1 Fun...

How to query a record in Mysql in which page of paging

Preface In practice, we may encounter such a prob...

Summary of MySQL's commonly used concatenation statements

Preface: In MySQL, the CONCAT() function is used ...

Detailed explanation of concat related functions in MySQL

1. concat() function Function: Concatenate multip...

React High-Order Component HOC Usage Summary

One sentence to introduce HOC What is a higher-or...

Use of Linux dynamic link library

Compared with ordinary programs, dynamic link lib...

The most comprehensive explanation of the locking mechanism in MySQL

Table of contents Preface Global Lock Full databa...