-1

我有 LINQ 查询检查查询结果并结束语句。该数据库有大约 600k 数据。下面是 LINQ 查询和 if 语句。

 var feetypelist = from feetype in dbDataContext.tbl_fee_types
                          orderby feetype.seq
                          select feetype.id;

        foreach (var fty in feetypelist)
        {
            var checkFeeCharges = from fee in dbDataContext.tbl_fee_schedules
                                  where fee.arr == arr && fee.dep == dep && fee.feetypeid == fty && fee.site_id == siteId
                                  orderby fee.seq
                                  select fee;

            if (checkFeeCharges.Count() == 0)
            {
                checkFeeCharges = from fee in dbDataContext.tbl_fee_schedules
                                  where fee.arr == arr && fee.dep == dep && fee.feetypeid == fty && fee.site_id == siteId
                                  && fee.country == country
                                  orderby fee.seq
                                  select fee;
            }

            if (checkFeeCharges.Count() == 0)
            {
                checkFeeCharges = from fee in dbDataContext.tbl_fee_schedules
                                  where fee.site_id == siteId && fee.country == country && 
                                  ((fee.arr == arr && fee.dep == dep) || (fee.arr.Equals("DOM") && fee.dep.Equals("DOM")) ||
                                  (fee.arr.Equals("*") && fee.dep == dep) || (fee.arr == arr &&  fee.dep.Equals("*")))
                                  && fee.feetypeid == fty
                                  orderby fee.seq
                                  select fee;

                if (checkFeeCharges.Count() == 0)
                {
                    var country_route = from route in dbDataContext.tbl_country_routes
                                        where route.origin_airport_cd == arr && route.destination_airport_cd == dep
                                        select route;

                    if (country_route.Count() >= 1)
                    {
                        foreach (var c in country_route)
                        {
                            if (c.air_asiax == 'Y')
                            {
                                //con_flight = "Y";
                                checkFeeCharges = from fee in dbDataContext.tbl_fee_schedules
                                                  where fee.site_id == siteId && fee.country == country &&
                                                  ((fee.arr == "AAX" && fee.dep == "AAX") || (fee.arr == "*" && fee.dep == dep) ||
                                                  (fee.arr == arr && fee.dep == "*")) && fee.feetypeid == fty
                                                  orderby fee.seq
                                                  select fee;

                                if (checkFeeCharges.Count() == 0)
                                {
                                    checkFeeCharges = from fee in dbDataContext.tbl_fee_schedules
                                                      where fee.site_id == siteId && fee.country == country &&
                                                      ((fee.arr == arr && fee.dep == dep) || (fee.arr == "INT" && fee.dep == "INT") ||
                                                      (fee.arr == "*" && fee.dep == dep) || (fee.arr == arr && fee.dep == "*") ||
                                                      (fee.arr == country && fee.dep == country)) &&
                                                      fee.feetypeid == fty
                                                      orderby fee.seq
                                                      select fee;
                                }
                            }
                            else
                            {
                                checkFeeCharges = from fee in dbDataContext.tbl_fee_schedules
                                                  where fee.site_id == siteId && fee.country == country &&
                                                  ((fee.arr == arr && fee.dep == dep) || (fee.arr == "INT" && fee.dep == "INT") ||
                                                  (fee.arr == "*" && fee.dep == dep) || (fee.arr == arr && fee.dep == "*") ||
                                                  (fee.arr == country && fee.dep == country)) &&
                                                  fee.feetypeid == fty
                                                  orderby fee.seq
                                                  select fee;
                            }

                        }
                    }
                    else
                    {
                        checkFeeCharges = from fee in dbDataContext.tbl_fee_schedules
                                          where fee.site_id == siteId && fee.country == country &&
                                          ((fee.arr == arr && fee.dep == dep) || (fee.arr == "INT" && fee.dep == "INT") ||
                                          (fee.arr == "*" && fee.dep == dep) || (fee.arr == arr && fee.dep == "*")) &&
                                          fee.feetypeid == fty
                                          orderby fee.seq
                                          select fee;
                    }
                }
            }

            if (checkFeeCharges.Count() == 0)
            {
                checkFeeCharges = from fee in dbDataContext.tbl_fee_schedules
                                  where fee.site_id == siteId && fee.country == country &&
                                  ((fee.arr == arr && fee.dep == dep) || (fee.arr == "INT" && fee.dep == "INT") ||
                                  (fee.arr == "*" && fee.dep == dep) || (fee.arr == arr && fee.dep == "*")) &&
                                  fee.feetypeid == fty
                                  orderby fee.seq
                                  select fee;
            }

查询执行需要一段时间才能完成,知道如何优化此查询。

返回 json 请求

if (checkFeeCharges.Any())
            {
                List<FeeAndChargesModel_Fee_Schedule> feeTable = new List<FeeAndChargesModel_Fee_Schedule>();
                foreach (var p in checkFeeCharges)
                {
                    feeTable.Add(
                        new FeeAndChargesModel_Fee_Schedule()
                        {
                            arr = FlightScheduleAPIController.GetCountryName(arr, siteId),
                            dep = FlightScheduleAPIController.GetCountryName(dep, siteId), 
                            country = p.country,
                            feedesc = p.feedesc,
                            feetype = p.feetype,
                            currency = p.currency,
                            value = p.value,
                            remark = p.remark
                        }
                    );
                    label = p.feetype;


                }

                mainJson.Add(
                        new FeeAndChargesModel_Main
                        {
                            label = label,
                            con_flight = FlightScheduleAPIController.GetCountryName(GetConnectingFlight(dep, arr), siteId),
                            details = feeTable
                        }
                        );
            }
        }

        return mainJson;
