Home Database Mysql Tutorial Some common misunderstandings about Mysq

Some common misunderstandings about Mysq

Jun 20, 2017 pm 03:37 PM
Base

Common misunderstandings

    1. ##count(1) and count(primary_key) are better than count(*)

    In order to count the number of records, many people use count(1) and count(primary_key) instead of count(*). They think this performs better. In fact, this is a misunderstanding. For some scenarios, this may result in worse performance, because the database has made some special optimizations for the count(*) counting operation.
      1. count(column) and count(*) are the same

      This misunderstanding even exists among many senior engineers Or it is common among DBAs, and many people take it for granted. In fact, count(column) and count(*) are completely different operations and have completely different meanings.
      count(column) indicates how many records in the result set the column field is not empty
      count(*) indicates how many records there are in the entire result set
        1. Selecting a,b from... can allow the database to access less data than selecting a,b,c from...

        This misunderstanding is mainly It exists among a large number of developers. The main reason is that they don't know much about the storage principles of the database.
        In fact, most relational databases are stored in rows, and data access operations are based on a fixed-size IO unit (called block or page). Generally 4KB, 8KB... Most of the time, multiple rows are stored in each IO unit, and each row stores all the fields of the row (except for special types of fields such as lob).
        So, whether we take one field or multiple fields, the amount of data that the database needs to access in the table is actually the same.
        Of course, there are exceptions, that is, our query can be completed in the index. That is to say, when only two fields a and b are fetched, there is no need to return the table, and the field c is not In the index used, it is necessary to return to the table to obtain its data. In this case, the IO volume between the two will be quite different.
          1. order by must require a sorting operation

          We know that the index data is actually ordered, if our If the required data is in the same order as an index, and our query is executed through this index, the database will generally omit the sorting operation and return the data directly, because the database knows that the data already meets our sorting needs.
          In fact, using indexes to optimize SQL with sorting requirements is a very important optimization method
          Extended reading: Analysis of the implementation of ORDER BY in MySQL, the basic implementation principle of GROUP BY in MySQL and The basic implementation principle of MySQL DISTINCT has a more in-depth analysis in these three articles, especially the first one
            1. If there is filesort in the execution plan, the disk will be processed File sorting

            We can’t blame us for this misunderstanding, but it’s because of the wording problem used by MySQL developers. filesort is the information we may see displayed in the "Extra" column when we use the explain command to view the execution plan of a SQL.
            In fact, as long as a SQL statement requires a sorting operation, "Using filesort" will be displayed, which does not mean that there will be a file sorting operation.
            Extended reading: Understanding filesort in the MySQL Explain command output, I have a more detailed introduction here
            • Basic principles

              1. As few joins as possible

              The advantage of MySQL is simplicity, but this is actually its disadvantage in some aspects. The MySQL optimizer is highly efficient, but due to its limited amount of statistical information, there is more possibility of deviations in the optimizer's work process. For complex multi-table Join, on the one hand, due to its limited optimizer, and on the other hand, insufficient efforts have been made in Join, so the performance is still far behind that of relational database predecessors such as Oracle. But if it is a simple single-table query, this gap will be very small and even better than these database predecessors in some scenarios.
                1. Sort as little as possible

                Sorting operations will consume more CPU resources, so reducing sorting can reduce cache hits In scenarios with sufficient IO capabilities such as high rates, it will greatly affect the response time of SQL.
                For MySQL, there are many ways to reduce sorting, such as:
                • The misunderstanding mentioned above is to optimize by using index to sort

                • Reduce the number of records participating in sorting

                • Do not sort data unless necessary

                • Avoid using resource-consuming operations. SQL statements with DISTINCT, UNION, MINUS, INTERSECT, ORDER BY will start the SQL engine execution , a resource-intensive sorting (SORT) function. DISTINCT requires one sorting operation, while others require at least two sorting operations

                The above is the detailed content of Some common misunderstandings about Mysq. For more information, please follow other related articles on the PHP Chinese website!

                Statement of this Website
                The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

                Hot AI Tools

                Undresser.AI Undress

                Undresser.AI Undress

                AI-powered app for creating realistic nude photos

                AI Clothes Remover

                AI Clothes Remover

                Online AI tool for removing clothes from photos.

                Undress AI Tool

                Undress AI Tool

                Undress images for free

                Clothoff.io

                Clothoff.io

                AI clothes remover

                AI Hentai Generator

                AI Hentai Generator

                Generate AI Hentai for free.

                Hot Article

                R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
                2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
                Repo: How To Revive Teammates
                4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
                Hello Kitty Island Adventure: How To Get Giant Seeds
                4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

                Hot Tools

                Notepad++7.3.1

                Notepad++7.3.1

                Easy-to-use and free code editor

                SublimeText3 Chinese version

                SublimeText3 Chinese version

                Chinese version, very easy to use

                Zend Studio 13.0.1

                Zend Studio 13.0.1

                Powerful PHP integrated development environment

                Dreamweaver CS6

                Dreamweaver CS6

                Visual web development tools

                SublimeText3 Mac version

                SublimeText3 Mac version

                God-level code editing software (SublimeText3)

                PHP Basics Tutorial: From Beginner to Master PHP Basics Tutorial: From Beginner to Master Jun 18, 2023 am 09:43 AM

                PHP is a widely used open source server-side scripting language that can handle all tasks in web development. PHP is widely used in web development, especially for its excellent performance in dynamic data processing, so it is loved and used by many developers. In this article, we will explain the basics of PHP step by step to help beginners from getting started to becoming proficient. 1. Basic syntax PHP is an interpreted language whose code is similar to HTML, CSS and JavaScript. Every PHP statement ends with a semicolon; Note

                Learn the basics of Go language variables Learn the basics of Go language variables Mar 22, 2024 pm 09:39 PM

                Go language is a statically typed, compiled language developed by Google. Its concise and efficient features have attracted widespread attention and love from developers. In the process of learning the Go language, mastering the basic knowledge of variables is a crucial step. This article will explain basic knowledge such as the definition, assignment, and type inference of variables in the Go language through specific code examples to help readers better understand and master these knowledge points. In the Go language, you can use the keyword var to define a variable, which is the format of the var variable name variable type.

                Introduction to PHP Basics: How to use the echo function to output text content Introduction to PHP Basics: How to use the echo function to output text content Jul 30, 2023 pm 05:38 PM

                Basic introduction to PHP: How to use the echo function to output text content. In PHP programming, you often need to output some text content to a web page. In this case, you can use the echo function. This article will introduce how to use the echo function to output text content and provide some sample code. Before starting, first make sure you have installed PHP and configured the running environment. If PHP is not installed yet, you can download the latest stable version from the PHP official website (https://www.php.net).

                Detailed explanation of C language functions: basic to advanced, comprehensive analysis of the use of functions Detailed explanation of C language functions: basic to advanced, comprehensive analysis of the use of functions Feb 18, 2024 pm 02:25 PM

                C language function encyclopedia: from basic to advanced, detailed explanation of how to use functions, specific code examples are required Introduction: C language is a widely used programming language, and its powerful functions and flexibility make it the first choice of many developers. In C language, function is an important concept. It can combine a piece of code into an independent module, improving the reusability and maintainability of the code. This article will introduce the use of C language functions from the basics and gradually advance to help readers master the skills of function writing. 1. Definition and calling of functions in C

                Don't miss your chance to get your free Basic C# certification now from Microsoft Don't miss your chance to get your free Basic C# certification now from Microsoft Sep 01, 2023 pm 12:45 PM

                Calling all C# developers! Microsoft and non-profit organization freeCodeCamp announce the launch of a new global free Basic C# certification. This certification is designed to help developers of all levels learn the basics of C#, a popular programming language used to create a variety of applications, and you can display it in your LinkedIn profile. This certification includes 35 hours of Microsoft Learn training courses and an 80-question exam hosted on freeCodeCamp. This course covers topics such as variables, data types, control structures, and object-oriented programming. “Our Basic C# certification provides just that – proof of your ability to master this versatile

                PHP function usage: from basics to advanced PHP function usage: from basics to advanced Jun 15, 2023 pm 11:11 PM

                PHP is a widely used server-side scripting language used to develop dynamic websites, web applications, and other Internet services. In the process of developing PHP applications, using functions can help simplify code, improve code reusability, and reduce development costs. This article will introduce the basic usage and advanced usage of PHP functions. 1. Basic usage of PHP functions 1. Define functions In PHP, use the function keyword to define functions, for example: functiongreet($name){

                PHP study notes: basic syntax and variable definition PHP study notes: basic syntax and variable definition Oct 09, 2023 am 08:03 AM

                PHP study notes: basic syntax and variable definition In today's Internet era, PHP (Hypertext Preprocessor), as a widely used server scripting language, is favored by more and more developers. This article will introduce you to the basic syntax of PHP and the definition of variables, and provide specific code examples to help beginners better understand and master it. 1. Basic syntax of PHP Markings of PHP code In PHP code, we often use "<?php" and "?&

                Can I learn Linux from scratch? What do I need to learn? Can I learn Linux from scratch? What do I need to learn? Feb 19, 2024 pm 12:57 PM

                If you want to work in the IT industry, but do you want to learn programming, which technology should you choose? Of course it is Linux operation and maintenance. Linux is a very popular technology on the market, with a wide range of applications and good employment prospects, and is liked by many people. So the question is, can I learn Linux operation and maintenance with zero basic knowledge? In the server market, Linux system has a market share of up to 80% because of its advantages such as stability, security, free open source, efficiency and convenience. From this, it can be seen that Linux applications are very popular. Extensive. Whether now or in the future, learning Linux is a very good choice. As for whether it is possible to learn from scratch? My answer is of course. Oldboy Education Linux face-to-face class is specially designed for people with zero basic knowledge

                See all articles