List:Internals« Previous MessageNext Message »
From:Konstantin Osipov Date:January 21 2009 11:04pm
Subject:Re: Optimization of repeated expressions
View as plain text  
* Baron Schwartz <baron@stripped> [09/01/14 04:41]:

> A friend asked me this question, and I thought I should ask it here
> before I try to figure it out on my own:
>     select id, pow(sum(x),4) +  pow(sum(x),3) +
>         pow(sum(x),2) + sum(x) as z
>     from foo
>     group by id
> Would mysql have to compute sum(x) four times, or is it smart enough to
> do it once then use the result four times?

The sum will be computed 4 times.

You should use an alias instead.

Optimization of repeated expressionsBaron Schwartz14 Jan
  • Re: Optimization of repeated expressionsKonstantin Osipov22 Jan