PHP:在 MySql 中存储 IP 的正确方法以及在数百万行中搜索 IP 的最快方法

发布于 2024-09-28 22:42:04 字数 150 浏览 0 评论 0原文

我使用 inet_aton 将 IPv4 地址存储在“int unsigned”列类型中。 【我这样做对吗?是否需要使用“无符号”?] 这个特定的列也被索引。由于整个表中将有数百万行和多行包含相同的 IP,搜索这些行的最快方法是什么?

..或者我的处理方式是错误的吗?

I'm storing IPv4 addresses in a "int unsigned" column type with inet_aton. [Am I doing this right? And is using "unsigned" necessary?] This particular column is indexed as well. Since there will be millions of rows and multiple rows containing the same IP throughout the entire table what will be the fastest way to search for these rows?

..or am I going about this the wrong way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

请恋爱 2024-10-05 22:42:04

使用 inet_aton 是执行此操作的正确方法,因此您不会存储额外的无意义信息(对于任何给定的 3 个数字,能够存储大于 256 的值是没有意义的)。它产生一个 32 位数字,该数字适合 unsigned int。

在 int 列上建立索引将使按 IP 地址进行快速查找。如果您的数据库变得非常大,那么在 MySQL 中存储此类内容就会开始遇到扩展问题。

我假设您不会这样做,但会指出,在 RDBMS 中存储大型繁忙站点的完整日志信息通常被认为是一件坏事。您不需要数据库保证的关系完整性属性,并且您写入的条目比您读取的条目多得多。考虑使用 nosql,或者附加到平面文件,并在必要时使用专用程序解析日志。

Using the inet_aton is the right way of doing this, so you're not storing extra meaningless info (no point in being able to store a value greater than 256 for any given 3 numbers). It results in a 32 bit number, which will fit into the unsigned int.

Indexing on the int column will make lookups by IP address quick. If your database gets REALLY big, you'll start running into scaling problems storing this sort of thing in MySQL.

I'll assume you're not going to do that, but will point out that storing complete log info for a large busy site in an RDBMS is generally agreed to be a Bad Thing(tm). You don't need the relational integrity properties the database guarantees, and you write many more entries than you read. Consider nosql, or appending to flat files instead, and parsing your logs when necessary using a dedicated program.

几味少女 2024-10-05 22:42:04

我这样做对吗?并且正在使用
“未签名”有必要吗?

是的。如果没有 unsigned,较高的 IP 地址将无法正确存储,而使用 int(而不是 varchar)可以最有效地存储它。

最快的搜索方式是什么
对于这些行?

就搜索优化而言,这取决于您要搜索的内容(附加表等)。一般来说,对 unsigned int 列建立索引可以提供快速的性能。

Am I doing this right? And is using
"unsigned" necessary?

Yes. Without unsigned higher ip addresses will not be stored properly and using int (instead of varchar) stores it most efficiently.

what will be the fastest way to search
for these rows?

As far as search optimization, that depends on what all you're searching for (additional tables, etc...). In general, indexing an unsigned int column gives you fast performance.

飘逸的'云 2024-10-05 22:42:04

是的,这是在 MySQL 中存储 IP 地址的最佳方式。

如果您查看 INET_ATON 您可以看到,建议使用 UNSIGNED INT 列,否则任何第一个八位字节超过 127 的 IP 地址将无法正确存储。

这也是一种非常快速的搜索方式。 MySQL 可以很好地处理整数列,通过对此列建立索引并在搜索中使用 INET_ATON,您可以实现非常快速的查询。

Yes, this is the best way to store IP addresses in MySQL.

If you look at the documentation for INET_ATON you can see that it is recommended to use an UNSIGNED INT column or any IP address with the first octed over 127 will not be stored correctly.

This is also a very fast way to do searches. MySQL handles integer columns very well, and by indexing this column and using INET_ATON in your search, you can achieve very fast queries.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文