Skip to content

Instantly share code, notes, and snippets.

@ptupitsyn
Last active October 3, 2022 18:27
Show Gist options
  • Save ptupitsyn/127a9cf8339e9bd592d95479ec1ce859 to your computer and use it in GitHub Desktop.
Save ptupitsyn/127a9cf8339e9bd592d95479ec1ce859 to your computer and use it in GitHub Desktop.
Apache Ignite.NET SQL indexes: compound, individual, parts of key and value
using Apache.Ignite.Core;
using Apache.Ignite.Core.Cache.Configuration;
using Apache.Ignite.Core.Cache.Query;
using var ignite = Ignition.Start();
var cacheCfg = new CacheConfiguration("people")
{
QueryEntities = new[]
{
new QueryEntity(keyType: typeof(PersonKey), valueType: typeof(Person))
}
};
var cache = ignite.GetOrCreateCache<PersonKey, Person>(cacheCfg);
var key = new PersonKey
{
Id = 1,
Company = "Acme"
};
var person = new Person
{
Name = "John",
Country = "Brazil",
Department = "Sales"
};
cache[key] = person;
// Uses individual index: "people".PERSON_COUNTRY_ASC_IDX
PrintResults(cache.Query(new SqlFieldsQuery("explain select name from person where country = ?", "Brazil")));
// Uses compound index: "people".PERSON_COMPANY_ASC_DEPARTMENT_ASC_IDX
PrintResults(cache.Query(new SqlFieldsQuery("explain select name from person where company = ?1 and department = ?2", "Acme", "Sales")));
void PrintResults(IFieldsQueryCursor cursor) =>
Console.WriteLine("\n>>> " + string.Join(Environment.NewLine, cursor.Select(row => string.Join(", ", row))));
public class PersonKey
{
public long Id { get; set; } // Not visible in SQL
[QuerySqlField(IsIndexed = true, IndexGroups = new[] { "IndexGroup1" })] // Visible in SQL, group index 1
public string? Company { get; set; }
}
public class Person
{
[QuerySqlField] // Visible in SQL, no index.
public string? Name { get; set; }
[QuerySqlField(IsIndexed = true, IndexGroups = new[] { "IndexGroup1" })] // Visible in SQL, group index 1
public string? Department { get; set; }
[QuerySqlField(IsIndexed = true)] // Visible in SQL, individual index.
public string? Country { get; set; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment