Perfomance index
I have class:
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name, code;
@Transient
public static final Attribute<Product, Boolean> IS_VISIBLE = attribute(Fields.visible, Product::isVisible);
@Transient
public static final Attribute<Product, String> PRODUCT_NAME = attribute(Fields.name, Product::getName);
@Transient
public static final Attribute<Product, String> PRODUCT_CODE = attribute(Fields.code, Product::getCode);
}
Query:
and(or(contains(PRODUCT_NAME, query.getKeyword()),
contains(PRODUCT_CODE, query.getKeyword())),
equal(IS_VISIBLE, true)
)
First time I index like this:
this.products = new TransactionalIndexedCollection<>(Product.class);
this.products.addIndex(NavigableIndex.onAttribute(IS_VISIBLE));
this.products.addIndex(SuffixTreeIndex.onAttribute(PRODUCT_NAME));
this.products.addIndex(SuffixTreeIndex.onAttribute(PRODUCT_CODE));
Second time I index like this:
this.products = new TransactionalIndexedCollection<>(Product.class);
this.products.addIndex(NavigableIndex.onAttribute(IS_VISIBLE));
this.products.addIndex(NavigableIndex.onAttribute(PRODUCT_NAME));
this.products.addIndex(NavigableIndex.onAttribute(PRODUCT_CODE));
Why first time faster than second time about 3-5 ms? Although query use contains method? I just spam about 10 -15 queries
If i understand your question correctly it's because the NavigableIndex does not support contains() queries, whereas the SuffixTreeIndex does. So you should expect better performance with SuffixTreeIndex for contains() queries.
Oh sorry, i mean NavigableIndex have better performance with SuffixTreeIndex with above Query sentence, while SuffixTreeIndex support contains() queries
One potential factor is how big is your dataset? Using an index can be more expensive than iteration if the dataset is small.
Another factor could be the microbenchmark setup. Could you share details of how you are running those tests? The overhead of class loading the bytecode for additional classes when you use additional indexes, and JIT compilation could be a factor as well.