首页 > 数据库 >复杂查询还是直接写sql吧

复杂查询还是直接写sql吧

时间:2024-08-08 17:39:22浏览次数:6  
标签:Name 复杂 GetIQueryable 查询 sql SEO new Id id

今日改了一个linq,为了查询优化。

主要思路是把子查询改为连接查询。

改完后,本地运行是很快的;但是发布到服务器,加上网络时间,就有点不如意了。

所以感觉,非常复杂的查询还是直接用sql写好。

更改前后

更改前是这样的。

            void setPropety(List<SEO_Cust_RechargeModel> _list)
            {
                SEO_Set_MealBusiness sEO_Set_MealBusiness = new SEO_Set_MealBusiness();
                SEO_CustBusiness sEO_CustBusiness = new SEO_CustBusiness();
                //SEO_Cust_ProductBusiness sEO_Cust_ProductBusiness = new SEO_Cust_ProductBusiness();
                var domain = new Seo_Domain.SEO_DomainBusiness().GetIQueryable();
                var keywords = new SEO_KeyWordBusiness().GetIQueryable();
                var newsfeed = new Seo_NewsFeed.SEO_NewsFeedBusiness().GetIQueryable();
                var zengzhibus = new SEO_ZengZhiFwBusiness();
                _list.ForEach(x =>
                {
                    x.SetMeal_Name = sEO_Set_MealBusiness.GetTheData(x.setmeal_id)?.set_meal_name;
                    x.Cust_Nmae = sEO_CustBusiness.GetTheData(x.cust_id)?.user_name;
                    x.domainnum = domain.Where(xx => xx.cust_recharge_id == x.Id).Count();
                    x.keywordnum = keywords.Where(xx => xx.recharge_id == x.Id).Count();
                    x.newsfeednum = newsfeed.Where(xx => xx.recharge_id == x.Id).Count();
                    x.zengzhinum = zengzhibus.GetIQueryable().Count(y => y.recharge_id == x.Id);
                    x.pinpainum = new SEO_PinPai_KeyWordBusiness().GetIQueryable().Where(xx => xx.recharge_id == x.Id).Count();
                    x.SEOUser_Name = Base_UserBusiness.GetTheUser(x.seo_user_id)?.RealName;
                });
            }

