Click here to Skip to main content
15,886,689 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a table which logs the user logins and the application from which user has logged.

| S.No | UserID | Application | DateTime |
-------------------------------------------

This table has more than 1 million records.

I am querying the list of records using LINQ based on list of userid and application below.

List<string> relyingParties = new List<string>(){"AAA","BBB","CCC","DDD"};

//Filtering the users by UserID and Application
var userLogActionList = dbContext.LogUserAction.Where(x => userIDList.Contains((x.UserID.Value)) && relyingParties.Contains(x.ApplicationLoggedIn)).ToList());

The avg time it takes to fetch from DB takes more than 15 seconds for a input of 100 userids in userIDList which is very bad i feel.

I want to have a better performance for querying and fetching the data. I came across Indexing while searching across the web.

I am very new to indexing concept. Since I have a PK in the table already it has a clustered index so i was not able to create another clustered index in the table.
I have implemented a non clustered index to column UserID.


IX_LogUserAction_UserID


If i run a SQL query in SQL server,

SELECT  *
  FROM [XYZDB].[dbo].[LogUserAction] 
  WITH (INDEX([IX_LogUserAction_UserID]))
  where UserID='0cef394e-06ac-49cf-8c62-2a37299913e1'


I can see the execution path plan for the seek and Table Scan(when no indexing is there).

As far as my knowledge, if indexing is applied to a column it will increase the query performance.

But if I run the same Linq code above, where i try fetch records the performance is still the same(Around 15 seconds).

Can someone help me in detailing, what i am doing wrong or where am i going wrong?

Your response is highly appreciated.

What I have tried:

List<string> relyingParties = new List<string>(){"AAA","BBB","CCC","DDD"};

//Filtering the users by UserID and Application
var userLogActionList = dbContext.LogUserAction.Where(x => userIDList.Contains((x.UserID.Value)) && relyingParties.Contains(x.ApplicationLoggedIn)).ToList());



SELECT  *
  FROM [XYZDB].[dbo].[LogUserAction] 
  WITH (INDEX([IX_LogUserAction_UserID]))
  where UserID='0cef394e-06ac-49cf-8c62-2a37299913e1'
Posted
Comments
F-ES Sitecore 23-Mar-17 7:58am    
You might want to convert this to a stored procedure so you can do things like CTE statements etc. You might get the performance you're after if you create a table variable that contains the ids you're looking for and do a JOIN on that table with the LogUserAction table. If you use LINQ you leave it up to LINQ to decide the SQL and it is probably using an "IN" to do the search and that is always going to have poor performance.
Member 1097736 23-Mar-17 9:48am    
Thanks for the response. May i know is there any other way to improve query performance and decrease time ?
Richard Deeming 23-Mar-17 10:11am    
The problem is that your test query does not reflect the actual query you're running from LINQ.

The test query is looking for records for a single UserID, so the index is a good fit.

The LINQ query is looking for records with one of a list of UserID values, and one of a list of ApplicationLoggedIn values. It's quite possible that the query optimiser has decided that your index is not a good fit for that query.

You need to examine the raw SQL query generated by your LINQ query, and examine the execution plan for that query in SQL Server Management Studio. If there are any obvious missing indexes, the execution plan will show them to you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900