Salesforce: Non-selective query简析

当构建queries, list views和reports时,最好创建selective的filter conditions.

只有在WHERE语句中仅有以下indexed fields时,才属于selective query:

Indexed Standard Fields:
1. Id
2. Name
3. OwnerId
4. CreatedDate
5. SystemModstamp
6. RecordType (indexed for all standard objects that feature it)
7. Master-detail Fields
8. Lookup fields

Other Indexed Fields:
1. Unique fields
2. External ID fields

如果Salesforce query optimizer识别出index可以提高频繁运行的queries的性能, 则默认情况下,不是indexed的fields可以自动indexed

另必要情况下,可以联系Salesforce Support为custom field添加index

某些field types不支持custom indexes, 包括multi-select picklists, multi-currency org中的currency fields, long text fields, 某些formula fields(non-deterministic formula field: 即引用了无法indexed的fields(例如multi-select picklist))以及binary fields

如果query result超过system-defined threshold, filter中使用negative operator或filter与空值进行比较(Name != ‘’), 则不能使用custom index

如果indexed custom field是empty或null, 则将使query变为non-selective

但是如果filter condition超过了Force.com query optimize的thresholds,那么它们也是unselective的,所以当查询的目标对象包含超过100万条记录时,选择性尤其重要

使用Standard Index时的threshold如下:

 

如果records的数量小于等于100万条,则threshold是总数的30%
如果records的数量小于等于200万条,则100万条以内的threshold为30万,100万条之外的threshold为15万,总threshold为45万
如果records的数量小于等于300万条,则100万条以内的threshold为30万,100万条之外的threshold为30万,总threshold为60万
.....
如果records的数量大于等于560万条,则100万条以内的threshold为30万,100万条之外的threshold为70万,总threshold为100万

使用Custom Index时的threshold如下:

如果是在trigger上运行query, 则如果超过20万条数据,可能就会引发System.QueryException: Non-selective query against large object type报错

如果在WHERE子句中存在AND,则每个filter的index selectivity thresholds都会翻一倍,但是最后查出来的总数据量不可超过总Threshold.

例如,第一个filter可查出50万数据,第二个filter可查出40万数据,它们都小于双倍的threshold:60万,所以不会出现问题。但是如果同时满足两个filter的数据量超过30万,则该query是unselective,反之为selective query.

如果在WHERE子句中存在OR,则每个filter查出来的数据都要低于对应的thresholds.

如果在WHERE子句中存在LIKE,且LIKE中的条件不是由leading wildcard开始,则不超过10万条数据为selective query.

不支持Index Selectivity的几种情况:

原文地址:https://www.cnblogs.com/clsriz/p/14481987.html