mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-04-24 18:04:52 +08:00
Query optimization
This commit is contained in:
parent
d3497faa69
commit
588bce0b5c
@ -228,6 +228,17 @@ namespace OrmTest.Demo
|
||||
.OrderBy(st => st.Id, OrderByType.Desc)
|
||||
.OrderBy((st, sc) => sc.Id, OrderByType.Desc)
|
||||
.Select((st, sc) => new ViewModelStudent { Name = st.Name, SchoolId = sc.Id }).ToList();
|
||||
|
||||
//The simple use of Join 2 table
|
||||
var list5 = db.Queryable<Student, School>((st, sc) => st.SchoolId == sc.Id).Select((st,sc)=>new {st.Name,st.Id,schoolName=sc.Name}).ToList();
|
||||
|
||||
//join 3 table
|
||||
var list6 = db.Queryable<Student, School,School>((st, sc,sc2) => st.SchoolId == sc.Id&&sc.Id==sc2.Id)
|
||||
.Select((st, sc,sc2) => new { st.Name, st.Id, schoolName = sc.Name,schoolName2=sc2.Name }).ToList();
|
||||
|
||||
//join 3 table page
|
||||
var list7= db.Queryable<Student, School, School>((st, sc, sc2) => st.SchoolId == sc.Id && sc.Id == sc2.Id)
|
||||
.Select((st, sc, sc2) => new { st.Name, st.Id, schoolName = sc.Name, schoolName2 = sc2.Name }).ToPageList(1,2);
|
||||
}
|
||||
public static void Funs()
|
||||
{
|
||||
|
@ -49,6 +49,16 @@ namespace SqlSugar
|
||||
return mappingInfo == null ? typeName : mappingInfo.DbTableName;
|
||||
}
|
||||
}
|
||||
public string GetTableName(Type entityType)
|
||||
{
|
||||
var typeName = entityType.Name;
|
||||
if (this.Context.MappingTables == null || this.Context.MappingTables.Count == 0) return typeName;
|
||||
else
|
||||
{
|
||||
var mappingInfo = this.Context.MappingTables.SingleOrDefault(it => it.EntityName == typeName);
|
||||
return mappingInfo == null ? typeName : mappingInfo.DbTableName;
|
||||
}
|
||||
}
|
||||
public string GetEntityName(string tableName)
|
||||
{
|
||||
if (this.Context.MappingTables == null || this.Context.MappingTables.Count == 0) return tableName;
|
||||
|
@ -46,6 +46,7 @@ namespace SqlSugar
|
||||
public int JoinIndex { get; set; }
|
||||
public bool IsDisabledGobalFilter { get; set; }
|
||||
public virtual List<SugarParameter> Parameters { get; set; }
|
||||
public string EasyJoinInfo { get; set; }
|
||||
public virtual List<JoinQueryInfo> JoinQueryInfos
|
||||
{
|
||||
get
|
||||
@ -188,7 +189,7 @@ namespace SqlSugar
|
||||
#region Common Methods
|
||||
public virtual bool IsSingle()
|
||||
{
|
||||
var isSingle = Builder.QueryBuilder.JoinQueryInfos.IsNullOrEmpty();
|
||||
var isSingle = Builder.QueryBuilder.JoinQueryInfos.IsNullOrEmpty()&&EasyJoinInfo.IsNullOrEmpty();
|
||||
return isSingle;
|
||||
}
|
||||
public virtual ExpressionResult GetExpressionValue(Expression expression, ResolveExpressType resolveType)
|
||||
@ -196,6 +197,7 @@ namespace SqlSugar
|
||||
ILambdaExpressions resolveExpress = this.LambdaExpressions;
|
||||
this.LambdaExpressions.Clear();
|
||||
resolveExpress.JoinQueryInfos = Builder.QueryBuilder.JoinQueryInfos;
|
||||
resolveExpress.IsSingle = IsSingle();
|
||||
resolveExpress.MappingColumns = Context.MappingColumns;
|
||||
resolveExpress.MappingTables = Context.MappingTables;
|
||||
resolveExpress.IgnoreComumnList = Context.IgnoreColumns;
|
||||
@ -212,7 +214,7 @@ namespace SqlSugar
|
||||
foreach (var item in gobalFilterList.Where(it => it.IsJoinQuery == !IsSingle()))
|
||||
{
|
||||
var filterResult = item.FilterValue(this.Context);
|
||||
WhereInfos.Add(this.Builder.AppendWhereOrAnd(this.WhereInfos.IsNullOrEmpty(),filterResult.Sql));
|
||||
WhereInfos.Add(this.Builder.AppendWhereOrAnd(this.WhereInfos.IsNullOrEmpty(), filterResult.Sql));
|
||||
var filterParamters = this.Context.Ado.GetParameters(filterResult.Parameters);
|
||||
if (filterParamters.IsValuable())
|
||||
{
|
||||
@ -226,7 +228,8 @@ namespace SqlSugar
|
||||
if (Skip != null && Take == null)
|
||||
{
|
||||
if (this.OrderByValue == null) this.OrderByValue = " Order By GetDate() ";
|
||||
if (this.PartitionByValue.IsValuable()) {
|
||||
if (this.PartitionByValue.IsValuable())
|
||||
{
|
||||
this.OrderByValue = this.PartitionByValue + this.OrderByValue;
|
||||
}
|
||||
return string.Format(PageTempalte, sql.ToString(), GetOrderByString, Skip.ObjToInt() + 1, long.MaxValue);
|
||||
|
@ -14,6 +14,9 @@ namespace SqlSugar
|
||||
/// ** email:610262374@qq.com
|
||||
public class ExpressionContext : ExpResolveAccessory
|
||||
{
|
||||
#region Fields
|
||||
private bool _IsSingle = true;
|
||||
#endregion
|
||||
|
||||
#region properties
|
||||
public IDbMethods DbMehtods { get; set; }
|
||||
@ -22,15 +25,24 @@ namespace SqlSugar
|
||||
public MappingColumnList MappingColumns { get; set; }
|
||||
public MappingTableList MappingTables { get; set; }
|
||||
public IgnoreComumnList IgnoreComumnList { get; set; }
|
||||
public List<JoinQueryInfo> JoinQueryInfos { get; set; }
|
||||
|
||||
public bool IsSingle
|
||||
{
|
||||
get
|
||||
{
|
||||
return _IsSingle;
|
||||
}
|
||||
set {
|
||||
_IsSingle = value;
|
||||
}
|
||||
}
|
||||
public bool IsJoin
|
||||
{
|
||||
get
|
||||
{
|
||||
return JoinQueryInfos.IsValuable();
|
||||
return !IsSingle;
|
||||
}
|
||||
}
|
||||
public List<JoinQueryInfo> JoinQueryInfos { get; set; }
|
||||
public ResolveExpressType ResolveType { get; set; }
|
||||
public Expression Expression { get; set; }
|
||||
public ExpressionResult Result
|
||||
|
@ -12,6 +12,7 @@ namespace SqlSugar
|
||||
MappingTableList MappingTables { get; set; }
|
||||
IgnoreComumnList IgnoreComumnList { get; set; }
|
||||
List<JoinQueryInfo> JoinQueryInfos { get; set; }
|
||||
bool IsSingle { get; set; }
|
||||
SqlSugarClient Context { get; set; }
|
||||
IDbMethods DbMehtods { get; set; }
|
||||
Expression Expression { get; set; }
|
||||
|
@ -67,18 +67,18 @@ namespace SqlSugar
|
||||
}
|
||||
protected void InitMppingInfo<T>()
|
||||
{
|
||||
string cacheKey = "Context.InitAttributeMappingTables"+typeof(T).FullName;
|
||||
var entityInfo=this.Context.RewritableMethods.GetCacheInstance<EntityInfo>().Func(cacheKey,
|
||||
(cm, key) =>
|
||||
{
|
||||
var cacheInfo = cm[key];
|
||||
return cacheInfo;
|
||||
},
|
||||
(cm, key) =>
|
||||
{
|
||||
var reval = this.Context.EntityProvider.GetEntityInfo<T>();
|
||||
return reval;
|
||||
});
|
||||
string cacheKey = "Context.InitAttributeMappingTables" + typeof(T).FullName;
|
||||
var entityInfo = this.Context.RewritableMethods.GetCacheInstance<EntityInfo>().Func(cacheKey,
|
||||
(cm, key) =>
|
||||
{
|
||||
var cacheInfo = cm[key];
|
||||
return cacheInfo;
|
||||
},
|
||||
(cm, key) =>
|
||||
{
|
||||
var reval = this.Context.EntityProvider.GetEntityInfo<T>();
|
||||
return reval;
|
||||
});
|
||||
InitMppingInfo(entityInfo);
|
||||
}
|
||||
private void InitMppingInfo(EntityInfo entityInfo)
|
||||
@ -184,6 +184,14 @@ namespace SqlSugar
|
||||
queryable.SqlBuilder.QueryBuilder.JoinQueryInfos = this.GetJoinInfos(joinExpression, ref shortName, types);
|
||||
queryable.SqlBuilder.QueryBuilder.TableShortName = shortName;
|
||||
}
|
||||
protected string CreateEasyQueryJoin<T>(Expression joinExpression, Type[] types, ISugarQueryable<T> queryable) where T : class, new()
|
||||
{
|
||||
this.CreateQueryable<T>(queryable);
|
||||
string shortName = string.Empty;
|
||||
queryable.SqlBuilder.QueryBuilder.EasyJoinInfo = this.GetEasyJoinInfo(joinExpression, ref shortName,queryable.SqlBuilder,types);
|
||||
queryable.SqlBuilder.QueryBuilder.TableShortName = shortName + "," + queryable.SqlBuilder.QueryBuilder.EasyJoinInfo;
|
||||
return null;
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Private methods
|
||||
@ -199,7 +207,7 @@ namespace SqlSugar
|
||||
var joinArray = expressionContext.Result.GetResultArray();
|
||||
foreach (var entityType in entityTypeArray)
|
||||
{
|
||||
var isFirst = i == 0;++i;
|
||||
var isFirst = i == 0; ++i;
|
||||
JoinQueryInfo joinInfo = new JoinQueryInfo();
|
||||
var hasMappingTable = expressionContext.MappingTables.IsValuable();
|
||||
MappingTable mappingInfo = null;
|
||||
@ -227,6 +235,20 @@ namespace SqlSugar
|
||||
}
|
||||
return result;
|
||||
}
|
||||
protected string GetEasyJoinInfo(Expression joinExpression, ref string shortName, ISqlBuilder builder, params Type[] entityTypeArray)
|
||||
{
|
||||
string result = null;
|
||||
var lambdaParameters = ((LambdaExpression)joinExpression).Parameters.ToList();
|
||||
shortName = lambdaParameters.First().Name;
|
||||
var index = 1;
|
||||
foreach (var item in entityTypeArray)
|
||||
{
|
||||
result+=builder.GetTranslationTableName(item.Name) +PubConst.Space+lambdaParameters[index].Name+",";
|
||||
++index;
|
||||
}
|
||||
result = result.TrimEnd(',');
|
||||
return result;
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
@ -178,6 +178,69 @@ namespace SqlSugar
|
||||
base.CreateQueryJoin(joinExpression, types, queryable);
|
||||
return queryable;
|
||||
}
|
||||
public virtual ISugarQueryable<T, T2> Queryable<T, T2>(Expression<Func<T, T2, bool>> joinExpression) where T : class, new()
|
||||
{
|
||||
InitMppingInfo<T, T2>();
|
||||
var types = new Type[] { typeof(T2) };
|
||||
var queryable = InstanceFactory.GetQueryable<T, T2>(base.CurrentConnectionConfig);
|
||||
base.CreateEasyQueryJoin(joinExpression, types, queryable);
|
||||
queryable.Where(joinExpression);
|
||||
return queryable;
|
||||
}
|
||||
public virtual ISugarQueryable<T, T2, T3> Queryable<T, T2, T3>(Expression<Func<T, T2, T3, bool>> joinExpression) where T : class, new()
|
||||
{
|
||||
InitMppingInfo<T, T2, T3>();
|
||||
var types = new Type[] { typeof(T2), typeof(T3) };
|
||||
var queryable = InstanceFactory.GetQueryable<T, T2, T3>(base.CurrentConnectionConfig);
|
||||
base.CreateEasyQueryJoin(joinExpression, types, queryable);
|
||||
queryable.Where(joinExpression);
|
||||
return queryable;
|
||||
}
|
||||
public virtual ISugarQueryable<T, T2, T3, T4> Queryable<T, T2, T3, T4>(Expression<Func<T, T2, T3, T4, bool>> joinExpression) where T : class, new()
|
||||
{
|
||||
InitMppingInfo<T, T2, T3, T4>();
|
||||
var types = new Type[] { typeof(T2), typeof(T3), typeof(T4) };
|
||||
var queryable = InstanceFactory.GetQueryable<T, T2, T3, T4>(base.CurrentConnectionConfig);
|
||||
base.CreateEasyQueryJoin(joinExpression, types, queryable);
|
||||
queryable.Where(joinExpression);
|
||||
return queryable;
|
||||
}
|
||||
public virtual ISugarQueryable<T, T2, T3, T4, T5> Queryable<T, T2, T3, T4, T5>(Expression<Func<T, T2, T3, T4, T5, bool>> joinExpression) where T : class, new()
|
||||
{
|
||||
InitMppingInfo<T, T2, T3, T4, T5>();
|
||||
var types = new Type[] { typeof(T2), typeof(T3), typeof(T4), typeof(T5) };
|
||||
var queryable = InstanceFactory.GetQueryable<T, T2, T3, T4, T5>(base.CurrentConnectionConfig);
|
||||
base.CreateEasyQueryJoin(joinExpression, types, queryable);
|
||||
queryable.Where(joinExpression);
|
||||
return queryable;
|
||||
}
|
||||
public virtual ISugarQueryable<T, T2, T3, T4, T5, T6> Queryable<T, T2, T3, T4, T5, T6>(Expression<Func<T, T2, T3, T4, T5, T6, bool>> joinExpression) where T : class, new()
|
||||
{
|
||||
InitMppingInfo<T, T2, T3, T4, T5, T6>();
|
||||
var types = new Type[] { typeof(T2), typeof(T3), typeof(T4), typeof(T5), typeof(T6) };
|
||||
var queryable = InstanceFactory.GetQueryable<T, T2, T3, T4, T5, T6>(base.CurrentConnectionConfig);
|
||||
base.CreateEasyQueryJoin(joinExpression, types, queryable);
|
||||
queryable.Where(joinExpression);
|
||||
return queryable;
|
||||
}
|
||||
public virtual ISugarQueryable<T, T2, T3, T4, T5, T6, T7> Queryable<T, T2, T3, T4, T5, T6, T7>(Expression<Func<T, T2, T3, T4, T5, T6, T7,bool>> joinExpression) where T : class, new()
|
||||
{
|
||||
InitMppingInfo<T, T2, T3, T4, T5, T6>();
|
||||
var types = new Type[] { typeof(T2), typeof(T3), typeof(T4), typeof(T5), typeof(T6), typeof(T7) };
|
||||
var queryable = InstanceFactory.GetQueryable<T, T2, T3, T4, T5, T6, T7>(base.CurrentConnectionConfig);
|
||||
base.CreateEasyQueryJoin(joinExpression, types, queryable);
|
||||
queryable.Where(joinExpression);
|
||||
return queryable;
|
||||
}
|
||||
public virtual ISugarQueryable<T, T2, T3, T4, T5, T6, T7, T8> Queryable<T, T2, T3, T4, T5, T6, T7, T8>(Expression<Func<T, T2, T3, T4, T5, T6, T7, T8, bool>> joinExpression) where T : class, new()
|
||||
{
|
||||
InitMppingInfo<T, T2, T3, T4, T5, T6, T8>();
|
||||
var types = new Type[] { typeof(T2), typeof(T3), typeof(T4), typeof(T5), typeof(T6), typeof(T7), typeof(T8) };
|
||||
var queryable = InstanceFactory.GetQueryable<T, T2, T3, T4, T5, T6, T7, T8>(base.CurrentConnectionConfig);
|
||||
base.CreateEasyQueryJoin(joinExpression, types, queryable);
|
||||
queryable.Where(joinExpression);
|
||||
return queryable;
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Insertable
|
||||
|
Loading…
Reference in New Issue
Block a user