When specifying an index why the expression is used? #2145
-
Hi All! I was checking the ChooseIndex function and i noticed, that the expression is used when adding index by using either groupBy or OrderBy. (QueryOptimization.cs l. 282) BR, |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Hi @frenyibeni, The ideia behind expressions (created in v5) is create a generic way to create index based in any document field, method call or any transformation you need to be indexed. But in SQL, you create SELECTs and engine will choose best index to use. To select the best index, index name are not used because are just "the name of index" and not "what data are indexed". Like this: If you create an index like this
All index keys will contains only UPPER strings of Name field. If you try run a query like this:
Index (
Now, index expression are the same are query expression: In this code: // if no index found, try use same index in orderby/groupby/preferred
if (lowest == null && (_query.OrderBy != null || _query.GroupBy != null || preferred != null))
{
var index =
indexes.FirstOrDefault(x => x.Expression == _query.GroupBy?.Source) ??
indexes.FirstOrDefault(x => x.Expression == _query.OrderBy?.Source) ??
indexes.FirstOrDefault(x => x.Expression == preferred);
if (index != null)
{
lowest = new IndexCost(index);
}
} The variable And the last try is use
If I have an index in |
Beta Was this translation helpful? Give feedback.
Hi @frenyibeni,
The ideia behind expressions (created in v5) is create a generic way to create index based in any document field, method call or any transformation you need to be indexed. But in SQL, you create SELECTs and engine will choose best index to use. To select the best index, index name are not used because are just "the name of index" and not "what data are indexed". Like this:
If you create an index like this
engine.EnsureIndex("mycollection", "Name", "UPPER($.Name)"
All index keys will contains only UPPER strings of Name field. If you try run a query like this:
SELECT $ FROM mycollection WHERE $.Name = 'JOHN'
Index (
Name
) will not be used. Only if you runSELECT $ FROM mycoll…