Is there a difference in performance of HQL if you select individual columns vs selecting a whole object? When I use HQL to select a whole object the query takes about 7 min to run (table has about 5k records in it). If I explicitly define the columns the query runs in about 5 seconds.
HQL that takes 7min
Code:
getHibernateTemplate().find(
"select l from Location l inner join fetch l.city city inner join fetch l.city city left outer join fetch city.cityVariations cityVariations left join fetch l.state state inner join fetch l.country country order by l.locationTitleSourceText");
HQL take takes 5 seconds.
Code:
getHibernateTemplate().find("select l.id,l.eoag, l.oag,l.website, l.email,l.actualCityName,l.locationTitleSourceText, l.address2,l.address3,l.address4,l.address5, l.zipCode, country.countryCode, state.stateSourceText, city.destinationCityNameSourceText from Location l order by l.locationTitleSourceText");
I know that there is some joins in the first but not in the second, but even if I take out all the joins (inner and outer) it does not change the execution time of the query.
hbm
Code:
<class name="Location" table="db2inst1.LocLocations" >
<id name="id" column="Id" type="long">
<generator class="native" />
</id>
<property name="eoag" column="EoagCode" not-null="true" length="6" index="eoag_index" unique-key="CompositeUniqueKey"/>
<property name="brand" column="brand" not-null="true" length="25" unique-key="CompositeUniqueKey"/>
<property name="oag" column="OagCode" length="3" index="oag_index"/>
<property name="zipCode" column="ZipCode" not-null="false" length="30"/>
<property name="locationType" column="LocationType" not-null="true" length="1"/>
<property name="website" column="Website" length="75" not-null="false"/>
<property name="email" column="Email" length="75"/>
<many-to-one name="state" column="LocStateId" class="State" fetch="join" lazy="false" />
<many-to-one name="country" column="LocCountryId" class="Country" fetch="join" unique="false" lazy="false" not-null="true" />
<many-to-one name="serviceLocation" column="LocLocationServiceId" class="Location" fetch="join" />
<many-to-one name="afterHoursLocation" column="LocLocationAfterHoursId" class="Location" fetch="join" />
<many-to-one name="city" column="LocCityId" class="City" fetch="join" lazy="false" not-null="true" />
<property name="actualCityName" column="ActualCityName" length="255"/>
<map name="phoneMap" cascade="all,delete-orphan" inverse="true" >
<key column="LocLocationId"/>
<index column="PhoneType" type="string"></index>
<one-to-many class="LocationPhone"/>
</map>
<property name="fee" column="Fee" type="double" length="4" precision="2"/>
<property name="formerlyKnownAs" column="LocationFormlyKnownAs" length="255"/>
<property name="address2" column="Address2" index="address2" length="255"/>
<property name="address3" column="Address3" index="address3" length="255"/>
<property name="address4" column="Address4" length="255"/>
<property name="address5" column="Address5" length="255"/>
<property name="locationTitleSourceText" column="LocationTitle" index="location_title_index" not-null="true" length="255"/>
<property name="majorAirport" column="MajorAirport" type="boolean" not-null="true"/>
<property name="locationNotes" column="Notes" type="text" length="5000" />
<property name="locationLandMarkDetails" column="locationLandMarkDetails" type="text" length="500" />
</class>