I have problem with query, who running in 2 seconds. Because subquery return 600 line, I need query without IN operator or faster query.
Currently, I have this query :
SELECT tag.idtag, tag, COUNT(*) AS nombre
FROM tag, questtag
WHERE tag.idtag = questtag.idtag
AND idquestion IN (
SELECT question.idquestion
FROM question, questtag
WHERE question.idquestion = questtag.idquestion AND idtag IN (1)
GROUP BY question.idquestion
HAVING COUNT(*) = 1
)
GROUP BY tag
ORDER BY nombre DESC, tag
LIMIT 0, 24
I want say to sql : "Get other tag related with Javascript tag (intersection constraint) like stackoverflow."
I have 3 tables : question, questag and tag. The question and tag are related with questtag table.
Sql schema :
QUESTION (idquestion, question) :
1, "Javascript and Jquery"
2, "HTML, CSS, Javascript"
....
QUESTTAG (#idquestion, #idtag) :
1, 1
1, 2
2, 3
2, 4
2, 1
....
QUESTTAG (#idtag, tag) :
1, Javascript
2, Jquery
3, HTML
4, CSS
....
With Javascript, I have to get Jquery, HTML, CSS. With Javascript, HTML, I have to get CSS.
The following recommendations will help you in your SQL tuning process.
You'll find 3 sections below:
SELECT
tag.idtag,
tag,
COUNT(*) AS nombre
FROM
tag,
questtag
WHERE
tag.idtag = questtag.idtag
AND idquestion IN (
SELECT
question.idquestion
FROM
question,
questtag
WHERE
question.idquestion = questtag.idquestion
AND idtag IN (
1
)
GROUP BY
question.idquestion
HAVING
COUNT(*) = 1
ORDER BY
NULL
)
GROUP BY
tag
ORDER BY
nombre DESC,
tag LIMIT 0,
24