使用两个布尔列在where条件中时出现速度较慢问题。

huangapple go评论54阅读模式
英文:

Slowness while using two boolean columns in the where condition

问题

创建表 IF NOT EXISTS schema1.table1
(
    ID bigint NOT NULL,
    eid bigint NOT NULL,
    sID bigint,
    oname character varying COLLATE pg_catalog."default" NOT NULL,
    obtype integer NOT NULL,
    parentid bigint,
    pageorder integer,
    obsubtype integer NOT NULL DEFAULT 0,
    pstate integer NOT NULL DEFAULT 1,
    lastmodifieddate timestamp without time zone,
    lastmodifiedbyid bigint,
    vstype integer,
    opath character varying COLLATE pg_catalog."default",
    arc boolean,
    folder boolean,
    fcategory bigint NOT NULL DEFAULT 915,
    imotype boolean,
    iostype boolean,
    isemailmsg boolean NOT NULL DEFAULT false,
    igtype boolean,
    extension character varying COLLATE pg_catalog."default",
    iher character varying COLLATE pg_catalog."default",
    realpathoffset integer,
    carvers0 bigint,
    carvers1 bigint,
    arctype boolean,
    icont boolean,
    email boolean,
    emailattach boolean,
    etype boolean,
    hofamilyid bigint,
    indstate integer NOT NULL DEFAULT 0,
    logicalsize bigint,
    addescription character varying COLLATE pg_catalog."default",
    ouid uuid DEFAULT uuid_generate_v1(),
    hperror boolean,
    pdescrip character varying COLLATE pg_catalog."default",
    CONSTRAINT pk_table1 PRIMARY KEY (ID),
    CONSTRAINT omp_o_omp_e FOREIGN KEY (eid)
        REFERENCES schema1.table2 (eid) MATCH SIMPLE
        ON UPDATE NO ACTION
        ON DELETE CASCADE
)
WITH (
    FILLFACTOR = 30,
    autovacuum_analyze_scale_factor = 0,
    autovacuum_analyze_threshold = 10000,
    autovacuum_vacuum_scale_factor = 0,
    autovacuum_vacuum_threshold = 10000
)
TABLESPACE schema1_ts;

ALTER TABLE IF EXISTS schema1.table1
    OWNER to schema1;

ALTER TABLE IF EXISTS schema1.table1
    ALTER COLUMN fcategory SET STATISTICS 10000;
英文:

CREATE TABLE IF NOT EXISTS schema1.table1
(
ID bigint NOT NULL,
eid bigint NOT NULL,
sID bigint,
oname character varying COLLATE pg_catalog."default" NOT NULL,
obtype integer NOT NULL,
parentid bigint,
pageorder integer,
obsubtype integer NOT NULL DEFAULT 0,
pstate integer NOT NULL DEFAULT 1,
lastmodifieddate timestamp without time zone,
lastmodifiedbyid bigint,
vstype integer,
opath character varying COLLATE pg_catalog."default",
arc boolean,
folder boolean,
fcategory bigint NOT NULL DEFAULT 915,
imotype boolean,
iostype boolean,
isemailmsg boolean NOT NULL DEFAULT false,
igtype boolean,
extension character varying COLLATE pg_catalog."default",
iher character varying COLLATE pg_catalog."default",
realpathoffset integer,
carvers0 bigint,
carvers1 bigint,
arctype boolean,
icont boolean,
email boolean,
emailattach boolean,
etype boolean,
hofamilyid bigint,
indstate integer NOT NULL DEFAULT 0,
logicalsize bigint,
addescription character varying COLLATE pg_catalog."default",
ouid uuid DEFAULT uuid_generate_v1(),
hperror boolean,
pdescrip character varying COLLATE pg_catalog."default",
CONSTRAINT pk_table1 PRIMARY KEY (ID),
CONSTRAINT omp_o_omp_e FOREIGN KEY (eid)
REFERENCES schema1.table2 (eid) MATCH SIMPLE
ON UPDATE NO ACTION
ON DELETE CASCADE
)

```WITH (```
```    FILLFACTOR = 30,```
```    autovacuum_analyze_scale_factor = 0,```
```    autovacuum_analyze_threshold = 10000,```
```    autovacuum_vacuum_scale_factor = 0,```
```    autovacuum_vacuum_threshold = 10000```
```)```
```TABLESPACE schema1_ts;```

ALTER TABLE IF EXISTS schema1.table1
OWNER to schema1;

```ALTER TABLE IF EXISTS schema1.table1```
```    ALTER COLUMN fcategory SET STATISTICS 10000;```

</details>


# 答案1
**得分**: 1

是的,您可以为这种情况创建复合索引或位图索引,甚至是覆盖索引。

1. 复合索引:

```sql
CREATE INDEX IF NOT EXISTS composite_index
ON schema1.table1 (folder, email);
  1. 位图索引:
CREATE BITMAP INDEX IF NOT EXISTS bitmap_index
ON schema1.table1 (folder, email);

在这种情况下,使用复合索引而不是位图索引有几个优点:

  1. 性能:复合索引通常比位图索引在多列查询时更高效。这是因为复合索引可以用于查找满足索引中所有列的条件的行。而位图索引只能用于查找匹配单个列条件的行。

  2. 空间:复合索引占用的空间比位图索引少。这是因为复合索引只存储索引中的列的值,而位图索引存储表中所有列的值。

  3. 使用方便:复合索引比位图索引更容易使用。这是因为复合索引可以与标准SQL查询一起使用,而位图索引需要特殊查询。

在您的情况下,由于您有一个包含1000万条记录的大表,而且两个布尔列都有50%的真和50%的假值,我建议使用复合索引。这将提高您的查询性能并使数据库更加高效。

然而,使用复合索引也有一些缺点:

  1. 复杂性:创建和维护复合索引可能比单列索引更复杂。
  2. 空间:复合索引可能占用比单列索引更多的空间。
英文:

Yes, you can create a composite or bitmap index or even a covering index for such scenarios.

  1. Composite Index:

    CREATE INDEX IF NOT EXISTS composite_index
    ON schema1.table1 (folder, email);

  2. Bitmap Index:

    CREATE BITMAP INDEX IF NOT EXISTS bitmap_index
    ON schema1.table1 (folder, email);

There are several advantages to using a composite index over a bitmap index in this scenario:

  1. Performance: Composite indexes are generally more efficient than bitmap indexes for multi-column queries. This is because composite indexes can be used to find rows that meet the criteria of all the columns in the index. Bitmap indexes, on the other hand, can only be used to find rows that match the criteria of a single column.

  2. Space: Composite indexes take up less space than bitmap indexes. This is because composite indexes only store the values of the columns in the index, whereas bitmap indexes store the values of all the columns in the table.

  3. Ease of use: Composite indexes are easier to use than bitmap indexes. This is because composite indexes can be used with standard SQL queries, whereas bitmap indexes require special queries.

  4. In your case, since you have a large table with 10 million records and the two boolean columns have 50% true and 50% false values, I would recommend using a composite index. This will improve the performance of your queries and make your database more efficient.

However, there are some disadvantages to using a composite index:

  1. Complexity: Composite indexes can be more complex to create and maintain than single-column indexes.
  2. Space: Composite indexes can take up more space than single-column indexes.

答案2

得分: 0

我可以看到在创建复合索引后查询性能有所改善。感谢您的帮助。

英文:

After creation of composite index I can see the difference in the query performance. Thanks for your assistance.

huangapple
  • 本文由 发表于 2023年5月26日 15:51:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76338735.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定