I have this query for gathering the information about a single order and its become quite complex.
I don\'t have any data to test with so i\'m asking, if anyone has
Well I once tried it myself to see the limit for the number of joins and I tested a join with 100 tables on mysql 5.0.4 if I recall it correctly.
I was given the following error:
Too many tables. MySQL can only use 61 tables in a join.
I think the limit for MySql 4 was 31 tables.
A word of advice if you plan to use that many tables in a query then it's time to start thinking about simplifying your query design.
I was searching for this as well, and then I found this other solution, adding "AND" as a part of a JOIN.
INNER JOIN kopplaMedlemIntresse
ON intressen.id = kopplaMedlemIntresse.intresse_id AND kopplaMedlemIntresse.medlem_id = 3
You don't have anywhere near the limit of JOINs for MySQL. Your number of joins isn't bad. However, joining on a derived table (your inner subquery) as you're doing can cause performance issues, since derived tables don't have indexes. Performing a join on a derived table without indexes can be slow.
You should consider making a real temporary table with indexes for joining, or figure out a way to avoid the subquery.
A JOIN in MySQL is basically like doing a lookup (seek) for each joined row. So, MySQL will have to perform many lookups if you are joining many records. It's less about how many tables you join than the number of rows that you join that can be a problem.
Anyway, MySQL will only perform so many seeks before it will give up and just read the whole table. It does a pretty good job at deciding which will be less expensive.
Perhaps the best thing you can do is help it guess by updating the index statistics with ANALYZE TABLE.
You can have one WHERE clause per SELECT. So your inner subquery will have a WHERE clause and your outer query will have a WHERE clause, and these get applied after the JOIN (at least logically, though MySQL will generally apply them first for performance).
Also, all of this is assuming you know how to properly use indexes.
I do not have any general advice, just my own expirience.
I had once a problem with very bad performance where I added tonz of tables using JOIN. Was around 20 JOIN I did. When I removed a few JOINS in a test the speed went up again. Due the fact I just needed single information I was able to replace most of the JOINS with sub selects. This solved my problem from 25 seconds for my query down to less then 1 second.
It is probably a fact of your table design, your amount of columns of the joined tables, and your indexes on the joined where clauses.