Using a function to return a list of Contacts based on a List of ID values
The parameter is a list of distinct integer values / contact IDs and size is varied from smaller numbers to bigger numbers [ usually in range of 1000 - 100000]
Approximately 500000 records in Contacts table and list is growing
At present this query is taking ~10 - 15 seconds for the completion
public void FetchFromIDList(List<int> contactIds)
{
try
{
IQueryable<Contact> query = context.Contacts
.Include(c => c.Company)
.Include(c => c.Addresses)
.ThenInclude(a => a.Country);
query = query.Where(c => contactIds.Contains(c.Id)).Take(10);
}
catch{}
}
Is there any way to improve perfomance of Contains ?
After some research i tries using Hashset , but not much difference on perfomace
//https://stackoverflow.com/questions/18651940/performance-benchmarking-of-contains-exists-and-any
HashSet<int> setIds = new HashSet<int>(contactIds);
Some articles mentioned about using temp table and insert to temp table and do a join , but i am wondering will it be extra works to insert first and then select and i am using Entity Framework
Is there any other approaches i can try ? I am using Server side pagination and usually page size is 10 and that is why using Take(10)