简体   繁体   English

哪一个性能更好?

[英]Which one have better performance?

I write same query with two approach by using NHibernate: 我使用NHibernate用两种方法编写相同的查询:

1- by using HQL like below 1-通过使用HQL如下

public long RetrieveHQLCount<T>(string propertyName, object propertyValue)
{
    using (ISession session = m_SessionFactory.OpenSession())
    {
        long r = Convert.ToInt64(session.CreateQuery("select count(o) from " + typeof(T).Name + " as o" + " where o." + propertyName + " like '" + propertyValue + "%'").UniqueResult());
        return r;
    }
}

2- by using ICriteria and SetProjections like below 2-通过使用如下所示的ICriteriaSetProjections

public long RetrieveCount<T>(string propertyName, object propertyValue)
{
    using (ISession session = m_SessionFactory.OpenSession())
    {
        // Create a criteria object with the specified criteria
        ICriteria criteria = session.CreateCriteria(typeof(T));
        criteria.Add(Expression.InsensitiveLike(propertyName, propertyValue))
            .SetProjection(Projections.Count(propertyName));

        long count = Convert.ToInt64(criteria.UniqueResult());

        // Set return value
        return count;
    }
}

Now my question is that which one has better performance? 现在我的问题是,哪一个性能更好? why? 为什么?

I think the best way to get a metric of which is better would be as was stated here. 我认为,获得更好的度量标准的最佳方法将如此处所述。 Go download nhProf and profile it. 去下载nhProf并对其进行配置。

http://nhprof.com/ http://nhprof.com/

If you want more detail take the sql that is generated and THEN run it through SQL Server profiler to get an even better idea of what it is doing. 如果您想了解更多细节,请使用生成的sql,然后通过SQL Server Profiler运行它,以更好地了解其功能。

But honestly, if you have any quantity of data in your database doing a LIKE query will give you horrible HORRIBLE results. 但老实说,如果您的数据库中有大量数据,则执行LIKE查询将为您带来可怕的可怕结果。

I would strongly recommend that you set up Full Text indexing in SQL Server and then use this: 我强烈建议您在SQL Server中设置全文本索引,然后使用此方法:

http://nhforge.org/blogs/nhibernate/archive/2009/03/13/registering-freetext-or-contains-functions-into-a-nhibernate-dialect.aspx http://nhforge.org/blogs/nhibernate/archive/2009/03/13/registering-freetext-or-contains-functions-into-a-nhibernate-dialect.aspx

to register the freetext and contains functions in nHibernate. 注册自由文本并在nHibernate中包含函数。

another great example to integrate with ICriteria queries is here: 与ICriteria查询集成的另一个很好的例子是:

http://xlib.wordpress.com/2009/12/04/integrating-freetext-search-in-nhibernate-detached-criteria/ http://xlib.wordpress.com/2009/12/04/integrating-freetext-search-in-nhibernate-detached-criteria/

Alternatively you can use Lucene.NET to do the full text indexing as well. 另外,您也可以使用Lucene.NET进行全文索引。

There is no significant intrinsic performance difference between HQL and Criteria. HQL和条件之间没有显着的内在性能差异。 They're just different APIs to express a query that in the end will be translated to SQL, that's it. 它们只是表示查询的不同API,最终将其转换为SQL,仅此而已。

The criteria (no pun intended) to pick one API over the other depends on the usage context. 选择一个API取代另一个API的标准(无双关语)取决于使用情况。 For example, in your particular case, I'd go with Criteria. 例如,在您的特定情况下,我会选择Criteria。 Building a query from string concatenation is quite error-prone and you have to be very careful not to be vulnerable to injection attacks . 从字符串连接构建查询非常容易出错,因此必须非常小心,以免受到注入攻击的影响 At least set the propertyValue to be a IQuery parameter... 至少将propertyValue设置为IQuery参数...

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

相关问题 哪种语法有更好的性能? - Which one of this syntaxs have better performance? 哪一个可以有更好的性能 - LINQ to EF或NHibernate? - Which one can have better performance - LINQ to EF or NHibernate? 这些代码示例中的哪一个具有更好的性能? - Which one of these code samples has better performance? 哪个表现更好 - Which is better in performance 哪个对性能更好:foreach()tatement或Where()查询? - Which one is better for performance: foreach()tatement or Where() query? SqlDataReader vs SqlDataAdapter:哪一个具有更好的返回DataTable的性能? - SqlDataReader vs SqlDataAdapter: which one has the better performance for returning a DataTable? 哪一个更适合获得准确的性能计数:日期时间或秒表? - Which one is better for getting accurate performance counte:DateTime or Stopwatch? XPath vs DeSerialization:哪一个在读取操作方面性能更好 - XPath vs DeSerialization: which one is better in performance for read operations 字符串插值和 string.format 中的哪一个在性能上更好? - Which of one from string interpolation and string.format is better in performance? 在这些简单的示例中哪个对性能更好? - Which is better for performance in these simple examples?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM