mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-10-24 01:49:12 +08:00
Improved performance of Count Hql queries (#7621)
This commit is contained in:
committed by
Sébastien Ros
parent
23e16a2f51
commit
fb949db305
@@ -5,6 +5,8 @@ using System.Globalization;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using NHibernate;
|
||||
using NHibernate.Engine;
|
||||
using NHibernate.Hql.Ast.ANTLR;
|
||||
using NHibernate.Transform;
|
||||
using Orchard.ContentManagement.Records;
|
||||
using Orchard.Data.Providers;
|
||||
@@ -28,6 +30,8 @@ namespace Orchard.ContentManagement {
|
||||
|
||||
public IContentManager ContentManager { get; private set; }
|
||||
|
||||
static readonly private ASTQueryTranslatorFactory TranslatorFactory = new ASTQueryTranslatorFactory();
|
||||
|
||||
public DefaultHqlQuery(
|
||||
IContentManager contentManager,
|
||||
ISession session,
|
||||
@@ -207,12 +211,17 @@ namespace Orchard.ContentManagement {
|
||||
|
||||
public int Count() {
|
||||
ApplyHqlVersionOptionsRestrictions(_versionOptions);
|
||||
var hql = ToHql(true);
|
||||
hql = "select count(Id) from Orchard.ContentManagement.Records.ContentItemVersionRecord where Id in ( " + hql + " )";
|
||||
return Convert.ToInt32(_session.CreateQuery(hql)
|
||||
.SetCacheable(true)
|
||||
.UniqueResult())
|
||||
;
|
||||
var sql = ToSql(true);
|
||||
sql = "SELECT count(*) as totalCount from (" + sql + ") t";
|
||||
return Convert.ToInt32(_session.CreateSQLQuery(sql)
|
||||
.AddScalar("totalCount", NHibernateUtil.Int32)
|
||||
.UniqueResult());
|
||||
}
|
||||
|
||||
public string ToSql(bool count) {
|
||||
var sessionImp = (ISessionImplementor)_session;
|
||||
var translators = TranslatorFactory.CreateQueryTranslators(ToHql(count), null, false, sessionImp.EnabledFilters, sessionImp.Factory);
|
||||
return translators[0].SQLString;
|
||||
}
|
||||
|
||||
public string ToHql(bool count) {
|
||||
|
||||
Reference in New Issue
Block a user