更改后是这样的。

            void setPropetyByJoin(List<SEO_Cust_RechargeModel> _list)
            {
                //5个count
                var queryCount = from r in _list
                                 join dm in new SEO_DomainBusiness().GetIQueryable() on r.Id equals dm.cust_recharge_id into dmg
                                 join kw in new SEO_KeyWordBusiness().GetIQueryable() on r.Id equals kw.recharge_id into kwg
                                 join nf in new SEO_NewsFeedBusiness().GetIQueryable() on r.Id equals nf.recharge_id into nfg
                                 join zz in new SEO_ZengZhiFwBusiness().GetIQueryable() on r.Id equals zz.recharge_id into zzg
                                 join pp in new SEO_PinPai_KeyWordBusiness().GetIQueryable() on r.Id equals pp.recharge_id into ppg
                                 select new
                                 {
                                     rId = r.Id,
                                     dmCount = dmg.Count(),
                                     kwCount = kwg.Count(),
                                     nfCount = nfg.Count(),
                                     zzCount = zzg.Count(),
                                     ppCount = ppg.Count(),
                                 };
                var lct = queryCount.ToList();

                //连接其他表
                var queryJoin = from r in _list
                                join c in queryCount.Distinct() on r.Id equals c.rId
                                join sm in new SEO_Set_MealBusiness().GetIQueryable() on r.setmeal_id equals sm.Id into rSmG
                                join cst in new SEO_CustBusiness().GetIQueryable() on r.cust_id equals cst.Id into rCstG
                                join bu in new Base_UserBusiness().GetIQueryable() on r.seo_user_id equals bu.UserId into rBuG
                                join pr in new SEO_ProductBusiness().GetIQueryable() on r.product_id equals pr.Id into rPrG
                                from rSm in rSmG.DefaultIfEmpty()
                                from rCst in rCstG.DefaultIfEmpty()
                                from rBu in rBuG.DefaultIfEmpty()
                                from rPr in rPrG.DefaultIfEmpty()
                                select new
                                {
                                    r = r,
                                    c = c,
                                    agent_id = rCst?.agent_id ?? "",
                                    xs_user_id = rCst?.xs_user_id ?? "",
                                    SetMeal_Name = rSm == null ? "" : rSm.set_meal_name,
                                    Cust_Nmae = rCst == null ? "" : rCst.user_name,
                                    SEOUser_Name = rBu == null ? "" : rBu.RealName,
                                    pro_name = rPr == null ? "-" : rPr.pro_name,
                                    aftersale_user_id = rCst.aftersale_user_id, 
                                };

                //二次连接的
                var queryJoin2 = from m in queryJoin
                                 join ag in new SEO_AgentBusiness().GetIQueryable() on m.agent_id equals ag.Id into mAgG
                                 join buAfter in new Base_UserBusiness().GetIQueryable() on m.aftersale_user_id equals buAfter.UserId into mBuAfterG
                                 from mAg in mAgG.DefaultIfEmpty()
                                 from mBuAfter in mBuAfterG.DefaultIfEmpty()
                                 select new
                                 {
                                     r = m.r,
                                     c = m.c,
                                     agent_id = m.agent_id,
                                     xs_user_id = m.xs_user_id,
                                     SetMeal_Name = m.SetMeal_Name,
                                     Cust_Nmae = m.Cust_Nmae,
                                     SEOUser_Name = m.SEOUser_Name,
                                     pro_name = m.pro_name,
                                     SEOAfterSale_Name = m.aftersale_user_id.IsNullOrEmpty() || m.aftersale_user_id == "0" ?
                                                        "-" :
                                                        mBuAfter?.RealName??"-",
                                     SEOSale_Name = (m.xs_user_id.IsNullOrEmpty() || m.xs_user_id == "0") ? 
                                                    mAg?.inst_value??"-" : 
                                                    m.SEOUser_Name,
                                 }; 

                //循环赋值
                queryJoin2.Select(m =>
                {
                    //Count的
                    m.r.domainnum = m.c.dmCount;
                    m.r.keywordnum = m.c.kwCount;
                    m.r.newsfeednum = m.c.nfCount;
                    m.r.zengzhinum = m.c.zzCount;
                    m.r.pinpainum = m.c.ppCount;
                    //单个属性
                    m.r.SetMeal_Name = m.SetMeal_Name;
                    m.r.Cust_Nmae = m.Cust_Nmae;
                    m.r.SEOUser_Name = m.SEOUser_Name;
                    m.r.pro_name = m.pro_name;
                    m.r.SEOAfterSale_Name = m.SEOAfterSale_Name;
                    m.r.SEOSale_Name = m.SEOSale_Name; 
                    return 1;
                }).ToList();
            }

可以明显的看到:

子查询看起来简单;但是运行效率低。

连接查询编写起来复杂,特别是用linq写,就更复杂了。

还有第二波问题:

改完一波,我才发现,原来的作者在ViewModel里弄了一些有get访问器的属性。

而有几个get访问器,在里面操作了数据库。也相当于子查询。

于是又改了一波。

另外重要的

另外重要的,感觉是数据表的设计。

因为,感觉,虽然不知道是什么业务;有些字段的获取应该不用关联这么多表吧?应该是可以简化的。

 

 

翻译

搜索

复制

标签:Name,复杂,GetIQueryable,查询,sql,SEO,new,Id,id
From: https://www.cnblogs.com/fabao/p/18349401

