gpt4 book ai didi

database - Hash Join 需要全表扫描

转载 作者:搜寻专家 更新时间:2023-10-30 20:32:23 25 4
gpt4 key购买 nike

所以,我想知道是否有必要在两个表之间进行哈希连接以对列进行全表扫描?

如果我想加入 COL1 和 COL2,并且 COL1 较小,它会在 COL1 中进行全面扫描,创建一个 HashMap ,然后使用 sabe 哈希函数在 COL2 中进行全面扫描。

这是正确的吗?

最佳答案

每个数据库都可以有自己的实际实现 Hash Join .但是我会说可能的方法类似于 this

The Hash Join algorithm builds an in-memory hash table of the smaller of its two inputs, and then reads the larger input and probes the in-memory hash table to find matches, which are written to a work table. If the smaller input does not fit into memory, the hash join operator partitions both inputs into smaller work tables. These smaller work tables are processed recursively until the smaller input fits into memory.

关于问题:需要对列进行全表扫描

我会说不,这还取决于数据库及其优化能力。如果查询中有足够的条件来限制任一表中的行,那么它将在使用哈希合并算法之前将这些行拉出。

当它为两个输入中较小的输入构建内存中哈希表时,它会使用最佳方法将这些行从表中拉出,这不一定是表扫描。如果您在查询中没有条件减少该表上的行,那么它会进行表扫描。

然后读取较大的输入并探查内存中的哈希表以查找匹配项时,它也会使用最佳方法提取这些行,这不一定是表扫描。

如果您的查询是:

SELECT
*
FROM BigTable
INNER JOIN LittleTable ON BigTable.Col=LittleTable.Col

并且使用散列连接,它很可能会通过表扫描从 LittleTable 在内存中创建一个散列表,然后表扫描 BigTable 检查这些散列键。

如果您的查询是:

SELECT
*
FROM BigTable
INNER JOIN LittleTable ON BigTable.Col=LittleTable.Col
WHERE LittleTable.Col2 >'2010/01/01' AND LittleTable.Col2<'2010/01/31'

并且使用散列连接,它很可能会从 LittleTable 在内存中创建一个散列表,但不使用表扫描(如果有要使用的索引),然后表扫描 BigTable 检查这些散列键。添加更多过滤器以更改删除 BigTable 上的表扫描。

关于database - Hash Join 需要全表扫描,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2604731/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com