简体   繁体   English

如何在ASP.NET MVC3中使用Nhibernate进行更有效的查询

[英]How to make more efficient query using Nhibernate in asp.net mvc3

I have about 1 million records in my contact table in the DB, now I have to get only 30. How can I make the following query more efficient: 我在数据库的联系表中大约有100万条记录,现在只需要30条记录。如何使以下查询更有效:

GetContacts Method GetContacts方法

    private IQueryable<Contact> GetContacts()
    {

        return this.Query().Where(c => c.ActiveEmployer== true); // here ' this ' is the current service context 
    }

Gettin Contacts Gettin联系人

 var contactlist = GetContacts(); // this will get all records from db 
 var finalcontacts = contactlist.Skip(0).Take(30).Fetch(c.Roles).Fetch(c.SalaryInfo).ThenFetch(c.Employer);

But this query takes almost 10 seconds or more some times, and in the future I can have 30 - 40 million contacts then what would I do? 但是此查询需要花费几乎10秒钟或更长时间,并且将来我可以拥有30到4000万个联系人,我该怎么办?

There may be some people that could come up with a great fix for that query to help you without needing more information, but for that query and all others you can probably gain much more insight by taking a look at the query that is created by NHibernate. 也许有些人可以为该查询提供一个很好的解决方案,以在不需要更多信息的情况下为您提供帮助,但是对于该查询和所​​有其他查询,您可以通过查看NHibernate创建的查询来获得更多的见解。 。 If you've not done that before you can enable Log4Net (and possibly NLog) to output the SQL statements NH produces, or you can profile the SQL connection inside SQL Server. 如果尚未执行此操作,则可以启用Log4Net(可能还包括NLog)以输出NH生成的SQL语句,或者可以在SQL Server内部配置SQL连接。 You can then run that SQL yourself and select to show the query plan. 然后,您可以自己运行该SQL,然后选择显示查询计划。 This will tell you exactly what the query is doing and potentially you can solve the problem yourself. 这将准确告诉您查询在做什么,并且有可能您可以自己解决问题。 It might be missing index, the original WHERE or one or all of the subsequent FETCH statements. 它可能缺少索引,原始WHERE或后续的FETCH语句中的一个或全部。 Another question would be, have you hand crafted a SQL statement to do something similar and how long does that take? 另一个问题是,您是否手工制作了一条SQL语句来执行类似的操作,这需要多长时间?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM