Home Database Mysql Tutorial Summary of select and where clause optimization in mysql

Summary of select and where clause optimization in mysql

Jan 19, 2019 am 10:18 AM
mysql

This article brings you a summary of the optimization of select and where clauses in mysql. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Database optimization:

1. Optimization can be performed at the level of a single SQL statement, the entire application, a single database server, or multiple networked database servers
#2. Database performance depends on several factors at the database level, such as tables, queries and configuration settings
3. Optimize at the database level, optimize at the hardware level, balance portability and performance
4. Appropriate structure, appropriate data type; applications that perform frequent updates with a large number of tables (few columns); applications that analyze large amounts of data with a small number of tables (multiple columns); choose appropriate storage engines and indexes;
5. Compression is applicable Various workloads for InnoDB tables, as well as read-only MyISAM tables
6. Choose an appropriate locking strategy; the InnoDB storage engine can handle most locking issues
7. The main memory areas configured are the InnoDB buffer pool and MyISAM Key cache.
8. Optimize the select statement. This technique is also applicable to other delete statements with where. Set indexes on the columns of the where clause; indexes are especially important for referencing multiple columns such as joins and foreign keys.

select where clause optimization:

1. Adjust the structure of the query, such as function calls, only call them once for each row in the result set, and for each row in the table Called only once
2. Reduce the number of full table scans in queries
3. Regularly use the ANALYZE TABLE statement to keep table statistics up-to-date
4. Understand the tuning techniques of the storage engine specific to each table , indexing technology and configuration parameters
5. Optimize single-query transactions for InnoDB tables
6. Investigate the internal details of a specific query by reading the EXPLAIN plan and adjusting indexes, WHERE clauses, join clauses, etc.
7. Adjust the size and properties of the memory area used by MySQL for caching. By effectively using the InnoDB buffer pool, MyISAM key cache and MySQL query cache
8.where condition, remove unnecessary parentheses, constant folding, constant condition removal, reduce unnecessary logic
9.Used by the index Constant expressions are only calculated once
10.count(*) is queried directly from the table information; when there is only one table, the same is true for not null expressions
11.If you do not use GROUP BY or aggregate function (COUNT (), MIN (), etc.), HAVING will be merged with WHERE
12. Constant table, only one row or empty table; where clause acts on primary key or unique index
13. If ORDER BY and GROUP BY If all the columns in the clause come from the same table, the table will be preferred when connecting
14. If the order by clause and the group by clause are different, or come from different tables, a temporary table will be created
15. If you use the SQL_SMALL_RESULT modifier, MySQL will use an in-memory temporary table
16. MySQL can just read rows from the index without even consulting the data file
17. Before outputting each row, a jump will Pass rows that do not match the HAVING clause

The following table is used as a constant table:

1

2

3

SELECT * FROM t WHERE primary_key=1;

SELECT * FROM t1,t2 

WHERE t1.primary_key=1 AND t2.primary_key=t1.id;

Copy after login

The following query runs very fast:

1

2

3

4

5

6

7

8

SELECT COUNT(*) FROM tbl_name;

SELECT MIN(key_part1),MAX(key_part1) FROM tbl_name;

SELECT MAX(key_part2) FROM tbl_name

  WHERE key_part1=constant;

SELECT ... FROM tbl_name

  ORDER BY key_part1,key_part2,... LIMIT 10;

SELECT ... FROM tbl_name

  ORDER BY key_part1 DESC, key_part2 DESC, ... LIMIT 10;

Copy after login
Assuming that the index column is numeric, the following query Only the index tree is used:

1

2

3

4

SELECT key_part1,key_part2 FROM tbl_name WHERE key_part1=val;

SELECT COUNT(*) FROM tbl_name

  WHERE key_part1=val1 AND key_part2=val2;

  SELECT key_part2 FROM tbl_name GROUP BY key_part1;

Copy after login

The following query uses the index to retrieve data in sorted order, no separate sorting is required

1

2

3

4

SELECT ... FROM tbl_name

  ORDER BY key_part1,key_part2,... ;

SELECT ... FROM tbl_name

   ORDER BY key_part1 DESC, key_part2 DESC, ... ;

Copy after login

The above is the detailed content of Summary of select and where clause optimization in mysql. 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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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)

How to open phpmyadmin How to open phpmyadmin Apr 10, 2025 pm 10:51 PM

You can open phpMyAdmin through the following steps: 1. Log in to the website control panel; 2. Find and click the phpMyAdmin icon; 3. Enter MySQL credentials; 4. Click "Login".

MySQL: An Introduction to the World's Most Popular Database MySQL: An Introduction to the World's Most Popular Database Apr 12, 2025 am 12:18 AM

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

MySQL's Place: Databases and Programming MySQL's Place: Databases and Programming Apr 13, 2025 am 12:18 AM

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

Why Use MySQL? Benefits and Advantages Why Use MySQL? Benefits and Advantages Apr 12, 2025 am 12:17 AM

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

How to connect to the database of apache How to connect to the database of apache Apr 13, 2025 pm 01:03 PM

Apache connects to a database requires the following steps: Install the database driver. Configure the web.xml file to create a connection pool. Create a JDBC data source and specify the connection settings. Use the JDBC API to access the database from Java code, including getting connections, creating statements, binding parameters, executing queries or updates, and processing results.

How to start mysql by docker How to start mysql by docker Apr 15, 2025 pm 12:09 PM

The process of starting MySQL in Docker consists of the following steps: Pull the MySQL image to create and start the container, set the root user password, and map the port verification connection Create the database and the user grants all permissions to the database

Centos install mysql Centos install mysql Apr 14, 2025 pm 08:09 PM

Installing MySQL on CentOS involves the following steps: Adding the appropriate MySQL yum source. Execute the yum install mysql-server command to install the MySQL server. Use the mysql_secure_installation command to make security settings, such as setting the root user password. Customize the MySQL configuration file as needed. Tune MySQL parameters and optimize databases for performance.

How to install mysql in centos7 How to install mysql in centos7 Apr 14, 2025 pm 08:30 PM

The key to installing MySQL elegantly is to add the official MySQL repository. The specific steps are as follows: Download the MySQL official GPG key to prevent phishing attacks. Add MySQL repository file: rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm Update yum repository cache: yum update installation MySQL: yum install mysql-server startup MySQL service: systemctl start mysqld set up booting

See all articles