我想提高以下查詢的性能。使用帶IN子句的嵌套子查詢提高MySQL查詢的性能
SELECT DISTINCT StringId
FROM translations
WHERE status = 1
AND TranslationId IN
(
SELECT Max(TranslationId)
FROM translations
WHERE languageId = 2
AND TranslationId > 0
GROUP BY StringId;
此查詢當前需要永久因爲In子句。
下面的子查詢
SELECT Max(TranslationId)
FROM translations
WHERE languageId = 2
AND TranslationId > 0
GROUP BY StringId;
收益16,000行,並與16,000行子句什麼是這麼長時間。
,我想出了一個改進的查詢是
SELECT DISTINCT t1.StringId
FROM translations t1
INNER JOIN
(
SELECT Max(TranslationId) MaxTranslationId,
StringId
FROM translations
WHERE languageId = 2
AND TranslationId > 0
GROUP BY StringId
) t2 ON t1.StringId = t2.StringId
AND t1.TranslationId = t2.MaxTranslationId
WHERE status = 1;
我希望內部聯接將返回只喜歡只用在子句同時匹配的StringID和Max(TranslationId)的行。
如果這是一個正確的方法來處理,有人可以給我一個答案嗎?
它是MySQL還是SQL Server?選一個。他們不一樣。 – Siyual
@Siyual我說的標題中的MySQL :) – CoderSpinoza
@Mike感謝您的編輯:) – CoderSpinoza