postgresql 查询计划中的“位图堆扫描”是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6592626/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 23:00:21  来源:igfitidea点击:

What is a "Bitmap heap scan" in a query plan?

postgresqlindexingsql-execution-plan

提问by francs

I want to know the principle of "Bitmap heap scan", I know this often happens when I execute a query with ORin the condition.

我想知道“位图堆扫描”的原理,我知道当我OR在条件中执行查询时经常发生这种情况。

Who can explain the principle behind a "Bitmap heap scan"?

谁能解释“位图堆扫描”背后的原理?

回答by Denis de Bernardy

The best explanation comes from Tom Lane, which is the algorithm's author unless I'm mistaking. See also the wikipedia article.

最好的解释来自Tom Lane,他是算法的作者,除非我弄错了。另请参阅维基百科文章

In short, it's a bit like a seq scan. The difference is that, rather than visiting every disk page, a bitmap index scan ANDs and ORs applicable indexes together, and only visits the disk pages that it needs to.

简而言之,有点像seq扫描。不同之处在于,位图索引不是访问每个磁盘页面,而是将适用索引 AND 和 OR 一起扫描,并且只访问它需要的磁盘页面。

This is different from an index scan, where the index is visited row by row in order -- meaning a disk page may get visited multiple times.

这与索引扫描不同,索引扫描是按行逐行访问的——这意味着一个磁盘页面可能会被多次访问。



Re: the question in your comment... Yep, that's exactly it.

回复:您评论中的问题......是的,就是这样。

An index scan will go through rows one by one, opening disk pages again and again, as many times as necessary (some will of course stay in memory, but you get the point).

索引扫描将逐行扫描,一次又一次地打开磁盘页面,根据需要多次(当然有些会留在内存中,但你明白了)。

A bitmap index scan will sequentially open a short-list of disk pages, and grab every applicable row in each one (hence the so-called recheck cond you see in query plans).

位图索引扫描将依次打开磁盘页面的短列表,并抓取每个页面中的每个适用行(因此,您在查询计划中看到所谓的重新检查条件)。

Note, as an aside, how clustering/row order affects the associated costs with either method. If rows are all over the place in a random order, a bitmap index will be cheaper. (And, in fact, if they're really allover the place, a seq scan will be cheapest, since a bitmap index scan is not without some overhead.)

请注意,顺便说一句,聚类/行顺序如何影响任一方法的相关成本。如果行以随机顺序到处都是,位图索引会更便宜。(而且,事实上,如果他们真的所有的地方,一个序列扫描将是最便宜的,因为一个位图索引扫描也不是没有一些开销。)