SQL optimization methods: 1. Try to avoid using [select *], useless fields will reduce query efficiency; 2. Avoid using in and not in, you can choose between and exists instead; 3. Avoid using or , you can choose union instead.
SQL optimization method:
(recommended learning: mysql tutorial)
1. Create an index in the table, giving priority to fields used by where and group by.
2. Try to avoid using select *. Returning useless fields will reduce query efficiency. As follows:
SELECT * FROM t
Optimization method: use specific fields instead of *, and only return the used fields.
3. Try to avoid using in and not in, which will cause the database engine to abandon the index and perform a full table scan. As follows:
SELECT * FROM t WHERE id IN (2,3) SELECT * FROM t1 WHERE username IN (SELECT username FROM t2)
Optimization method: If it is a continuous value, it can be replaced by between. As follows:
SELECT * FROM t WHERE id BETWEEN 2 AND 3
If it is a subquery, it can be replaced by exists. As follows:
SELECT * FROM t1 WHERE EXISTS (SELECT * FROM t2 WHERE t1.username = t2.username)
4. Try to avoid using or, which will cause the database engine to abandon the index and perform a full table scan. As follows:
SELECT * FROM t WHERE id = 1 OR id = 3
Optimization method: Union can be used instead of or. As follows:
SELECT * FROM t WHERE id = 1 UNION SELECT * FROM t WHERE id = 3
(PS: If the fields on both sides of or are the same, as in the example. It seems that the two methods are about the same efficiency, even if union scans the index, or scans the entire table)
5. Try to avoid fuzzy queries at the beginning of fields, which will cause the database engine to abandon the index and perform a full table scan. As follows:
SELECT * FROM t WHERE username LIKE '%li%'
Optimization method: Try to use fuzzy query after the field. As follows:
SELECT * FROM t WHERE username LIKE 'li%'
6. Try to avoid judging null values, which will cause the database engine to abandon the index and perform a full table scan. As follows:
SELECT * FROM t WHERE score IS NULL
Optimization method: You can add a default value of 0 to the field and judge the 0 value. As follows:
SELECT * FROM t WHERE score = 0
7. Try to avoid performing expressions and function operations on the left side of the equal sign in the where condition, which will cause the database engine to abandon the index and perform a full table scan. As follows:
SELECT * FROM t2 WHERE score/10 = 9 SELECT * FROM t2 WHERE SUBSTR(username,1,2) = 'li'
Optimization method: Expressions and function operations can be moved to the right side of the equal sign. As follows:
SELECT * FROM t2 WHERE score = 10*9 SELECT * FROM t2 WHERE username LIKE 'li%'
8. When the amount of data is large, avoid using the condition where 1=1. Usually, in order to facilitate the assembly of query conditions, we will use this condition by default, and the database engine will abandon the index and perform a full table scan. As follows:
SELECT * FROM t WHERE 1=1
Optimization method: Use code to judge when assembling sql. If there is no where, add where, if there is where, add and.
The above is the detailed content of What are the methods for sql optimization?. For more information, please follow other related articles on the PHP Chinese website!

本篇文章给大家带来了关于SQL的相关知识,其中主要介绍了SQL Server使用CROSS APPLY与OUTER APPLY实现连接查询的方法,文中通过示例代码介绍的非常详细,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于SQL server的相关知识,其中主要介绍了SQL SERVER没有自带的解析json函数,需要自建一个函数(表值函数),下面介绍关于SQL Server解析/操作Json格式字段数据的相关资料,希望对大家有帮助。

如何优化sql中的orderBy语句?下面本篇文章给大家介绍一下优化sql中orderBy语句的方法,具有很好的参考价值,希望对大家有所帮助。

monacoeditor创建//创建和设置值if(!this.monacoEditor){this.monacoEditor=monaco.editor.create(this._node,{value:value||code,language:language,...options});this.monacoEditor.onDidChangeModelContent(e=>{constvalue=this.monacoEditor.getValue();//使value和其值保持一致i

本篇文章给大家带来了关于SQL server的相关知识,开窗函数也叫分析函数有两类,一类是聚合开窗函数,一类是排序开窗函数,下面这篇文章主要给大家介绍了关于SQL中开窗函数的相关资料,文中通过实例代码介绍的非常详细,需要的朋友可以参考下。

0x01前言概述小编又在MySQL中发现了一个Double型数据溢出。当我们拿到MySQL里的函数时,小编比较感兴趣的是其中的数学函数,它们也应该包含一些数据类型来保存数值。所以小编就跑去测试看哪些函数会出现溢出错误。然后小编发现,当传递一个大于709的值时,函数exp()就会引起一个溢出错误。mysql>selectexp(709);+-----------------------+|exp(709)|+-----------------------+|8.218407461554972

当某些sql因为不知名原因堵塞时,为了不影响后台服务运行,想要给sql增加执行时间限制,超时后就抛异常,保证后台线程不会因为sql堵塞而堵塞。一、yml全局配置单数据源可以,多数据源时会失效二、java配置类配置成功抛出超时异常。importcom.alibaba.druid.pool.DruidDataSource;importcom.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;importorg.apache.

monacoeditor创建//创建和设置值if(!this.monacoEditor){this.monacoEditor=monaco.editor.create(this._node,{value:value||code,language:language,...options});this.monacoEditor.onDidChangeModelContent(e=>{constvalue=this.monacoEditor.getValue();//使value和其值保持一致i


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version
Visual web development tools

Notepad++7.3.1
Easy-to-use and free code editor

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft
