zoukankan      html  css  js  c++  java
  • 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 的位置

  • 相关阅读:
    How many things do you really about .net framwork?
    Things that will impact concurrency & capacity behavior of WCF service (with simoultaneous client requests/connections)
    3 ways to do WCF Message Exchange Model
    3 ways to create WCF Client(ChannelFactory)
    Do not apply "using" for the clientWCF Client
    Understanding WCF Session
    Using FaultContract
    3 ways to do WCF Concurrency Management(Single, Multiple, and Reentrant and How to do with Throttling)
    webview使用技巧汇总
    反序列化网易miniblog json格式数据 原创 create by lee
  • 原文地址:https://www.cnblogs.com/liyanbofly/p/10221734.html
Copyright © 2011-2022 走看看