Sql程序使用2个日期d1和d2慢

时间:2017-06-07 13:51:24

标签: c# postgresql

我有2个问题,首先当我使用2个定义的日期时我的请求有效,但是当我尝试使用来自我的C#代码的两个var时,它不起作用它非常慢。然后,如果我使用这两个变量的限制,它工作直到294但在295它不起作用我不知道为什么。有我的代码。

CREATE OR REPLACE FUNCTION prc_get_list_resa(d1 date, d2 date, p_id_resa integer, p_state character, p_id_clt integer, p_id_chauff integer, p_id_conv integer, p_id_fact integer, p_id_org integer)
  RETURNS refcursor AS
$BODY$
DECLARE
      ref refcursor;
    BEGIN
        OPEN ref FOR
          SELECT *
          FROM db_reservations res
          LEFT JOIN db_clients clt ON res.id_clt_resa = clt.id_clt
          LEFT JOIN db_organisations org ON res.id_org_resa = org.id_org
          LEFT JOIN db_chauffeurs chauff ON res.id_chauff_resa = chauff.id_chauff
          LEFT JOIN db_vehicules vehic ON res.id_vehic_resa = vehic.id_vehic
          LEFT JOIN db_type_transport ttransp ON res.id_type_transp_resa = ttransp.id_type_transp

          WHERE  res.date_dep_resa >= (select distinct d1 from db_reservations) and res.date_dep_resa < (select distinct d2 from db_reservations)
           and  ((res.id_resa        = p_id_resa)   or (p_id_resa = 0))
           and  ((res.id_org_resa    = p_id_org)    or (p_id_org = 0))
           and  ((res.id_chauff_resa = p_id_chauff) or (p_id_chauff = 0))
           and  ((res.id_clt_resa    = p_id_clt)    or (p_id_clt  = 0))
           and  ((res.id_conv_resa   = p_id_conv)   or (p_id_conv = 0))
           and  ((res.id_fact_resa   = p_id_fact)   or (p_id_fact = 0))
           and  ((res.etat_resa      = p_state)     or (p_state   = ''))

           order by id_org_resa,id_clt_resa, date_dep_resa,type_trajet asc limit 1000;
      RETURN ref ;
    END;
  $BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;
ALTER FUNCTION prc_get_list_resa(date, date, integer, character, integer, integer, integer, integer, integer)
  OWNER TO postgres;

c#代码:

public static List<Reservation> ListReservations(string param, int p_id_resa, string p_state, int p_id_clt, int p_id_chauff, int p_id_conv, string p_deb_period, string p_fin_period, int p_id_fact, int p_id_pass, int p_id_org)
{
    string d1, d2;
    DateTime baseDate = DateTime.Today;

    var today = baseDate;
    var yesterday = baseDate.AddDays(-1);
    var thisWeekStart = baseDate.AddDays(-(int)baseDate.DayOfWeek);
    var thisWeekEnd = thisWeekStart.AddDays(7).AddSeconds(-1);
    var lastWeekStart = thisWeekStart.AddDays(-7);
    var lastWeekEnd = thisWeekStart.AddSeconds(-1);
    var thisMonthStart = baseDate.AddDays(1 - baseDate.Day);
    var thisMonthEnd = thisMonthStart.AddMonths(1).AddSeconds(-1);
    var lastMonthStart = thisMonthStart.AddMonths(-1);
    var lastMonthEnd = thisMonthStart.AddSeconds(-1);
    if (param == "lastweek")
    {
        d1 = thisWeekStart.ToString("dd-MM-yyyy");
        d2 = thisWeekEnd.ToString("dd-MM-yyyy");
    }
    else if (param == "lastmonth")
    {
        d1 = thisMonthStart.ToString("dd-MM-yyyy");
        d2 = thisMonthEnd.ToString("dd-MM-yyyy");
    }
    else if (param == "today")
    {
        d1 = DateTime.Today.ToString("dd-MM-yyyy");
        d2 = DateTime.Today.ToString("dd-MM-yyyy");
    }
    else if (param == "all")
    {
        d1 = "01-01-1900";
        d2 = "01-01-2100";
    }
    else
    {
        d1 = p_deb_period;
        d2 = p_fin_period;
    }

    NpgsqlDataReader dr = null;
    List<Reservation> lst_reservation = new List<Reservation>();
    ApplicationConfig db = new ApplicationConfig();
    string cnx = db.getConnectionStringFromXML();
    NpgsqlConnection npgsqlcnx = new NpgsqlConnection(cnx);

    npgsqlcnx.Open();
    NpgsqlTransaction tran = npgsqlcnx.BeginTransaction();
    //  NpgsqlCommand command = new NpgsqlCommand("prc_get_reservations", npgsqlcnx);
    NpgsqlCommand command = new NpgsqlCommand("prc_get_list_resa", npgsqlcnx);

    command.CommandType = CommandType.StoredProcedure;

    if ((d1 != "") && (d1 != null))
    {
        command.Parameters.AddWithValue("@d1", DateTime.Parse(d1));
    }
    else
    {
        command.Parameters.AddWithValue("@d1", DateTime.Parse("01/01/1900"));
    }
    if ((d2 != "") && (d2 != null))
    {
        command.Parameters.AddWithValue("@d2", DateTime.Parse(d2));
        }
    else
    {
        command.Parameters.AddWithValue("@d2", DateTime.Parse("01/01/2100"));
    }


    command.Parameters.AddWithValue("@p_id_resa", p_id_resa);
    command.Parameters.AddWithValue("@p_state", p_state);
    command.Parameters.AddWithValue("@p_id_clt", p_id_clt);
    command.Parameters.AddWithValue("@p_id_chauff", p_id_chauff);
    command.Parameters.AddWithValue("@p_id_conv", p_id_conv);
    command.Parameters.AddWithValue("@p_id_fact", p_id_fact);
    command.Parameters.AddWithValue("@p_id_org", p_id_org);



    dr = command.ExecuteReader();

    int t = dr.FieldCount;
    lst_reservation = Reservations(dr);
    //    lst_reservation = Reservations(dr);
    npgsqlcnx.Close();
    return lst_reservation;
}

1 个答案:

答案 0 :(得分:1)

d1和d2必须声明为DateTime变量 - 不要将日期存储在字符串中。特别是在将这些日期传递给期望日期而不是字符串的数据库时。

很可能,您正在将字符串传递给数据库,而数据库正在以不同方式解释该字符串(例如,2月1日1月1日或1月2日?)。通过仅将日期传递给数据库,没有歧义。

因此而不是代码:

d1 = DateTime.Today.ToString("dd-MM-yyyy");

您将使用以下代码:

d1 = DateTime.Today;

同时更改此行:

WHERE  res.date_dep_resa >= (select distinct d1 from db_reservations) and res.date_dep_resa < (select distinct d2 from db_reservations)

为:

WHERE  res.date_dep_resa >= d1 and res.date_dep_resa < d2