0
我無法解釋爲什麼,因爲這兩個查詢過濾,MySQL查詢與GROUPBY在同一領域中的WHERE子句
SELECT YEAR(TIMESTAMP) AS YEAR,
MONTH(TIMESTAMP) AS MONTH,
DAY(TIMESTAMP) AS DAY,
COUNT(IDENTITY) AS number,
type
FROM entity AS e,
annotation AS a
WHERE e.annotationIdAnnotation = a.idAnnotation
AND type = "Location"
GROUP BY YEAR(TIMESTAMP),
MONTH(TIMESTAMP),
DAY(TIMESTAMP),
type
SELECT YEAR(TIMESTAMP) AS YEAR,
MONTH(TIMESTAMP) AS MONTH,
DAY(TIMESTAMP) AS DAY,
COUNT(IDENTITY) AS number
FROM entity AS e,
annotation AS a
WHERE e.annotationIdAnnotation = a.idAnnotation
AND type = "Location"
GROUP BY YEAR(TIMESTAMP),
MONTH(TIMESTAMP),
DAY(TIMESTAMP)
第二個取第一個的100%以上。 我在類型上有一個btree索引,但我無法解釋爲什麼通過where子句已經過濾的字段上的組可以增強查詢的性能。
在此先感謝
在這兩種情況下都使用索引,正如我可以用EXPLAIN語句看到的那樣,並且驗證了select中沒有類型的第一個查詢以相同的方式執行。 – flea89 2013-05-01 13:14:01
@ user2088847我編輯了答案並添加了一個鏈接檢查,以清除您的疑惑。 – 2013-05-01 13:25:44
非常感謝! – flea89 2013-05-02 11:15:35