-1

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;```
Progman
  • 16,827
  • 6
  • 33
  • 48
jptech84
  • 1
  • 3
  • 1
    Please update the question with the results of `EXPLAIN ANALYZE` for the query. Without adequate details, it's impossible for the community to adequately address questions. Although a row count and table size are given, execution time is not, so we're not able to assess if the query is completing in a reasonable time. Has `ANALYZE` been in the table? – JohnH May 26 '23 at 08:17
  • @JohnH, i'll share shortly – jptech84 May 26 '23 at 08:53
  • @JohnH, please find below – jptech84 May 26 '23 at 09:01
  • SELECT count(ID) AS CO_ID_Count FROM schema1.table1 CO where Folder=false AND Email=false """Finalize Aggregate (cost=21293487.81..21293487.82 rows=1 width=8)"" "" -> Gather (cost=21293487.39..21293487.80 rows=4 width=8)"" "" Workers Planned: 4"" "" -> Partial Aggregate (cost=21292487.39..21292487.40 rows=1 width=8)"" "" -> Parallel Seq Scan on table1 co (cost=0.00..21275201.53 rows=6914343 width=8)"" "" Filter: ((NOT folder) AND (NOT email))""" – jptech84 May 26 '23 at 09:01
  • That appears to be the output from `EXPLAIN`, which only informs about the optimizer's estimate. **Please update the question with the results of `EXPLAIN ANALYIZE`, which will include the actual counts and times.** Don't post updates to the question in comments, **update the question**. – JohnH May 26 '23 at 10:35
  • I've updated the explain plan – jptech84 May 26 '23 at 11:20
  • You've replaced the question with just the explain plan. Please edit the question so that there is a question; table DDL, including indexes; the actual query; and the results of `EXPLAIN ANALYZE`. From the explain plan, it is obvious that no indexes are being used. – JohnH May 26 '23 at 11:54
  • Unable to upload the table DDL here, can you help ? – jptech84 May 26 '23 at 12:58
  • If you are using pgAdmin, then navigate to the table and go to the SQL tab to see the DDL. Select the text and paste it into the Stack Overflow edit window. Place a line with only `\`\`\`` before and after the pasted text. – JohnH May 26 '23 at 13:20
  • @JohnH, updated table DDL – jptech84 May 27 '23 at 03:53

2 Answers2

1

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.
Navitas28
  • 745
  • 4
  • 13
  • Thanks @Navitas28. Let me create and keep you update the status. – jptech84 May 26 '23 at 08:50
  • please find below. I can see still it is using seq scan. Here i've described about the Index creation script and the explain plan – jptech84 May 26 '23 at 09:57
  • CREATE INDEX IF NOT EXISTS folder_email_composite_index ON schema1.table1 (folder, email); "Finalize Aggregate (cost=22530634.52..22530634.53 rows=1 width=8)" " -> Gather (cost=22530634.00..22530634.51 rows=5 width=8)" " Workers Planned: 5" " -> Partial Aggregate (cost=22529634.00..22529634.01 rows=1 width=8)" " -> Parallel Seq Scan on table1 co (cost=0.00..22515783.42 rows=5540230 width=8)" " Filter: ((NOT folder) AND (NOT email))" – jptech84 May 26 '23 at 09:57
  • When using true in the where clause it is using our newly created index – jptech84 May 26 '23 at 10:07
  • "Aggregate (cost=1565435.12..1565435.13 rows=1 width=8)" " -> Index Scan using folder_email_composite_index on table1 co (cost=0.56..1563702.93 rows=692876 width=8)" " Index Cond: ((folder = true) AND (email = true))" – jptech84 May 26 '23 at 10:08
  • Please elaborate , what problem are you facing after making composite index – Navitas28 May 26 '23 at 12:56
  • Actually my requirement is whatever the condition in the query, it should get completed within 3 secs. – jptech84 May 26 '23 at 14:34
  • Due to 70% of records are with false flag it is taking too much time. Created composite indexes, created new column with integer datatype and replaced the records as 1 & 2 for true & false. Eventhough we didn't reach our expected result, i.e., result should come within 3 secs. – jptech84 May 27 '23 at 03:58
  • after creation of composite index I can see the difference in the query performance. Thanks for your assistance. – jptech84 May 29 '23 at 11:14
0

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

jptech84
  • 1
  • 3
  • 1
    please post comments on the answers that helped you, rather than posting new answers. if the issue is resolved, mark the answer as correct – Levi May 29 '23 at 13:25