---
title: "MySQL的benchmark函数"
author: "libin123"
date: "2011-03-24T00:59:33Z"
tags: []
categories: ["数据库"]
slug: "mysql-benchmark"
draft: false
lastmod: "2011-03-24T00:59:33Z"
id: 973332
url: "https://dbua.iteye.com/blog/973332"
---

**MySQL**实现了很多独特的函数，有时候使用起来是非常的方便，或许这就是开源的好处吧。这里记录一下**benchmark**函数，一个用于测试MySQL函数性能的函数。benchmark函数只有两个参数，第一个是执行次数，第二个是要测试的函数或者表达式。返回的结果始终是0，执行时间才是我们需要的结果：

mysql&gt; select benchmark(1e8,current\_date());  
+——————————-+  
| benchmark(1e8,current\_date()) |  
+——————————-+  
| 0 |  
+——————————-+  
1 row in set (31.08 sec)

mysql&gt; select benchmark(1e8,abs(1));  
+———————–+  
| benchmark(1e8,abs(1)) |  
+———————–+  
| 0 |  
+———————–+  
1 row in set (1.31 sec)

除法的效率明显不如乘法：

mysql&gt; select benchmark(1e8,1\*1);  
+——————–+  
| benchmark(1e8,1\*1) |  
+——————–+  
| 0 |  
+——————–+  
1 row in set (1.45 sec)

mysql&gt; select benchmark(1e8,1/1);  
+——————–+  
| benchmark(1e8,1/1) |  
+——————–+  
| 0 |  
+——————–+  
1 row in set (19.47 sec)