相关文章

  • OS-Ubuntu-从源查询拉取安装包依赖-apt-cache&apt download
    OS-Ubuntu-从源查询拉取安装包依赖-apt-cache&aptdownload引用:Ubuntuapt-getapt-cache命令使用apt-get下载包及所有依赖指令apt-cachedepends--no-*--no-pre-depends--no-suggests--no-recommends--no-conflicts--no-breaks--no-enhances--recurseap......
  • 面试官:说说MySQL调优?
    MySQL作为关系型数据库的典型代表,其流行程度超越于任何数据库,因此在Java面试中,MySQL是一定会被问到的重要知识点。而在MySQL中有一道极其常见的面试题,我们这里系统的来看一下,这就是我们今天要讨论的MySQL调优问题。MySQL调优的大体思路如下:具体调优思路如下。1.查询......
  • OS-Ubuntu-系统版本信息查询及含义与源配置
    OS-Ubuntu-系统版本信息查询及含义与源配置引用https://blog.csdn.net/ix_fly/article/details/138271843指令lsb_release-aDemo:lsb_release-aDistributorID: UbuntuDescription: Ubuntu22.04.4LTSRelease: 22.04Codename: jammyUbuntu不同版本代号ver......
  • 用友U9数据库--用户对应的权限明细查询
    -----用户对应的权限菜单----------------------------------------------------------------------------------------selectdistincta.name组织,y.Name用户,U.Name用户组,b.Name角色名称,d.Name,menuBETrl.DisplayName菜单名称,caseauthorityBE.Opinionwhen0then......
  • 【漏洞复现】某赛通电子文档安全管理系统 PolicyAjax SQL注入漏洞
    0x01产品简介某赛通电子文档安全管理系统(简称:CDG)是一款电子文档安全加密软件,该系统利用驱动层透明加密技术,通过对电子文档的加密保护,防止内部员工泄密和外部人员非法窃取企业核心重要数据资产,对电子文档进行全生命周期防护,系统具有透明加密、主动加密、智能加密等多种加密......
  • NoSQL 数据库之MongoDB
    MongoDB是一个开源的NoSQL数据库,由MongoDBInc.研发和维护。它采用文档存储模型,使用JSON类似的BSON(二进制JSON)格式来存储数据。MongoDB具有高性能、易扩展和高可用性等特点,广泛应用于现代web应用程序中。以下是对MongoDB的详细介绍:核心特性1.文档存储MongoD......
  • 条件中in值过多导致的慢SQL问题
    在前段时间的项目中,出现了一个很典型的查询优化问题。在此跟大家分享问题分析及解决方法。此例中SQL文本大小达1.8MB,如下:这是一个多表连接的比较复杂的视图,SQL的过滤条件里id列“in”了几万个常量(红框部分)。这条语句第一次执行需要12秒,第二次执行时间为毫秒级。原因分析 ......
  • 关于java连接数据库时提示异常java.sql.SQLException: No suitable driver found for
    当我们测试一个新的数据库服务时,需要使用对方提供jdbc驱动来连接数据库,有时候简单的写个demo去连接,发现提示异常:java.sql.SQLException:Nosuitabledriverfoundforjdbc:jdbc:nuuv://10.1.8.99:8832/xxoo比如有以下程序连接数据库测试:publicstaticvoidmain(String[]a......
  • 达梦数据库如何定位查询慢SQL
    数据库日常运维中,常常需要对数据库的性能和负载进行监控和优化,因此,能够及时采集发现慢SQL很重要。达梦有三种方式,可以获取数据库慢SQL。第一种,利用达梦的SQL日志如果将DM数据库配置文件中的参数SVR_LOG设置为打开,则系统还会在log目录下生成名为dmsql_实例名_日期_时间.log......
  • 推荐一个创新高效的开源数据翻译工具,一个注解搞定数据翻译,减少30%的SQL代码量
    前言在软件开发中,数据翻译是一个常见且关键的需求,尤其是在处理数据库字段与用户界面显示之间的转换时。然而,传统的数据翻译方式往往涉及到繁琐的SQL联表查询,这不仅增加了数据库的负担,也使得代码变得复杂难以维护。为了解决这一问题,提高开发效率,减少不必要的SQL代码量,一款新的......