4

2 回答 2

1

好的,所以这段代码真的很难阅读,但让我们给你一些建议。

我注意到的第一件事是执行了第一个 sql 语句

var checkFeeCharges = from fee in dbDataContext.tbl_fee_schedules
                              where fee.arr == arr && fee.dep == dep && fee.feetypeid == fty && fee.site_id == siteId
                              orderby fee.seq
                              select fee;

如果它什么都不返回

        if (checkFeeCharges.Count() == 0)
        {

它执行一个比第一个更具体的 sql 查询。它具有所有相同的 AND 语句 + 另一个。如果第一个查询什么都不返回,那么期望(所有东西都是不变的)应该是第二个什么都不返回。如果是这样,那为什么要这样做?

            checkFeeCharges = from fee in dbDataContext.tbl_fee_schedules
                              where fee.arr == arr && fee.dep == dep && fee.feetypeid == fty && fee.site_id == siteId
                              && fee.country == country
                              orderby fee.seq
                              select fee;
        }

我个人会尝试理解或解决以下问题

1 预期的结果集大小是多少,我是否可以检索“少量”记录并在内存中处理这些记录。我需要了解结果集是否会随着时间及其最小、最大类型边界而增长。

2 是否有更好的数据库结构,我可以重构要使用的代码,这将使该查询更容易和更有效。

3 为了真正能够优化这个查询,我会尝试先直接在 sql 中编写它并使用 explain 来帮助准确了解 sql 数据库在做什么,正在使用哪些索引以及返回的结果集的大小每个查询。

. 我也相信您应该能够将您拥有的许多查询组合成一个表达式。

这可能不完全正确,但应该可以作为演示

from fee in dbDataContext.tbl_fee_schedules
where (fee.arr == arr && fee.dep == dep && fee.feetypeid == fty && fee.site_id == siteId
    && fee.country == country) || (fee.site_id == siteId && fee.country == country &&
                                              ((fee.arr == "AAX" && fee.dep == "AAX") || (fee.arr == "*" && fee.dep == dep) ||
                                              (fee.arr == arr && fee.dep == "*")) && fee.feetypeid == fty)
|| ( ..... ) | ( .... )

从我在那里看到的情况来看,您将需要几个连接或子选择,这就是为什么在 sql 中尝试它会最快的原因。还有一些 linq2sql/ORM 分析工具可能有助于诊断。

于 2013-04-17T05:25:29.533 回答
0

很难说如何优化它,因为这里有很多重复。我会先尝试减少这种情况。

一件明显需要改变的事情是:

if (checkFeeCharges.Count() == 0)

每次调用它时都会枚举整个查询。检查同一件事的更便宜的方法:

if (!checkFeeCharges.Any())

这只会从查询中枚举一个结果以确定它是否为空。

于 2013-04-17T04:14:29.670 回答