简体   繁体   中英

Is DISTINCT applied before or after GROUP BY in a MySQL query?

Below is the query I'm running. Does the DISTINCT get applied before or after the GROUP BY ?

The table looks like this: id, state

The id is not unique, and an entity can have many entries with the same state or different states.

This behavior could drastically change my results. If DISTINCT is applied before GROUP BY , then it will only count each entity once throughout the entire set ( will only show up in one state ). If it happens after, then each entity will only be counted once per state , but could be counted in many states .

SELECT
    state,
    COUNT( DISTINCT entity_id ) AS count
FROM entities
GROUP BY state
ORDER BY count DESC;

My goal is to count each entity only once per state but to allow an entity to be counted in more than one states ... which is the behavior I would expect if DISTINCT is applied after GROUP BY .

The GROUP BY is applied first:

http://sqlfiddle.com/#!2/92876/1

Using the same query as you've got in your question on the following data:

CREATE TABLE Entities (
  state INT,
  id INT
);

INSERT INTO Entities VALUES
  (1, 1), (1, 1), (1, 1),
  (2, 1),
  (3, 1),
  (1, 2),
  (1, 3),
  (2, 3);

the following result was output:

STATE | COUNT
1     | 3
2     | 2
3     | 1

GROUP BY happens first. To do grouping, the database may look for a suitable index on state. That will result in something like this:

Stat ID
---- --
Iowa 1
Iowa 1
Utah 2
Utah 1
Utah 2

After grouping is done, distinct happens.

Iowa 1
Utah 2
Utah 1

And then follows count.

Iowa count-distinct = 1
Utah count-distinct = 2

Then ORDER BY count DESC kicks in.

Utah 2
Iowa 1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM