SQL to re-program comparison GROUP BY and DISTINCT

Suppose a demand for a weight according to the type + id, SQL are as follows

GROUP BY的SQL

SELECT type,id FROM table GROUP BY  type,id
如果要显示其它字段,可以使用MAX(xxx)

DISTINCT in SQL

SELECT DISTINCT type,id FROM table
如果要显示其它字段,可以使用DISTINCT ON(type,id) xxxx

How more than two SQL efficient?

GROUP BY principle

The type, id be sort sort to achieve weight

DISTINCT principle

The type, id in memory, similar to the Map structure. type, id is a map of the key, to achieve weight

compare results

The machine's memory enough,

DISTINCT efficient than GROUP BY
but the memory footprint is higher than GROUP BY

Guess you like

Origin www.cnblogs.com/JaminXie/p/11884196.html
Recommended