Hi everyone.
I think I have solved the problem..
It's a little complecated.
I have a table with profiles and their data called Kunder (costumers in Danish)
Then I have a table with a lot of choices.
And at last I have a table with all the choices each profil has checkmarked called Valg (choices in Danish)
When a costumers (ex. costumer ID 1) checkmarkes ex. choice number 5 it puts data in the table Valg with KundeID 1 and Valg 5
So if a costumer has checkmarked 5 choices there are 5 rows in the Valg table called
1 5
1 7
1 2
1 9
1 15
ex.
At the top of the page I then have a filter so when I choose the filter 5 it only shows the costumers who has checkmarked choise number 5.
But the tricky part is that I have 4 filters. So if I choose ex. 5, 7, 9 and 15 it has to check the Valg table and find the costumers who has checkmarked all 4 choices.
Therefore I have it like this:
SELECT * FROM Valg v1
JOIN Valg v2 ON (v1.KunID = v2.KunID)
JOIN Valg v3 ON (v1.KunID = v3.KunID)
JOIN Valg v4 ON (v1.KunID = v4.KunID)
LEFT JOIN Kunder ON (v1.KunID = Kunder.KundeID)
The $opt1-4 is then something like this
$opt1 = " AND v1.Choice='1' "
$opt2 = " AND v2.Choice='4' "
$opt3 = " AND v3.Choice='8' "
$opt4 = " AND v4.Choice='13' "
so the final query could looks like this ex. if all 4 filters is in use :
SELECT * FROM Valg v1
JOIN Valg v2 ON (v1.KunID = v2.KunID)
JOIN Valg v3 ON (v1.KunID = v3.KunID)
JOIN Valg v4 ON (v1.KunID = v4.KunID)
LEFT JOIN Kunder ON (v1.KunID = Kunder.KundeID)
WHERE 1 AND v1.Choice='1'
AND v2.Choice='4'
AND v3.Choice='8'
AND v4.Choice='13'
AND Branche='Musik'
AND EmailValid='x'
GROUP BY v1.KunID
ORDER BY Kunder.Vip DESC, Kunder.WogE DESC, Kunder.KundeNavn
You can see the funktion here, though it's in Danish.
http://www.festfagbogen.dk/Liste.php?Branche=Musik
The problem with it is that when no filters are in use when you enter the site, the query makes about 758.000 rows because it prints out every possible way the checkmarks for ex, costumer 1 can be combined. If costumer 1 has checkmarked 11 choices it gets 9x9x9x9=6561 rows.
And with about 100 costumers in this category it makes 758.000 rows which it has to GROUP so each costumer is only shown ones and order them afterward.
This took 17 seconds.
But now I have done it like this.
IF ($valg2>''){$join2='JOIN Valg v2 ON (v1.KunID = v2.KunID)';}
IF ($valg3>''){$join3='JOIN Valg v3 ON (v1.KunID = v3.KunID)';}
IF ($valg4>''){$join4='JOIN Valg v4 ON (v1.KunID = v4.KunID)';}
$getlist=mysql_query("
SELECT * FROM Valg v1
$join2
$join3
$join4
LEFT JOIN Kunder ON (v1.KunID = Kunder.KundeID)
WHERE 1 $opt1 $opt2 $opt3 $opt4 AND Branche='$branche' AND EmailValid='x'
GROUP BY v1.KunID
ORDER BY Kunder.Vip DESC, Kunder.WogE DESC, Kunder.KundeNavn
This way when you enter the site and no filters are in use, it doesn't add the Valg 4 times in the query and it takes no time at all.
But when you take 2 or more filters in use it adds JOINs in the query.
When all the filters are in use it can max be 4x4x4x4=256 rows per costumer and when all 4 filters are in use there are only 10-20 left of the 100 costumers, so it will make max 256x20=5120 rows.
That's a lot less than 758.000 and now there has to be 3000 costumers matching all 4 filters for it to take 17 second, and there are only 100 in the largest category, so it's safe for now :o)
Well it was a long explanation, but all your comments halped me figure out where the problem was, and I got it solved :o)
Thanks a lot
Michael