0

我在 sql-server 中有两个表。

System{
  AUTO_ID           -- identity auto increment primary key
  SYSTEM_GUID       -- index created, unique key
}

File{
  AUTO_ID       -- identity auto increment primary key
  File_path
  System_Guid -- foreign key refers system.System_guid, index is created
              -- on this column
}

系统表有 100,000 行。文件表有 200,000 行。文件表只有 10 个不同的值System_guid

我的查询如下:

Select * from 
File left outer join System
on file.system_guid = system.system_guid

SQL 服务器正在使用哈希匹配连接来给我结果,这需要很长时间。

我想优化此查询以使其运行得更快。只有 10 个不同的 system_guid 的事实可能意味着哈希匹配会浪费能量。如何利用这些知识来加快查询速度?

4

2 回答 2

0

当索引列具有几乎不变的值时,索引的目的就失效了。如果您只想从 System_guid 是 File 中的其中之一的 System 中提取记录,那么您最好使用如下查询(在您的情况下):

select * from System 
where system_guid in (select distinct system_guid from File).
于 2013-08-06T17:16:01.413 回答
0

LEFT 连接真的有必要吗?查询如何作为内部联接执行?你得到一个不同的加入。

我怀疑哈希连接对于这么多的 I/O 来说是个很大的问题。

你可以做一个这样的 UNION ......也许可以哄出一个不同的计划。

Select * from File 
WHERE System_Guid NOT IN (SELECT system_guid from system)
union all
Select * from 
File inner join System
on file.system_guid = system.system_guid
于 2013-08-06T19:31:59.673 回答