我有 2 个大表:Table_A(1000 万行)和 Table_B(700 万行)使用 3 个不同的列连接。我的 SELECT 语句如下:
SELECT Table_A.* , Table_B.*
FROM
Table_A
LEFT OUTER JOIN Table_B ON (Table_A.column_a4=Table_B.column_b4 AND Table_A.column_a5=Table_B.column_b5 AND Table_A.column_a6=Table_B.column_b6)
WHERE Table_A.column_a1=....
我目前在 Table_B 上有复合非集群索引:([column_b4],[column_b5],[column_b6])。3 列的组合是唯一的。
此连接非常慢(返回第一条记录需要 5 分钟以上)。请告知我应该为 LEFT OUTER JOIN 创建 Table_A 和 Table_B 上的哪个索引以提供最佳性能。如果我创建由 3 列组成的复合主键会更快吗?
谢谢。
CREATE TABLE Table_A
(
column_a1 [nvarchar] (20) NULL,
column_a2 [nvarchar] (20) NULL,
column_a3 [nvarchar] (20) NULL,
column_a4 [nvarchar] (20) NULL,
column_a5 [nvarchar] (20) NULL,
column_a6 [nvarchar] (20) NULL
)
CREATE INDEX IDX_a1a2a3 ON [Table_A] ([column_a1],[column_a2],[column_a3])
INSERT INTO Table_A values('1','1','1','1','1','1')
INSERT INTO Table_A values('2','2','2','2','2','2')
INSERT INTO Table_A values('3','3','3','3','3','3')
INSERT INTO Table_A values('4','4','4','4','4','4')
INSERT INTO Table_A values('5','5','5','5','5','5')
CREATE TABLE Table_B
(
column_b1 [nvarchar] (20) NULL,
column_b2 [nvarchar] (20) NULL,
column_b3 [nvarchar] (20) NULL,
column_b4 [nvarchar] (20) NOT NULL,
column_b5 [nvarchar] (20) NOT NULL,
column_b6 [nvarchar] (20) NOT NULL,
column_b7 [nvarchar] (20) NULL
)
INSERT INTO Table_B values('11','11','11','1','1','1','11')
INSERT INTO Table_B values('22','22','22','2','2','2','22')
INSERT INTO Table_B values('33','33','33','3','3','3','33')
INSERT INTO Table_B values('44','44','44','4','4','4','44')
INSERT INTO Table_B values('55','55','55','55','55','55','55')
CREATE INDEX IDX_b4b5b6 ON [Table_B] ([column_b4],[column_b5],[column_b6])