sql 查询优化非常重要的工作,就像必须要管理好数据库系统的任何其他组件一样。如果不对数据查询进行优化,数据库的性能将受到影响。在许多情况下,可能会阻止用户快速访问必要的信息。本文将讨论各种 sql 查询优化技术,这些技术可用于提高查询性能并降低成本。
照片出处 hiroshi kimura on unsplash
方法 1. 在 select 语句中使用列名(column name) 而不是 *
如果只想选择一定数量的列,则应在 select 语句中使用列名而不是 *。尽管这样写起来更简单,但数据库需要更多时间来处理查询。通过限制所选列数,可以减小结果表的大小、降低网络流量并提高整体查询性能。
例如:
original query :
select * from sales;
improved query :
select product_id from sales;
方法 2. 使用where来定义过滤器代替使用having
sql 优化查询将仅从数据库中检索必要的记录。根据 sql 操作顺序,having 语句是在 where 语句之后计算。如果目标是基于条件筛选查询,则 where 语句更有效。
例如:
original query:
select customer_id,count(customer_id)
from sales
group by customer_id
having customer_id != '16' and customer_id != '2';
improved query:
select customer_id,count(customer_id)
from sales
where customer_id != '16'
and customer_id !='2'
group by customer_id;
方法 3. 避免不必要的使用distinct
使用 distinct 语句是删除重复项的便捷方法。它的工作原理是在查询中创建组。但是,要实现这一目标,需要大量的计算能力。此外,数据可能在一定程度上被不准确地分类。m6米乐安卓版下载的解决方案是选择更多字段来生成不同的结果,而不是使用“select distinct”。
例如:
original query:
select distinct firstname, lastname, state
from teachers;
improved query
select firstname, lastname, address, state,coursename,timings
from teachers;
方法 4. 使用 join 代替subquery
与子查询相比,使用 join 的优点是它的执行速度更快。与子查询不同,子查询将执行所有查询并加载所有数据以执行处理,join允许数据库管理系统构建更适合的执行计划,并且可以预测应加载哪些数据进行处理以节省时间开支。
例如:
original query:
select *
from products p
where p.product_id =
(select s.product_id
from sales s
where s.customer_id = 2468
and s.quantity_sold = 12 );
improved query:
select p.*
from products p, sales s
where p.product_id = s.product_id
and s.customer_id = 2468
and s.quantity_sold = 12;
方法 5. 在查询索引列中使用 谓词in
对于索引检索,可以使用 in 列表谓词,并且优化程序可以对 in 列表进行排序以匹配索引的排序顺序,以便更有效地进行检索。请记住,in 列表只能包含常量,即在单个查询块执行期间保持不变的内容,如外部引用。
例如:
original query:
select *
from sales
where product_id = 4
or product_id = 7;
improved query:
select *
from sales
where product_id in (4, 7);
方法 6. 当使用涉及具有一对多关系的表的表联接时,请使用 exists 而不是 distinct。
distinct的工作原理是在查询中创建组,这需要大量的计算。您可以将子查询与 exists 关键字一起使用,以避免返回整个表。
例如:
original query:
select distinct c.country_id, c.country_name
from countries c, customers e
where e.country_id = c.country_id;
improved query:
select c.country_id, c.country_name
from countries c
where exists (select * from customers e
where e.country_id = c.country_id);
方法 7. 尽可能使用union all 而不是union
union all 的执行速度比union 快,因为在 union 中,无论重复项是否存在,都会被删除。“union all”显示带有重复项的数据。
例如:
original query:
select customer_id
from sales
union
select customer_id
from customers;
improved query:
select customer_id
from sales
union all
select customer_id
from customers;
方法 8. 避免在 join 查询中使用 or
如果在join查询时使用 or,则查询的速度会减慢 2 倍。
例如:
original query:
select *
from costs c
inner join products p on c.unit_price =
p.product_min_price or c.unit_price = p.product_list_price;
improved query:
select *
from costs c
inner join products p on c.unit_price =
p.product_min_price
union all
select *
from costs c
inner join products p on c.unit_price =
p.product_list_price;
方法 9. 避免在运算符右侧使用聚合函数
避免在运算符右侧使用聚合函数将极大地优化 sql 查询。
例如:
original query:
select *
from sales
where extract (year from to_date (time_id, ‘dd-
mon-yyyy’)) = 2021 and extract (month from
to_date (time_id, ‘dd-mon-yyyy’)) = 2002;
improved query:
select * from sales
where trunc (time_id) between
trunc(to_date(‘12/01/2001’, ’mm/dd/yyyy’)) and
trunc (to_date (‘12/30/2001’,’mm/dd/yyyy’));
结论
查询优化是dba、数据分析师和应用程序设计人员执行的常规操作,用于微调数据库系统的整体性能。遵循这些简单的方法将有助于优化 sql 查询,希望本文对您有所帮助。
原文标题:9 ways to optimize sql queries
原文作者:diksha mohnani
原文地址:https://medium.com/geekculture/9-ways-to-optimize-sql-queries-f62680d6f59a