栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

高精度的MySQL NOW()函数

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

高精度的MySQL NOW()函数

因此,在获得技巧后研究MySQL用户定义的函数,我能够使它起作用。

在Code :: Blocks中,我编写了这个简单的库文件

#include <stdlib.h>#include <stdio.h>#include <string.h>typedef long long longlong;    #include <mysql.h>#include <ctype.h>    #include <sys/time.h>#include <unistd.h>static pthread_mutex_t LOCK_hostname;extern "C" longlong PreciseNow(UDF_INIT *initid, UDF_ARGS *args, char *is_null, char *error);extern "C" my_bool PreciseNow_init(UDF_INIT *initid, UDF_ARGS *args, char *message);extern "C" void PreciseNow_deinit(UDF_INIT *initid);my_bool PreciseNow_init(UDF_INIT *initid, UDF_ARGS *args, char *message){ return 0; //optional}void PreciseNow_deinit(UDF_INIT *initid){ //optional}longlong PreciseNow(UDF_INIT *initid, UDF_ARGS *args, char *is_null, char *error){   struct timeval start;   long mtime, seconds, useconds;   gettimeofday(&start, NULL);   return start.tv_sec * 1000000 + start.tv_usec;}

它唯一要做的就是返回当前系统时间。我将lib文件放在’/ usr / lib / mysql / plugin /’中。其次,在数据库中,执行以下查询:

创建函数PreciseNow返回整数SONAME’libhrt.so’

最后,我将时间戳列转换为bigint(8字节整数)。也许这不是必需的,但是看到C函数返回相同的对象类型,为什么不呢?

现在,当运行SQL查询“ SELECT PreciseNow()”时,我获得了内置的“
NOW()”的微秒级精确版本!由于我对MySQL的了解非常有限,因此我认为这是对我的问题的完全合法和有效的解决方案?



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/416700.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号