How can I improve this query in Entity framework 6.0? - asp.net-mvc

Can someone please suggest me what wrong with this query? How can I improve the performance and decrease the time taken to execute it?
IQueryable<Mapper> query = null;
query = (from c in entities.Users
where c.UserEmailAddress == emailAddress
&& c.UserPassword == password
&& c.IsAccountVerified == true
select new Mapper()
{
UserId= c.UserID,
Name = c.UserName
});
custObj = query.ToList<Mapper>().FirstOrDefault();
I am using EF profiler it alerts me following warning:-
Query on unindexed column
Column Type mismatch
More than one session per request
FYI:-
EmailAddress - varchar(50) - Non ClusteredIndex
Password - varchar(max) - No Index
IsAccountVerified - bool - No Index
Even in local, I notice its taking 2-4 seconds to execute?
Apart from it, is there can someone suggest imp guidelines to fine tune the queries in EF.
I am using EF6.0

I think the problem is that you're using unnecessary complex query as EmailAddress is probably unique. Now you are checking three conditions to select your record, but using only email address should be fairly enough. I would rather select user basing on EmailAddress (and maybe IsAccountVerified) and later checked password hash in code.
The code would be something like this (I haven't checked it):
var user = entities.Users.FirstOrDefault(u => u.EmailAddress == emailAddress)
Mapper custObj = null;
if(user != null && user.IsAccountVerified && user.Password == password)
custObj = new Mapper
{
UserId = user.UserID,
Name = user.UserName
};
Now you are not making a query on non indexed column, and results will be the same.
I checked simillar case on MS SQL database. Select based on one condition using indexed column boosted the query nicely (0ms instead of 13ms in my case).

Related

Efficient way to get a huge number of records sorted via Linq. Also some help regarding editing an existing DB entry

The fist part of my question is
suppose I have a poco class
public class shop{
public virtual string fruitName {get;set;}
public virtual double numberOfFruitsLeftToConsume {get;set;}
public virtual double numberOfFruitsLeftForStorage {get;set;}
public virtual List<Locations> shopLocations {get;set;}
}
I add new fruits in the db by creating a new object of shop and then add it via my context then save it.
Now to retrieve the data
will it be more efficient for me to first filter by fruit name get a List then in that collection should I run my query to sort by the number of fruits to consume , or should I just put it all into one query. Supposing that the site has more than 1000 hits/sec and a massive DB, which method will be efficient.
List<shop> sh = context.shopDB.Where(p => p.fruitName == "mango" &&
p.fruitName == "apple").ToList();
List<shop> sh = sh.Where(f => f.numberOfFruitsLeftToConsume >= 100 &&
f.numberOfFruitsLeftForStorage <= 100).ToList();
The example has no meaning , I just wanted to show the type of query I am using.
The second part of my question is, when I initialize the class shop I do not initialize the List within it. Later on when I try to add it it does not get saved, the shop is connected to the user.
ApplicationUser user = await usemanager.FindByEmailAsync("email");
if(user.shops.shopLocations == null){
user.shops.shopLocation = new List<Location>();
uset.shops.shopLocation.Add(someLocation);
await context.shopDB.SaveChangesAsync();
}
////already tried
//List<Location> loc = new List<Location>();
//loc.Add(someLocation);
//user.shops.shopLocation = loc;
//await context.shopDB.SaveChangesAsync();
I tried both the methods in a try catch block and no exception is thrown.
If you need any more details or if something is not clear to you please ask.
Thank you.
If I add Location and LocationId properties to shop, and then save, I can only view the LocationId , but the Location property still remains null.
To clear any question , If I save a location Individually it saves. So I don't think I'm providing wrong data.
Now to retrieve the data will it be more efficient for me to first filter by fruit name get a List then in that collection should I run my query to sort by the number of fruits to consume , or should I just put it all into one query. Supposing that the site has more than 1000 hits/sec and a massive DB, which method will be efficient.
You are the only one who can answer that question by measuring the query performance. Only as a general rule I can say that putting all into one query and let the database do the most of the job (eventually tuning it by creating appropriate indexes) is usually preferable.
What about the second part of the question (which basically is a different question), this block
if (user.shops.shopLocations == null)
{
user.shops.shopLocation = new List<Location>();
user.shops.shopLocation.Add(someLocation);
await context.shopDB.SaveChangesAsync();
}
looks suspicious. Your shopLocations member is declared as virtual, which means it's intended to use lazy loading, hence most probably will never be null. And even if it is null, you need to keep only the new part inside the if and do the rest outside, like this
if (user.shops.shopLocations == null)
user.shops.shopLocation = new List<Location>();
user.shops.shopLocation.Add(someLocation);
await context.shopDB.SaveChangesAsync();
1st Question
Because you are calling .ToList() at the end of your queries it will have to fetch all the rows from the db each time, so it will be much faster to do all your filtering in one LINQ .Where() call like this:
List<shop> sh = context.shopDB.Where(p => p.fruitName == "mango" && p.fruitName == "apple" && f.numberOfFruitsLeftToConsume >= 100 && f.numberOfFruitsLeftForStorage <= 100).ToList();
but if you don't call .ToList() at the end of first Linq query, spliting your query into two calls will be tottally fine and will yield the same performance as the previous approach like this:
var sh = context.shopDB.Where(p => p.fruitName == "mango" &&
p.fruitName == "apple");
List<shop> shList = sh.Where(f => f.numberOfFruitsLeftToConsume >= 100 &&
f.numberOfFruitsLeftForStorage <= 100).ToList();
2nd Question
when you initialize the Location for the shop, you must set the shopId property and then it should work, if not the problem might be with your database relationships.

Searching HP-Trim / Records-Manager using SDK

Using the HP-Trim SDK, how do you search for a document by its reference number?
The alleged documentation refers to methods for straightforward searches:
SelectByPrefix
SelectFavorites
SelectByUserLabel
SelectNone
SelectAll
SelectByUris
SelectTopLevels
SelectThoseWithin
and a generic search:
records.SetSearchString(“createdOn:this week and assignee:me”);
but all I want to do is find a document by its index.
These don't work:
records.SetSearchString("recordNum: <RecordNumber>");
records.SetSearchString("recordNumber: <RecordNumber>");
records.SetSearchString("reference: <RecordNumber>");
Any suggestions?
Are you using the .NET SDK? If so you can grab a record by its record number like so (C# example):
using (Database db = new Database()) {
db.Connect();
Record record = new Record(db, "123456"); // Replace with record number
// Do stuff with record
Console.WriteLine(record.Title);
}
You aren't required to construct a 'formal search' as such.
In case you're curious about the correct string search syntax, this would have worked:
records.SetSearchString("number: <RecordNumber>");
Using the COM SDK;
using (Database db = new Database()) {
db.Connect();
Records records = db.MakeRecords();
records.SelectAll();
records.FilterString = "number:<RecordNumber>";
if (records == null || records.Count.Equals(0))
return;
Record existing = records.Item(0);
}

Where not null using Linq

I have a mySQL database with a Website field that is of type VarChar. Some of these fields will be null on the database.
I have an MVC application that I sending the database information to.
I am trying to set up a filter on the Index page so I can filter by certain columns. I am using the Request.QueryString to do this.
switch (Request.QueryString["FilterOptionSelect"])
{ case "CountyName":
if (!string.IsNullOrEmpty(filteredText))
{
filteredText = filteredText.ToUpper();
var modelFiltered = from n in model
where n.CountyName.ToUpper().Contains(filteredText)
select n;
return View(modelFiltered);
}
break;
case "Website":
if (!string.IsNullOrEmpty(filteredText))
{
filteredText = filteredText.ToUpper();
var modelFiltered = from n in model
where n.CountyWebsite.ToUpper().Contains(filteredText)
select n;
return View(modelFiltered);
}
break;
}
The only problem I have is on the Website case. It gives me a Object reference not set to an instance of an object. on the WHERE CLAUSE of the Website case. When I debug, my model is not null (it has 130+ items inside...some with website info and some without website info).
I have already tried using the Lambda method (which had the same problem). I have also tried using where n.CountyWebsite.ToUpper().Contains(filteredText) && n.CountyWebsite != null which did not work either.
You were on the right track with your second attempt, but you need to switch the order of those statements.
where n.CountyWebsite != null && n.CountyWebsite.Contains(filteredText)
Just like all the other && operators, you don't want to evaulate any websites that are null, so do that operation first.
Also, .Contains in EF automatically is case-insensitive, so you don't need the ToUpper().
Try this, I think this will solve.
!String.IsNullOrEmpty(n.CountyWebsite) && n.CountyWebsite.Contains(filteredText)

Add multiple relationships between 2 nodes with Neo4jClient

I am building a social graph application using Neo4jClient to store data and I am trying to come up with the best strategy to model where a user has worked and currently works. So my User is connected with the relationship "USER_WORK" to several Work nodes which has StartDate/EndDate properties. If EndDate is not set I want to add another relationship "CURRENT" between the user/work node to be able to fetch only current work places in an efficient way.
For some reason Neo4jClient does not let me do this? Below query executes without exceptions and the work node and all relationships except "CURRENT" is added (and yes I have checked that there is no problem with the EndDate logic:) I have also tried using Create instead of CreateUnique but that doesn't solve the problem :(
var query = graphClient.Cypher
.Match("(city:City)", "(profession:Profession)", "(company:Company)", "(user:User)")
.Where((CityEntity city) => city.Id == model.CityId)
.AndWhere((ProfessionEntity profession) => profession.Id == model.ProfessionId)
.AndWhere((CompanyEntity company) => company.Id == model.CompanyId)
.AndWhere((UserEntity user) => user.Id == model.UserId)
.Merge("(work:Work { Id: {Id} })")
.OnCreate()
.Set("work = {entity}")
.WithParams(new
{
Id = entity.Id,
entity
})
.CreateUnique("work-[:WORK_AS_PROFESSION]->profession")
.CreateUnique("work-[:WORK_AT_COMPANY]->company")
.CreateUnique("work-[:WORK_IN_CITY]->city")
.CreateUnique("user-[:USER_WORK]->work");
if (model.EndDate == DateTime.MinValue)
{
query.CreateUnique("user-[:CURRENT]->work");
}
query.ExecuteWithoutResults();
When you call CreateUnique to create the user-[:CURRENT]->work relationship, it's not actually being appended to the query. What you need to change that line to is:
query = query.CreateUnique("user-[:CURRENT]->work");
Which is what is happening for all the fluent methods chained in the first query you write out. The easiest way to spot these things is stick a breakpoint on the query.ExecuteWithoutResults(); method and when VS breaks there, hover over query and see if the text matches what you think it should.

Query - Does Not Contain

I have a search query to lookup Customers.
I would like to use the Sounds Like function to return additional possible results, however this is returning some of the same results in my main search query.
I would like to only show the additional results in a partial view.
I basically need a DoesNotContain.
Here is what I have so far for my main query:
customer = customer.Where(c => SqlFunctions.StringConvert((double)c.CustomerID).Trim().Equals(searchString)
|| c.CustomerName.ToUpper().Contains(searchString.ToUpper()));
And for the additional results:
customeradditional = customeradditional.Where(c => SqlFunctions.SoundCode(c.CustomerName.ToUpper()) == SqlFunctions.SoundCode(searchString.ToUpper()));
The only possible solution I can see at the minute is to do a Contains Query, loop through each item and get the IDs, then do another query for CustomerID != 1 or CustomerID != 2 or CustomerID != 3, etc.
Try using Except:
customeradditional = customeradditional
.Where(c => SqlFunctions.SoundCode(c.CustomerName.ToUpper()) == SqlFunctions.SoundCode(searchString.ToUpper()))
.Except(customer);
I am not sure if I understood you correct:
From what you have now, the customeraddtional query does return some of the customers already returned in the customer query. And you only want the results, which are not already contained in the customer query.
Then the solution would be:
customeradditional = customeradditional.Where(c =>
SqlFunctions.SoundCode(c.CustomerName.ToUpper()) ==
SqlFunctions.SoundCode(searchString.ToUpper()))
.Except(customer);
This way your are explicitly excluding every item, which is present in the customer object.

Resources