Originally posted by DR_LaRRY_PEpPeR
that's what i was gonna ask, but you would think that it would process the COUNT() the same way, except counting the rows instead of returning them.
ya know, it should still have to examine the same amount or rows...
Well, if there is an index on the column it would only have to read the index which is much smaller than the table (so the likelihood of it sitting in memory is higher).
Plus, depending on how MySQL optimizes distinct it could factor out rows, since on the leaf-levels of the B tree you have your key in asc order:
1203
1204
1205
1205
1205
1205
So it can 'skip ahead' to the next largest value without counting all of them if it is smart as to the key distribution.
Take a look at the EXPLAIN output, here is what it says for Sybase:
Code:
SELECT COUNT( DISTINCT userid )
FROM user
QUERY PLAN FOR STATEMENT 1 (at line 1).
STEP 1
The type of query is INSERT.
The update mode is direct.
FROM TABLE
users
Nested iteration.
Table Scan.
Forward scan.
Positioning at start of table.
Using I/O Size 2 Kbytes for data pages.
With LRU Buffer Replacement Strategy for data pages.
TO TABLE
Worktable1.
STEP 2
The type of query is SELECT.
Evaluate Ungrouped COUNT AGGREGATE.
FROM TABLE
Worktable1.
Nested iteration.
Table Scan.
Forward scan.
Positioning at start of table.
Using I/O Size 2 Kbytes for data pages.
With MRU Buffer Replacement Strategy for data pages.
STEP 3
The type of query is SELECT.
Bookmarks