當我使用SQL_CALC_FOUND_ROWS
運行以下查詢或使用IN
子句在子查詢中指定所有顏色RED,BLUE,YELLOW
時,我得到的記錄數爲198
。但是,當我單獨執行每種顏色時,我會得到一個COUNT
,合計爲248 (72 + 59 + 118)
。額外的50行從哪裏來?我會想,如果我單獨做了他們,然後加上他們,我會得到198
,但我得到248
。我是否查看IN子句如何正確工作?我寧願用COUNT
代替SQL_CALC_FOUND_ROWS
因爲我發現SQL_CALC_FOUND_ROWS
要慢得多(+3秒),在mysql查詢中計數不同的原因是什麼?
這裏有一些數據:
1. I am running MySQL Version: 5.5.2
2. The tables engine is INNODB.
3. All the `CHECK TABLE .... EXTENDED` return OK.
-- Returns 198
SELECT SQL_CALC_FOUND_ROWS DISTINCT p.* FROM Persons AS p
INNER JOIN Colors AS c ON c.PersonId = p.Id
WHERE p.Name = 'John Doe' AND c.ColorName IN ('YELLOW','RED','BLUE');
SELECT FOUND_ROWS();
-- Returns 198
SELECT COUNT(1) as MyCount FROM (SELECT DISTINCT p.* FROM Persons AS p
INNER JOIN Colors AS c ON c.PersonId = p.Id
WHERE p.Name = 'John Doe' AND c.ColorName IN ('YELLOW','RED','BLUE')) all;
-- Returns 72
SELECT COUNT(1) as MyCount FROM (SELECT DISTINCT p.* FROM Persons AS p
INNER JOIN Colors AS c ON c.PersonId = p.Id
WHERE p.Name = 'John Doe' AND c.ColorName IN ('RED')) red;
-- Returns 59
SELECT COUNT(1) as MyCount FROM (SELECT DISTINCT p.* FROM Persons AS p
INNER JOIN Colors AS c ON c.PersonId = p.Id
WHERE p.Name = 'John Doe' AND c.ColorName IN ('BLUE')) blue;
-- Returns 118
SELECT COUNT(1) as MyCount FROM (SELECT DISTINCT p.* FROM Persons AS p
INNER JOIN Colors AS c ON c.PersonId = p.Id
WHERE p.Name = 'John Doe' AND c.ColorName IN ('YELLOW')) yellow;
http://stackoverflow.com/questions/18025315/no-mysql-records-return-when-past-a-certain-limit –
@MitchWheat - 是的,它被擱置了,這樣做更有意義,更清晰。 – Xaisoft