If this is your first visit, be sure to check out the FAQ by clicking the link above.
You may have to register before you can post: click the register link above to proceed.
To start viewing messages, select the forum that you want to visit from the selection below.
This is really strange. It is not identifying the rows in the subquery first and then using those in the main query. I know that you are unable to rewrite the query but perhaps we can run a test using a rewritten query to see how this responds?
SELECT col1, col2
FROM table b
JOIN (SELECT DISTINCT id FROM idxmail i JOIN matching m ON (i.proxyaddress = m.proxyaddress) WHERE m.mailnickname = '@var1') AS sub
ON (b.id = sub.id);
I would also have a look at the explain plan of this query.
One final question, what version of MySQL are you using?
iam sorry that i didnt mention that before.
The Version is 5.0.84
select Betreff, absender
from belege b
JOIN (SELECT DISTINCT id FROM idxmail i JOIN matchingtabelle m ON (i.proxyadresse = m.proxyadresse) WHERE m.mailnickname = 'brockmann@detectomat.com') AS sub
ON (b.dwdocid = sub.id);
We could try to get the subquery to process first by rewriting the query this way:
SELECT col1, col2
FROM table b
WHERE b.id IN (
SELECT DISTINCT id
FROM idxmail i
JOIN matching m ON i.proxyaddress = m.proxyaddress
WHERE m.mailnickname = '@var1')
This might identify the correct rows first in the subquery and then use this information in the main table.
select Betreff, absender
from belegarchiv b
where b.dwdocid in (
SELECT DISTINCT id
FROM idxmail i
JOIN matchingtabelle m ON i.proxyadresse = m.proxyadresse
WHERE m.mailnickname = '@var1')
I fear that without being able to rewrite this query it will be almost impossible to get this to work correctly. It is using a dependent subquery which means it is using the primary table using each dwdocid and then checking in the subquery to see if this exists.
You could try playing around with the subquery and see if it can be rewritten in such as way that this is processed first. Alternatively can you already populate a temporary table with the dwdocid and then select the temporary table in the subquery.
hm,
selecting a view in the subquery is even worse. Takes about 3 minutes.
I cant prepare one view for each possible @var1 anyway that would be more that 200 views.
It's a pity you cannot change the SQL statement. As you can see we can get 0.08seconds for execution time by changing this. Let me think about this more and see if there is anything we can do with this. Unfortunately it appears that the primary table is doing a full tablescan rather than using the ID's returned by the subquery and using the index to pick those entries.
That is good news!! It is a pain when you cannot touch the original code to make sure that it is written more efficiently for your own use but if this works that is better than nothing. Good luck!!