使用 BETWEEN 条件对 INNER JOIN 进行性能调整

2023-09-17数据库问题
21

本文介绍了使用 BETWEEN 条件对 INNER JOIN 进行性能调整的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我有两个表,分别是 tbl_Smalltbl_Large.

I have two table's namely tbl_Small and tbl_Large.

我存储在 Microsoft Azure 中的表和从 Microsoft SQL Server 查询的表.

Both the table's I have stored in Microsoft Azure and querying from Microsoft SQL Server.

--表 1:Tbl_Small

--Table 1: Tbl_Small

CREATE TABLE tbl_Small
(
    cola int
);

INSERT INTO tbl_Small VALUES(1234),(123),(34); 
--1000 rows

--表2:tbl_Large

--Table 2: tbl_Large

CREATE TABLE tbl_Large
(
    ID bigint identity(1,1),
    cola int,
    colb int,
    colc varchar(100)
);

INSERT INTO tbl_Large(cola,colb,colc) VALUES(0,140,'A'),(150,200,'C'),(1000,15000,'D');
--30 million rows 

我想通过在条件之间加入小表来获取大表的详细信息.

I want to get large table details by joining small table with between condition.

我的尝试:

  1. 在 tbl_Small(cola) 上创建了 NONCLUSTERED 索引.
  2. 在 tbl_Large(cola) 和 tbl_Large(colb) 上创建了 NONCLUSTERED 索引.
  1. Created NONCLUSTERED index on tbl_Small(cola).
  2. Created NONCLUSTERED index on tbl_Large(cola) and tbl_Large(colb).

查询:

SELECT s.cola as [Input],l.cola,l.colb,l.colc
FROM tbl_Large AS l
INNER JOIN tbl_Small s ON s.cola BETWEEN l.cola and l.colb

注意:上述查询的执行时间超过 10 分钟.

Note: The above query's execution time is over 10 minutes.

编辑:按照回答所述在所有列上添加非聚集索引后,我得到了以下执行计划.

Edit: After adding nonclustered index on all columns as said in answer, I got the following execution plan.

执行时间:5分钟

DTU 百分比图:

推荐答案

您在 tbl_Large 上的索引需要覆盖,即它包含查询所需的所有数据.如果您只是在一列上创建索引,那么要获取所有数据,服务器将需要使用索引和另一个源来获取另一列数据.它很可能不会发现它值得额外的工作,并且会一起忽略索引.

Your index on tbl_Large needs to be covering i.e. it holds all the data the query needs. If you just create an index on the one column then to get all the data the server will need to use the index and another source to get the other column data. It's probable it won't find it worth the extra work and will ignore the index all together.

对于 tbl_Large,在 col a 和 col b 上创建索引,并包含 col c 的值,因此代码如下所示:

For tbl_Large create an index on both col a and col b and also include the value for col c so the code looks like this:

CREATE NONCLUSTERED INDEX IX_tbl_Large_cola_colb on tbl_Large (cola, colb)
INCLUDE (colc)

这篇关于使用 BETWEEN 条件对 INNER JOIN 进行性能调整的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

Mysql目录里的ibtmp1文件过大造成磁盘占满的解决办法
ibtmp1是非压缩的innodb临时表的独立表空间,通过innodb_temp_data_file_path参数指定文件的路径,文件名和大小,默认配置为ibtmp1:12M:autoextend,也就是说在文件系统磁盘足够的情况下,这个文件大小是可以无限增长的。 为了避免ibtmp1文件无止境的暴涨导致...
2025-01-02 数据库问题
151

按天分组的 SQL 查询
SQL query to group by day(按天分组的 SQL 查询)...
2024-04-16 数据库问题
77

SQL 子句“GROUP BY 1"是什么意思?意思是?
What does SQL clause quot;GROUP BY 1quot; mean?(SQL 子句“GROUP BY 1是什么意思?意思是?)...
2024-04-16 数据库问题
62

MySQL groupwise MAX() 返回意外结果
MySQL groupwise MAX() returns unexpected results(MySQL groupwise MAX() 返回意外结果)...
2024-04-16 数据库问题
13

MySQL SELECT 按组最频繁
MySQL SELECT most frequent by group(MySQL SELECT 按组最频繁)...
2024-04-16 数据库问题
16

在 Group By 查询中包含缺失的月份
Include missing months in Group By query(在 Group By 查询中包含缺失的月份)...
2024-04-16 数据库问题
12