hql-запрос, приносящий слишком много данных - PullRequest
0 голосов
/ 01 января 2011

Я бы хотел, чтобы запрос, приведенный ниже, возвращал объект Employee с выделениями, попадающими в запрошенный диапазон дат.

Запрос возвращает все распределения. Он также выполняет три запроса к базе данных, которые я не уверен, что понимаю (третий может быть из-за того, что у меня размер пакета равен 100 для субъектов деятельности).

Как изменить этот запрос, чтобы эффективно возвращать только ассигнования за запрошенный период?

Приветствия
Berryl

БАЗА ДАННЫХ SQLS:

NHibernate: select resource0_.ResourceId as ResourceId0_, resource0_.BusinessId as BusinessId0_, resource0_.ResourceName as Resource4_0_, resource0_.OwnerName as OwnerName0_, resource0_.EmployeeNumber as Employee6_0_, resource0_.FirstName as FirstName0_, resource0_.LastName as LastName0_, resource0_.DepartmentId as Departme9_0_, resource0_.ResourceType as Resource2_0_ from Resources resource0_ 
inner join Allocations allocation1_ on resource0_.ResourceId=allocation1_.ResourceId 
where resource0_.BusinessId=@p0 
and (allocation1_.StartTime between @p1 and @p2);@p0 = '000001' [Type: String (0)], @p1 = 12/27/2010 12:00:00 AM [Type: DateTime (0)], @p2 = 1/2/2011 11:59:59 PM [Type: DateTime (0)]

NHibernate: SELECT allocation0_.ResourceId as ResourceId1_, allocation0_.AllocationId as Allocati1_1_, allocation0_.AllocationId as Allocati1_2_0_, allocation0_.ActivitySubjectId as Activity2_2_0_, allocation0_.ResourceId as ResourceId2_0_, allocation0_.StartTime as StartTime2_0_, allocation0_.EndTime as EndTime2_0_, allocation0_.PostingTime as PostingT6_2_0_ 
FROM Allocations allocation0_ WHERE allocation0_.ResourceId=@p0;@p0 = 98304 [Type: Int32 (0)]

NHibernate: SELECT activitysu0_.ActivitySubjectId as Activity1_3_0_, activitysu0_.BusinessId as BusinessId3_0_, activitysu0_.Description as Descript4_3_0_, activitysu0_.ActivitySubjectType as Activity2_3_0_ 
FROM ActivitySubjects activitysu0_ WHERE activitysu0_.ActivitySubjectId in (@p0, @p1, @p2, @p3);@p0 = 32784 [Type: Int32 (0)], @p1 = 32854 [Type: Int32 (0)], @p2 = 32860 [Type: Int32 (0)], @p3 = 32861 [Type: Int32 (0)]

HQL QUERY

    public Resource GetResourceForDateRange<T>(string businessId, DateRange period) where T : Resource
    {
        Check.RequireStringValue(businessId, "businessId");
        Check.RequireNotNull(period);

        const string hql =
            @"
                                select r 
                                from Resource r 
                                inner join r.Allocations as a
                                where r.BusinessId = :businessId 
                                and a.TimeRange.StartTime between :periodStart and :periodEnd";

        return _session.CreateQuery(hql)
            .SetString("businessId", businessId)
            .SetDateTime("periodStart", period.Start)
            .SetDateTime("periodEnd", period.End)
            .UniqueResult<Resource>();

    }

КАРТЫ

<id name="Id" type="System.Int32" unsaved-value="0">
  <column name="ResourceId" />
  <generator class="hilo" />
</id>

<discriminator column="ResourceType" type="System.String" />

<property name="BusinessId" length="50" not-null="true" unique="true" unique-key="DomainSignature" index="ResourceDomainSignature" />
<property name="ResourceName" length="75" not-null="true" />
<property name="OwnerName" length="75" not-null="true" />

<set access="field.camelcase-underscore" cascade="all-delete-orphan" inverse="true" name="Allocations">
  <key foreign-key="Allocations_Resource_FK">
    <column name="ResourceId" />
  </key>
  <one-to-many class="Allocations.Allocation" />
</set>

<subclass name="Resources.HumanResources.Employee" discriminator-value="EMPLOYEE">
    ...
</subclass>

1 Ответ

1 голос
/ 01 января 2011

Причина, по которой он выполняет 3 запроса, скорее всего из-за отложенной загрузки.Таким образом, он загружает все объекты, связанные с объектом Resource.Вот почему он получает все распределения.

Попробуйте выполнить выборочное соединение

from Resource r 
left join fetch r.Allocations as a
where r.BusinessId = :businessId 
and a.TimeRange.StartTime between :periodStart and :periodEnd
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...