I'm building a dynamic reporting system. The report can of course query multiple tables.
The query declares which tables it will access, the fields that it needs for display, and the dependent fieds or fields not in the report, but in the "filter" part.
The problem is when a "filter" is not used, the field will not show up in the dynamically built SQL.
For example, the user doesn't choose to see a report for a particular product, but for all products, so therefore I don't constraint by the product field, but the product table is still included in FROM and joined.
When I run the query manually at the prompt, it takes a very long time to run (over 2min for 50 records) and when I take out the tables that are not used the query takes 0.03 seconds to run.
Is there any way to change this MySQL behavior? to complete ignore the tables that have no fields related to them, but are used in joins.
I'm trying to prevent coding so as to put the table names based on the criteria used.
Sorry can't give actuall query example because data is sensitive, but I can try to write out a similar query:
You can see, same tables, but when I use fields in those tables in the WHERE clause, it executes fast.
I hope i've made my problem clear, if not let me know.
Dan
The query declares which tables it will access, the fields that it needs for display, and the dependent fieds or fields not in the report, but in the "filter" part.
The problem is when a "filter" is not used, the field will not show up in the dynamically built SQL.
For example, the user doesn't choose to see a report for a particular product, but for all products, so therefore I don't constraint by the product field, but the product table is still included in FROM and joined.
When I run the query manually at the prompt, it takes a very long time to run (over 2min for 50 records) and when I take out the tables that are not used the query takes 0.03 seconds to run.
Is there any way to change this MySQL behavior? to complete ignore the tables that have no fields related to them, but are used in joins.
I'm trying to prevent coding so as to put the table names based on the criteria used.
Sorry can't give actuall query example because data is sensitive, but I can try to write out a similar query:
Code:
/** SLOW **/ SELECT c.number, c.name FROM customer AS c, invoice AS i, ordreLine AS o WHERE c.id = i.customerID AND i.id = o.invoiceID; /** FAST **/ SELECT c.number, c.name FROM customer AS c, invoice AS i, ordreLine AS o WHERE c.id = i.customerID AND i.id = o.invoiceID AND (i.date < 2008-02-14 AND o.qty > 5);
I hope i've made my problem clear, if not let me know.
Dan
Comment