mysql - The process order of SQL order by, group by, distinct and aggregation function? -


query like:

select distinct max(age), area t_user group area order area; 

so, process order of order by, group by, distinct , aggregation function ?

maybe different order same result, cause different performance. want merge multi-result, got sql, , parsed.so want know order of standard sql dose.

this bigger group by/aggregation/order by. want have sense of how query engine creates result set. @ high level, means creating execution plan, retrieving data table query's working set, manipulating data match requested result set, , returning result set caller. simple queries, or queries matched table design (or table schemas well-designed queries you'll need run), can mean streaming data table or index directly caller. more often, means thinking @ more detailed level, follow these steps:

  1. look @ query determine tables needed.
  2. look @ joins , subqueries, determine of table depend on other tables.
  3. look @ conditions on joins , in clause, in conjunction indexes, determine how space each table needed, , how work take extract portions of each table need (how query matches indexes or table stored on disk).
  4. based information collected steps 1 through 3, figure out efficient way retrieve data needed select list, regardless of order in tables included in query , regardless of order clause. step, "most efficient" defined method keeps working set small possible long possible.
  5. begin iterate on records indicated step 4. if there group clause, each record has checked against existing discovered groups before engine can determine whether or not new row should generated in working set. often, efficient way query engine conduct effective order step here, such potential rows results materialized working set, ordered columns in group clause, , condensed duplicate rows removed. aggregate function results each group updated records group discovered.
  6. once of indicated records materialized, such results of aggregate functions known, having clauses can evaluated.
  7. now, finally, order can factored in, well.
  8. the records remaining in working set returned caller.

and complicated was, it's beginning. doesn't begin account windowing functions, common table expressions, cross apply, pivot, , on , on. is, however, enough give sense of kind of work database engine needs do.


Comments

Popular posts from this blog

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

c++ - OpenMP unpredictable overhead -

javascript - Wordpress slider, not displayed 100% width -