什么是“位图堆扫描”?在查询计划中?
我想知道“位图堆扫描”的原理,我知道这种情况经常发生 当我在条件中使用 OR
执行查询时。
谁能解释一下“位图堆扫描”背后的原理?
I want to know the principle of "Bitmap heap scan", I know this often happens
when I execute a query with OR
in the condition.
Who can explain the principle behind a "Bitmap heap scan"?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最好的解释来自算法的作者 Tom Lane。除非我弄错了。另请参阅维基百科文章。
简而言之,这有点像 seq 扫描。不同之处在于,位图索引不是访问每个磁盘页,而是将适用的索引与或或一起扫描,并且仅访问它需要的磁盘页。
这与索引扫描不同,索引扫描按顺序逐行访问索引,这意味着磁盘页面可能会被多次访问。
回复:您评论中的问题...是的,就是这样。
索引扫描将逐行扫描,一次又一次打开磁盘页面,根据需要多次(有些当然会保留在内存中,但您明白了)。
位图索引扫描将顺序打开磁盘页面的短列表,并抓取每个页面中的每个适用行(因此您在查询计划中看到所谓的重新检查条件)。
顺便说一句,请注意集群/行顺序如何影响任一方法的相关成本。如果行以随机顺序遍布各处,则位图索引会更便宜。 (事实上,如果它们真的遍布,那么顺序扫描将是最便宜的,因为位图索引扫描并非没有一些开销。)
The best explanation comes from Tom Lane, which is the algorithm's author unless I'm mistaking. See also the wikipedia article.
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.
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 all over the place, a seq scan will be cheapest, since a bitmap index scan is not without some overhead.)