# Check-existing-DB-indices-on-globalid-for-Native-SACM-per-farm-and-add-missing-condition-to-ensure-the-indices-can-properly-work_688983295
## Background

The Native SACM feature will cause tons of the following SQL **when CMS notification load is heavy**:

```
SELECT SystemElement.entity_id AS "Id", SystemElement.schar0 AS "DisplayLabel", CAST(SystemElement.ssmallint0 AS SMALLINT ) AS "SubType", SystemElement.schar4 AS "GlobalId", SystemElement.data_domains AS "DataDomains", CAST(SystemElement.sint1 AS INTEGER) AS "BitPosition", CAST(SystemElement.sint2 AS INTEGER) AS "BitmapId", SystemElement.last_update_time AS "LastUpdateTime" FROM entities_xxxxxxxxx SystemElement WHERE ( (upper(SystemElement.schar4) = upper($1)) AND SystemElement.is_deleted = $2 ) AND SystemElement.entity_type_id = $3 ORDER BY SystemElement.entity_id ASC LIMIT $4
```

Due to historical reasons, some existing DB indices on globalid were created without the accurate condition ("is\_deleted=false") for the four Native SACM entities (Device, Actual Service, Service Component, and System Element), **similar SQLs including the above one will cause high CPU load on the DB server**.

## Reason

Due to historical reasons, the index on some old tenants doesn't have “is\_deleted=false” in the WHERE condition. On some newer tenants, the index's WHERE condition is fine.

## Solution

If the index is not created as expected, we would drop them and create the new ones. Please run the following steps for the whole farm.

1. Get the create INDEX SQLs by running the following SQL. If you get empty result, then you can ignore the next steps. If not, keep the CREATE INDEX SQLs, but don’t run the CREATE INDEX SQL at this step.
	```
	select indexdef||' AND is_deleted = false;'from pg_indexes where INDEXDEF LIKE '%WHERE%' AND indexdef not like '%WHERE%is_deleted%' AND tablename like 'entitie%' AND indexname like '%upper%globalid%';
	```
2. Get the ANALYZE SQLs.
	```
	select 'ANALYZE '||tablename||';' from pg_indexes where INDEXDEF LIKE '%WHERE%' AND indexdef not like '%WHERE%is_deleted%' AND tablename like 'entitie%' AND indexname like '%upper%device_globalid%';
	```
3. Run the following SQL to generate the DROP indexes SQL.
	```
	select 'DROP INDEX '||indexname||';'from pg_indexes where INDEXDEF LIKE '%WHERE%' AND indexdef not like '%WHERE%is_deleted%' AND tablename like 'entitie%' and indexname like '%upper%globalid%';
	```
4. Run the DROP INDEX SQLs you get from step3.
5. Run the CREATE INDEX SQLs you get from step1.
6. Run the ANALYZE TABLE SQLs you get from step2.
