How smart is the MySQL optimizer? If it sees an expression repeated many times, does it realize they’re all the same and not calculate the result for each of them?
I had a specific case where I needed to find out for sure, so I made a little benchmark. The query looks something like this:
|
1 |
<br>select sql_no_cache<br> pow(sum(rental_id), 1),<br> pow(sum(rental_id), 2),<br> pow(sum(rental_id), 3),<br> pow(sum(rental_id), 4),<br> pow(sum(rental_id), 5),<br> pow(sum(rental_id), 6),<br> pow(sum(rental_id), 7),<br> pow(sum(rental_id), 8),<br> pow(sum(rental_id), 10),<br> pow(sum(rental_id), 11),<br> pow(sum(rental_id), 12),<br> pow(sum(rental_id), 13),<br> pow(sum(rental_id), 14),<br> pow(sum(rental_id), 15),<br> pow(sum(rental_id), 16),<br> pow(sum(rental_id), 17),<br> pow(sum(rental_id), 18)<br> from sakila.rental;<br> |
I wrote a bash script to execute the query 500 times and redirect the results to /dev/null. Results: 31s wall-clock time.
The following is an alternative query that returns the same result:
|
1 |
<br>select sql_no_cache<br> pow(@sum := (select sum(rental_id) from sakila.rental), 1),<br> pow(@sum, 2),<br> pow(@sum, 3),<br> pow(@sum, 4),<br> pow(@sum, 5),<br> pow(@sum, 6),<br> pow(@sum, 7),<br> pow(@sum, 8),<br> pow(@sum, 10),<br> pow(@sum, 11),<br> pow(@sum, 12),<br> pow(@sum, 13),<br> pow(@sum, 14),<br> pow(@sum, 15),<br> pow(@sum, 16),<br> pow(@sum, 17),<br> pow(@sum, 18);<br> |
This query runs 500 iterations in 12s. So it looks like the optimizer was computing the SUM() 18 times in the first query. (A response on the internals mailing list agreed with this finding. Thanks Konstantin.)
Along the same lines, I benchmarked POW() and found that squaring a number is more than twice as fast to do with ordinary multiplication as with POW(). Does this matter? Sometimes… we had a client who was doing vector math in a many-dimensional space, and the POW() seemed to be taking a pretty significant amount of time; changing to ordinary multiplication made the query faster. (It was not the ultimate suggestion though.)
Resources
RELATED POSTS