大网数据广告活动
服务器运维问答

Is it better to generate "UUID" and "TIMESTAMP" within the NodeJS application or using the database built-in functions?

· 52CE 我爱测

I am writing a TypeScript-NodeJS application and want to handle object id s and created_at TIMESTAMP within the NodeJS application, instead of using MySQL or Cassandra built-in UUID or TIMESTAMP generator. 1- First of all I like …

问题原文

原帖链接 · best_of_man · CC BY-SA 4.0

I am writing a TypeScript-NodeJS application and want to handle object ids and created_at TIMESTAMP within the NodeJS application, instead of using MySQL or Cassandra built-in UUID or TIMESTAMP generator.

1- First of all I like to know is this a good idea to generate id and created_at values within a web server application instead of letting databases generate them?

2- Secondly, I want to know if I use database's built-in functions like uuid() or toTimestamp(now()) in Cassandra and UUID_TO_BIN(UUID()) in MySQL will this add more overhead/latency to my application compare to using NodeJS uuid() library?

已采纳解答

原帖链接 · Rick James · CC BY-SA 4.0

Re MySQL or MariaDB:

Mostly the performance difference is negligible. In general, fetching a row is much more costly than any 'standard' function being used in it.

If you do need the value in both places (client and server), then it would be more convenient for you to compute it in one place.

MySQL 8.0 and MariaDB 10.7 have an optimization on UUID that is probably not available in any client -- this rearranges the bits in UUID to make them work somewhat like TIMESTAMP, thereby improving "locality of reference" for a variety of actions. Without this, UUIDs are bad for performance in huge tables. Since the optimization only works for Type 1, it is probably not available to the client.

TIMESTAMPs cannot be trusted to be unique. Hence, they should not be used [alone] in a PRIMARY KEY.

UUIDs are useful when you have multiple clients independently creating ids. Even so, usually, an auto_inc is often just as easy.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2023-02-06)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。