Question: I have two tables:

* trips: id\_trip, id\_object, trip\_date, delta (8980026 rows)
* ranges: id\_range, id\_object, date\_since, date\_until (18490 rows)

I need to optimize the following select statement

```
  select r.id_range, sum(t.delta) sum_deltas
    from trips t,
         ranges r
   where t.id_object = r.id_object
     and t.trip_date between r.date_since and r.date_until
group by r.id_range

```

according to the condition there is always exactly one matching row for trip in 'ranges'

* the trips table is constantly growing, but there are no updates or deletions
* table ranges may change from time to time in any way (deletions, updates, inserts), so function based index is not the way :(
* there are indexes on id\_object (in both tables) and date\_since (in trips)

Does anyone have an idea how to speed things up, is it even possible?

Answer: 
The QueryBuilder object has two relevant methods:
- getRootAliases()
- getRootEntities()

Calling getRootAliases on a a QueryBuilder which has the `from` clause set will return an array of all aliases; similarly, calling getRootEntities will return an array of the shortened class names of the selected entities.

```
$qb = $em->createQueryBuilder();
$qb->from('BundleName:EntityName', 'entityName');

var_dump($qb->getRootAliases()); // returns ['entityName']

```
I managed to get alias with

```
 $alias = current($builder->getDQLPart('from'))->getAlias();

```

(where $builder is an instance of Doctrine\ORM\QueryBuilder)