# Statistics of an Index

**URL:** <https://forum.yugabyte.com/t/statistics-of-an-index/2131>\
**Category:** General\
**Created:** [May 28, 2023, 12:58pm UTC](https://forum.yugabyte.com/t/statistics-of-an-index/2131 "2023-05-28T12:58:37Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Karthikeyan](https://avatars.discourse-cdn.com/v4/letter/k/898d66/32.png) [@Karthikeyan](https://forum.yugabyte.com/u/Karthikeyan)\
**Post date:** [May 28, 2023, 12:58pm UTC](https://forum.yugabyte.com/t/statistics-of-an-index/2131/1 "2023-05-28T12:58:37Z")

</div>

Can we update statistics of the an index manually? Or is there way to figure out recent when the statistics was updated?

I have a table in yugabyte fairly smaller in size 250k rows, but when i run my query its not picking the index instead it goes for seq scan, i have re-created same table, index copied data to another table and the same query on the new table picks up the proper index and goes for index scan.

Any help here would be much appreaciated

---

<div class="post-metadata">

**Author:** ![dorian\_yugabyte](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.yugabyte.com/dorian_yugabyte/32/206_2.png) [@dorian\_yugabyte](https://forum.yugabyte.com/u/dorian_yugabyte)\
**Post date:** [May 29, 2023, 7:14am UTC](https://forum.yugabyte.com/t/statistics-of-an-index/2131/2 "2023-05-29T07:14:57Z")

</div>

Hi @Karthikeyan

Please explain your table layout + indexes, and an “explain analyze” of your query?

---

<div class="post-metadata">

**Author:** ![Karthikeyan](https://avatars.discourse-cdn.com/v4/letter/k/898d66/32.png) [@Karthikeyan](https://forum.yugabyte.com/u/Karthikeyan)\
**Post date:** [May 29, 2023, 8:44am UTC](https://forum.yugabyte.com/t/statistics-of-an-index/2131/3 "2023-05-29T08:44:49Z")

</div>

@dorian_yugabyte Thanks for your response. Below is my schema structure

```sql
CREATE TABLE public.posts (
	id uuid NOT NULL,
	title varchar(255) NOT NULL,
	is_private bool NOT NULL,
	member_id uuid NOT NULL,
	association_id uuid NOT NULL,
	association_type public.enum_posts_association_type NOT NULL,
	is_active public.enum_posts_is_active NULL DEFAULT 'inactive'::enum_posts_is_active,
	is_deleted timestamptz NULL,
	created_at timestamptz NULL DEFAULT now(),
	updated_at timestamptz NULL DEFAULT now(),
	description text NULL,
	comment_count int8 NULL DEFAULT 0,
	reaction_count int8 NULL DEFAULT 0,
	external_links jsonb NULL DEFAULT '{}'::jsonb,
	CONSTRAINT posts_pkey PRIMARY KEY (id)
);
CREATE INDEX ix_posts_associationid ON public.posts USING lsm (association_id HASH) WHERE ((is_active = 'active'::enum_posts_is_active) AND (is_deleted IS NULL));

```

```sql
**explain**  **analyse**
 **select** * **FROM**"posts" **AS**"post"
**WHERE**"post"."is_deleted" **IS**  **null**
 **and**"post".is_active = 'active'
**AND**"post"."association_type" = 'organization'
**AND**"post"."association_id" = '1a3ae463-03c3-414a-b540-a3d480b0c167'

Seq Scan on posts post (cost=0.00..107.50 rows=1000 width=677) (actual time=21.506..2781.863 rows=5 loops=1)
  Filter: ((is_deleted IS NULL) AND (is_active = 'active'::enum_posts_is_active) AND (association_type = 'organization'::enum_posts_association_type) AND (association_id = '1a3ae463-03c3-414a-b540-a3d480b0c167'::uuid))
  Rows Removed by Filter: 269944
Planning Time: 0.050 ms
Execution Time: 2781.912 ms
Peak Memory Usage: 72 kB

```

I just copied the same data to another table with same schema structure and index using below statements

```sql
CREATE TABLE public.posts_tmp (
	id uuid NOT NULL,
	title varchar(255) NOT NULL,
	is_private bool NOT NULL,
	member_id uuid NOT NULL,
	association_id uuid NOT NULL,
	association_type public.enum_posts_association_type NOT NULL,
	is_active public.enum_posts_is_active NULL DEFAULT 'inactive'::enum_posts_is_active,
	is_deleted timestamptz NULL,
	created_at timestamptz NULL DEFAULT now(),
	updated_at timestamptz NULL DEFAULT now(),
	description text NULL,
	comment_count int8 NULL DEFAULT 0,
	reaction_count int8 NULL DEFAULT 0,
	external_links jsonb NULL DEFAULT '{}'::jsonb,
	CONSTRAINT posts_pkey_tmp PRIMARY KEY (id)
);
CREATE INDEX ix_posts_associationid_tmp ON public.posts_tmp USING lsm (association_id HASH) WHERE ((is_active = 'active'::enum_posts_is_active) AND (is_deleted IS NULL));

```

```sql
INSERT INTO posts_tmp
SELECT * FROM posts

```

Now with the new table it generated the better execution plan.

```sql
**explain**  **analyse**
 **select** * **FROM** posts_tmp "post"
**WHERE**"post"."is_deleted" **IS**  **null**
 **and**"post".is_active = 'active'
**AND**"post"."association_type" = 'organization'
**AND**"post"."association_id" = '1a3ae463-03c3-414a-b540-a3d480b0c167'

Index Scan using ix_posts_associationid_tmp on posts_tmp post (cost=0.00..5.04 rows=10 width=677) (actual time=1.463..1.484 rows=5 loops=1)
  Index Cond: (association_id = '1a3ae463-03c3-414a-b540-a3d480b0c167'::uuid)
  Filter: (association_type = 'organization'::enum_posts_association_type)
Planning Time: 0.073 ms
Execution Time: 1.532 ms
Peak Memory Usage: 8 kB

```

---

<div class="post-metadata">

**Author:** ![dorian\_yugabyte](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.yugabyte.com/dorian_yugabyte/32/206_2.png) [@dorian\_yugabyte](https://forum.yugabyte.com/u/dorian_yugabyte)\
**Post date:** [May 29, 2023, 8:50am UTC](https://forum.yugabyte.com/t/statistics-of-an-index/2131/4 "2023-05-29T08:50:53Z")

</div>

Can you try analyzing the table [ANALYZE statement [YSQL] | YugabyteDB Docs](https://docs.yugabyte.com/preview/api/ysql/the-sql-language/statements/cmd_analyze/) ?

---

<div class="post-metadata">

**Author:** ![Karthikeyan](https://avatars.discourse-cdn.com/v4/letter/k/898d66/32.png) [@Karthikeyan](https://forum.yugabyte.com/u/Karthikeyan)\
**Post date:** [May 29, 2023, 9:10am UTC](https://forum.yugabyte.com/t/statistics-of-an-index/2131/5 "2023-05-29T09:10:03Z")

</div>

Not much difference even after analysing the table.

 ![Screenshot 2023-05-29 at 14.39.21](https://canada1.discourse-cdn.com/flex027/uploads/yugabyte/original/1X/392c3852d8d1c1206bc6d6df969dfe211aa5d601.png)

---

<div class="post-metadata">

**Author:** ![FranckPachot](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.yugabyte.com/franckpachot/32/353_2.png) [@FranckPachot](https://forum.yugabyte.com/u/FranckPachot)\
**Post date:** [May 29, 2023, 9:52am UTC](https://forum.yugabyte.com/t/statistics-of-an-index/2131/6 "2023-05-29T09:52:06Z")

</div>

@Karthikeyan The best to understand a query planner decision is to show the cost of both plans and compare:

```auto
explain analyze
/*+ NoIndexScan(post) */
select * FROM "posts" AS "post"
WHERE "post"."is_deleted" IS null
and "post".is_active = 'active'
AND "post"."association_type" = 'organization'
AND "post"."association_id" = '1a3ae463-03c3-414a-b540-a3d480b0c167';

explain analyze
/*+ IndexScan(post) */
select * FROM "posts" AS "post"
WHERE "post"."is_deleted" IS null
and "post".is_active = 'active'
AND "post"."association_type" = 'organization'
AND "post"."association_id" = '1a3ae463-03c3-414a-b540-a3d480b0c167';

```

Note that using statistics by the planner is still in beta and you can enable with:

```auto
set yb_enable_optimizer_statistics=on

```

However, I don’t see how the same table with same data and same index and same query get a different plan. Are you sure the index is valid? you can see it with: `\d posts`

---

<div class="post-metadata">

**Author:** ![Karthikeyan](https://avatars.discourse-cdn.com/v4/letter/k/898d66/32.png) [@Karthikeyan](https://forum.yugabyte.com/u/Karthikeyan)\
**Post date:** [May 30, 2023, 4:27am UTC](https://forum.yugabyte.com/t/statistics-of-an-index/2131/7 "2023-05-30T04:27:01Z")

</div>

There seems to be difference in the cost on using IndexScan, but it not doing any indexScan, instead it goes for seqScan. I have tried enabling yb\_enable\_optimizer\_statistics and analysed the table posts but not much help, since it shows rows updated as **0**. I can solve this problem for now by copying over the data to a temp table and swap it but worried about the prod if it re-occurs again.

**explain**  **analyze**  
/\*+ NoIndexScan(post) \*/  
**select** \* **FROM** “posts” **AS** “post”  
**WHERE** “post”.“is\_deleted” **IS**  **null**  
**and** “post”.is\_active = ‘active’  
**AND** “post”.“association\_type” = ‘organization’  
**AND** “post”.“association\_id” = ‘1a3ae463-03c3-414a-b540-a3d480b0c167’;

**QUERY PLAN** |  
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+  
Seq Scan on posts post (cost=0.00…4743.06 rows=2 width=677) (actual time=21.334…2815.430 rows=5 loops=1) |  
Filter: ((is\_deleted IS NULL) AND (is\_active = ‘active’::enum\_posts\_is\_active) AND (association\_type = ‘organization’::enum\_posts\_association\_type) AND (association\_id = ‘1a3ae463-03c3-414a-b540-a3d480b0c167’::uuid))|  
Rows Removed by Filter: 269946 |  
Planning Time: 0.091 ms |  
Execution Time: 2815.532 ms |  
Peak Memory Usage: 147392 kB |

**explain**  **analyze**  
/\*+ IndexScan(post) \*/  
**select** \* **FROM** “posts” **AS** “post”  
**WHERE** “post”.“is\_deleted” **IS**  **null**  
**and** “post”.is\_active = ‘active’  
**AND** “post”.“association\_type” = ‘organization’  
**AND** “post”.“association\_id” = ‘1a3ae463-03c3-414a-b540-a3d480b0c167’;

QUERY PLAN |  
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+  
Seq Scan on posts post (cost=10000000000.00…10000004743.06 rows=2 width=677) (actual time=22.723…2829.749 rows=5 loops=1) |  
Filter: ((is\_deleted IS NULL) AND (is\_active = ‘active’::enum\_posts\_is\_active) AND (association\_type = ‘organization’::enum\_posts\_association\_type) AND (association\_id = ‘1a3ae463-03c3-414a-b540-a3d480b0c167’::uuid))|  
Rows Removed by Filter: 269946 |  
Planning Time: 0.094 ms |  
Execution Time: 2829.824 ms |  
Peak Memory Usage: 147656 kB |

---

<div class="post-metadata">

**Author:** ![Frits\_Hoogland](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.yugabyte.com/frits_hoogland/32/564_2.png) [@Frits\_Hoogland](https://forum.yugabyte.com/u/Frits_Hoogland)\
**Post date:** [May 30, 2023, 9:42am UTC](https://forum.yugabyte.com/t/statistics-of-an-index/2131/8 "2023-05-30T09:42:22Z")

</div>

Hi, can you please put the SQL and SQL output in a code block? A code block can be started and stopped with 3 backticks.

Something that we don’t understand is going on when a renamed version of the table with identical data and an identical index can use the index. If these are identical except for the name, they should perform the same plan.

Can you please show the description (`/d`) of both the tables?

Also, can you show the explain analyze output of the execution of the second table that does use the index?

---

<div class="post-metadata">

**Author:** ![FranckPachot](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.yugabyte.com/franckpachot/32/353_2.png) [@FranckPachot](https://forum.yugabyte.com/u/FranckPachot)\
**Post date:** [May 30, 2023, 12:39pm UTC](https://forum.yugabyte.com/t/statistics-of-an-index/2131/9 "2023-05-30T12:39:48Z")

</div>

> [@Karthikeyan](#):
>
> ```auto
> /*+ IndexScan(post) */
> ...
> Seq Scan on posts post (cost=10000000000.00…10000004743.06 rows=2 width=677)
> 
> ```

This shows that the index is not usable. Maybe not valid. This can happen if the create index fails. You can list invalid indexes with:

```auto
 select pg_get_indexdef(indexrelid, 0, true) from pg_index where not indisvalid;

```

---

<div class="post-metadata">

**Author:** ![Karthikeyan](https://avatars.discourse-cdn.com/v4/letter/k/898d66/32.png) [@Karthikeyan](https://forum.yugabyte.com/u/Karthikeyan)\
**Post date:** [June 6, 2023, 5:41am UTC](https://forum.yugabyte.com/t/statistics-of-an-index/2131/10 "2023-06-06T05:41:24Z")

</div>

@FranckPachot Thanks for your response, yes i could see that index was not valid. Any idea why schema definition still shows that index is part of the table but on the other side the query that you had provided shows index is invalid?

---

<div class="post-metadata">

**Author:** ![FranckPachot](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.yugabyte.com/franckpachot/32/353_2.png) [@FranckPachot](https://forum.yugabyte.com/u/FranckPachot)\
**Post date:** [June 6, 2023, 12:13pm UTC](https://forum.yugabyte.com/t/statistics-of-an-index/2131/11 "2023-06-06T12:13:01Z")

</div>

which schema definition?  
a \d in psql shows it. I’m not sure about DBeaver
