简体   繁体   English

Postgresql,使用带Between的索引

[英]Postgresql, Using Index with Between

I have index on a column, let's say ID (bigint).我在一列上有索引,比如说ID (bigint)。 If I have a query with something like this:如果我有这样的查询:

SELECT * 
  FROM table 
 WHERE id = 12345

...it will use index. ...它将使用索引。 But when I'm using query like...但是当我使用查询时...

SELECT * 
  FROM table 
 WHERE id >= 12345 
   AND id <= 12366

It use sequential scan, which is very slow.它使用顺序扫描,非常慢。 Can I force using the ID index?我可以强制使用 ID 索引吗?

It should use the index if the index type is btree and select doesn't fetch more then 30% of all record count (is it true in postgresql as well?) @scott-marlowe says that "..for PostgreSQL the switchover point comes much earlier, somewhere in the 1 to 10% range where it's cheaper to do a sequential scan..".如果索引类型是btree并且 select 没有获取超过所有记录数的 30%,它应该使用索引(在 postgresql 中也是如此吗?) @scott-marlowe 说“..for Z399BD391EE58972846更早的时候,在 1% 到 10% 的范围内,进行顺序扫描更便宜......”。

Try calling REINDEX action maybe?尝试调用 REINDEX 操作可能吗?

I don't use postgresql, but what you need to do is.我不使用postgresql,但你需要做的是。

  • Look at the query plan to confirm it's not using any index.查看查询计划以确认它没有使用任何索引。
  • In sybase, you can force a query to use a certain index using an "index hint"在 sybase 中,您可以使用“索引提示”强制查询使用某个索引

Looks like this question gives you exactly what you want.看起来这个问题给了你你想要的东西。 How do I force Postgres to use a particular index? 如何强制 Postgres 使用特定索引?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM