Question

So, I have a relatively simple query which does a summation of values on 11 different columns. The query itself runs very fast but it seems that grouping by 11 columns is extremely slow.

This is an excerpt from an explain file, it shows that query ran for two seconds but spent almost 17 seconds grouping the data. Is there anything to optimize this?

Query statistics:
-----------------

  Table map :
  ----------------------------
  Internal name     Table name
  ----------------------------
  t1                ustroj
  t2                tfinkbilanca
  t3                tfinkkontogodina
  t4                tbaseanalitikanaziv

  type     table  rows_prod  est_rows  rows_scan  time       est_cost
  -------------------------------------------------------------------
  scan     t1     77         12        77         00:00.00   2       

  type     table  rows_prod  est_rows  rows_scan  time       est_cost
  -------------------------------------------------------------------
  scan     t2     76231      77376     76231      00:00.36   90      

  type     table  rows_prod  est_rows  rows_scan  time       est_cost
  -------------------------------------------------------------------
  scan     t3     152462     19203     76231      00:00.67   1       

  type     rows_prod  est_rows  time       est_cost
  -------------------------------------------------
  nljoin   152462     41266     00:01.05   337835  

  type     rows_prod  est_rows  time       est_cost
  -------------------------------------------------
  nljoin   76231      644       00:01.08   37456   

  type     table  rows_prod  est_rows  rows_scan  time       est_cost
  -------------------------------------------------------------------
  scan     t4     151780     33        75890      00:00.77   0       

  type     rows_prod  est_rows  time       est_cost
  -------------------------------------------------
  nljoin   76231      632       00:01.86   38828   

  type     rows_prod  est_rows  rows_cons  time
  -------------------------------------------------
  group    15158      316       76231      00:16.55

  type     rows_sort  est_rows  rows_cons  time
  -------------------------------------------------
  sort     55         316       15158      00:16.93
Was it helpful?

Solution

First, I suggest to you validate the time of the query "saving" it into a temporary table (include the into temp tmp01 with no log) , which you answer at the comments the time don't change.

Considering the large fields mentioned (char(256)) as description of something , my suggestion is :

  • At group by use only key columns. Use they id/codes if possible.
  • Execute the SQL with "group by" saving into a temporary table.
  • Execute other SQL over this temporary table and join to get the descriptions. (char(256) fields)

please, accept the answer if they really work for you... if is useful but not solve your problem only set +1 for it

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top