Таблица Objects (integer idx not null Primary key и еще столбцы)
Таблица Metrics (integer idx not null Primary key, integer Object not null -> FK на Objects.idx, double Turn индексированное);

Составлял запросы по частям, типа REPL


Дальше ряд вроде бы одинаковыx запросов.

select  m.object /* o.idx */ as object_idx, max (m.turn) as max_turn
 from /* objects o, */ metrics m
where /* m.object = o.idx and */ m.turn > 45
group by m.object
order by 2 descending
--------------
PLAN SORT ((M ORDER FK_METRICS_1 INDEX (METRICS_IDX2)))

select distinct m.object /* o.idx */ as object_idx, max (m.turn) as max_turn
 from /* objects o, */ metrics m
where /* m.object = o.idx and */ m.turn > 45
group by m.object
order by 2 descending
--------
PLAN SORT (SORT ((M ORDER FK_METRICS_1 INDEX (METRICS_IDX2))))

select distinct m.object /* o.idx */ as objecy_idx, max (m.turn) as max_turn
 from  objects o,  metrics m
where  m.object = o.idx and m.turn > 45
group by m.object
order by 2 descending
--------------
PLAN SORT (SORT (JOIN (M ORDER FK_METRICS_1 INDEX (METRICS_IDX2), O INDEX (PK_OBJECTS))))

select m.object /*  o.idx */ as object_idx, max (m.turn) as max_turn
 from  objects o,  metrics m
where  m.object = o.idx and m.turn > 45
group by m.object
order by 2 descending
--------------
PLAN SORT (JOIN (M ORDER FK_METRICS_1 INDEX (METRICS_IDX2), O INDEX (PK_OBJECTS)))


1) Насколько понимаю, в присутсвии Group By, distinct тут не играет никакой роли, она автоматически получается ? Но оптимизатор добавляет безындексную сортировку. 2) Учитывая, что из таблицы Objects мы не выбираем значений (кроме FK), а наличие самой соотв. строки обеспечивается через FK, то добавлять её в план не нужно.


----------------------------------

select distinct /* m.object */ o.idx as object_idx, max (m.turn) as max_turn
 from  objects o,  metrics m
where  m.object = o.idx and m.turn > 45
group by /* m.object */  o.idx
order by 2 descending
--------------
PLAN SORT (SORT (SORT (JOIN (M INDEX (METRICS_IDX2), O INDEX (PK_OBJECTS)))))

select distinct m.object /* o.idx */ as object_idx, max (m.turn) as max_turn
 from  objects o,  metrics m
where  m.object = o.idx and m.turn > 45
group by  m.object /*  o.idx */
order by 2 descending
--------------
PLAN SORT (SORT (JOIN (M ORDER FK_METRICS_1 INDEX (METRICS_IDX2), O INDEX (PK_OBJECTS))))

Учитывая FK - запросы одинаковые, а план разный. Вотрой, вероятно, лучше.


-------------------------------


--
Написано в почтовом клиенте браузера Opera: http://www.opera.com/mail/

Ответить