views:

46

answers:

1

I have quite complicated view in mysql, like

select filter.id as filter_id, person.id, person.name
from person, filter
inner  join 
...
left join
...
where person_match_filter_condition ...
group by filter.filter_id, person.id, person.name

Query filters person which corresponds domain specific conditions.

Typical use of view is:

select * from where filter_id = some_value

Problem is that mysql cannot optimize query. It applies confition by filter_id AFTER get data for all filters - very ineffective. Idea to get filter_id from other tables is not good for my case.

How can I transform my query to make it more effective?

A: 

Wrap the long query in a procedure, and pass the filters to the procedure call as parameters. Then instead of using views you call the procedure, the procedure will build you the entire query and will run optimized query.

Pentium10
Thanks for your answer!I have considered this. Unfortunately:1) I cannot provide for every possible parameter, I need to join the query flexibly with lots other conditions. I do not want use dynamic SQL.2) Java back-end uses Hibernate. Stored procedures are compatible with ORM badly.Any further ideas?
Just provide 1 string as parameter, the full clause you passed to the view. I don't know Hibernate, I can't help you on that one.
Pentium10