int转换为VARBINARY并再次返回

int转换为VARBINARY并再次返回

本文介绍了将c ++ int转换为VARBINARY并再次返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚发现除了 VAR 之外的所有数据类型都不会改变它们的 LENGTH 。这让我非常低效,因为可能有 INT 字段,如 autoincrement PRIMARY 不会改变其中 1 BIGINT 列中将占用整个8字节。 p>

我有很多列,插入后不会改变,但可能会很大。而不是使用所有 BIGINT 列,我想使用 VARBINARY



我想在c ++中将数据操作为 int ,然后将它存储为 VARBINARY mysql。



如何在c ++中进行这些转换?

解决方案



如果您查看MySQL文档,您会看到

让我们取一个64位的值,如0xDEADBEEF2B84F00D。如果将它存储在 BIGINT 字段中,它需要8个字节(存储为二进制值。)



如果你将它存储为 VARCHAR ,即使你假设数字总是十六进制,我们说的长度字段为1或2字节,然后另一个16字节十六进制字符! 18字节,并且由于不得不转换为/从ASCII转换,性能损失,这是一个可怕的折衷!



让我们看看相反的情况下,0x01。存储在 BIGINT 字段中,它仍然是8个字节。使用与以前相同的约定,它可能在您的数据库中3个字节。甚至不值得,在我看来。特别是当你考虑数据库如何用一堆ASCII数据执行时。



只是不要这样做。按照设计的方式使用数据库。


I've just discovered that all but VAR data types do not vary their LENGTHs. This strikes me as extremely inefficient since there may be INT fields like an autoincrement PRIMARY that never change where a 1 in a BIGINT column will take the full 8 bytes.

I have many columns that won't change once they're inserted but have the potential to be large. Instead of using all BIGINT columns, I'd like to use VARBINARY instead.

I want to manipulate the data as an int in c++ yet store it as VARBINARY in mysql.

How can these conversions be made in c++?

解决方案

I would highly recommend against this.

If you look at the MySQL documentation for VARCHAR, you'll see that

Let's take a 64-bit value like 0xDEADBEEF2B84F00D. If you store that in a BIGINT field, it takes 8 bytes (it's stored as a binary value.)

If you store that as a VARCHAR, even if you assume the numbers are always hexadecimal, we're talking 1 or 2 bytes for the length field, and then another 16 bytes for the hexadecimal characters! 18 bytes, and the performance lost by having to convert to/from ASCII, this is a terrible trade-off!

Let's look at the opposite case, 0x01. Stored in a BIGINT field, it's still 8 bytes. Using the same convention from before, it's probably going to be 3 bytes in your database. Not even close to being worth it, in my opinion. Especially when you consider how poorly the database would perform with a bunch of ASCII data.

Just don't do it. Use the database the way it was designed.

这篇关于将c ++ int转换为VARBINARY并再次返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 03:34