EntityFramework 知识点与sql优化汇总


一、EntityFramework
modelBuilder.Entity<Domain.UseOilPlanDetail>().HasRequired(x => x.MainOilPlan).WithMany().HasForeignKey(y => y.PlanId);// 建立用油明细和主计划关系
说明: 多对1 UseOilPlanDetail 明细中包必须含MainOilPlan 主数据中可无导航属性:WithMany() 明细主关联主数据外键PlanId

modelBuilder.Entity<Domain.UseOilPlan>().HasMany(x => x.Attachments).WithRequired().HasForeignKey(y => y.BelongId);// 用油计划于附件表之间导航关系
说明:主数据与明细 1对多 主数据中有多个子数据Attachments,子数据中必须包含主数据Id 但可无导航属性:WithRequired() 用明细数据外键关联:BelongId (及主数据主键Id)


modelBuilder.Entity<Domain.UseOilPlan>().HasRequired(x => x.ConfirmUser).WithMany().HasForeignKey(y=>y.ConfirmedById);//用油计划和用户之间关系
modelBuilder.Entity<Domain.LadingBill>().HasMany(x => x.Attachments).WithRequired().HasForeignKey(y => y.BelongId);// 提货单与附件之间导航关系

二、sql优化 01
1、索引 所取数据条数小于总数据条数5%
2、加索引列 该列分组总条数 大于数据总条数 20% 如 select count(1) from systemUser group by Id / select count(1) from Systemuser 成为选择性,
3、在有可能为null 的字段上建立索引可以为其设置默认值 create index idx_page on t_page(object_id,0)
4、sql优化的核心思想 就是想方设法减少sql的物理I/O次数
5、not in 里面如果有null,整个查询会把这回空,而in 里面有null查询不受Null影响
6、Table Access by Index rowId ,Index Unique Scan ,Index Range Scan,Index Skip Scan ,【Index Full Scan ,Index Fast Full Scan (如果索引字段比较大(GB级别)应该优先使用 Index Fast Full Scan) 】 其性能由好到低排序的
7、建立索引 等值条件字段 排在前面、排序字段、不等值条件字段 按照依次顺序建组合索引
8、 select * from (select * from (select a.* ,rownum rn from (select * from tp_page order by object_id,object_name desc) a) where rownum<=10)where rn>=1 注意 Rownum<=10 的位置

 

posted @ 2019-01-04 18:13  xiaoBai1001  阅读(291)  评论(0编辑  收藏  举报