Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
518 views
in Technique[技术] by (71.8m points)

sql - Performance tuning on INNER JOIN with BETWEEN Condition

I have two table's namely tbl_Small and tbl_Large.

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

--Table 1: Tbl_Small

CREATE TABLE tbl_Small
(
    cola int
);

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

--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.

My try:

  1. Created NONCLUSTERED index on tbl_Small(cola).
  2. Created NONCLUSTERED index on tbl_Large(cola) and tbl_Large(colb).

Query:

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

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.

Time taken for execution: 5 min

enter image description here

DTU Percentage graph:

enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...