首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >提高PostgreSQL内网IP地址正则表达式匹配的性能

提高PostgreSQL内网IP地址正则表达式匹配的性能
EN

Stack Overflow用户
提问于 2013-03-19 02:37:18
回答 1查看 550关注 0票数 0

我使用的是从8.3派生出来的PostgreSQL的MPP版本。

我正在尝试使用where子句优化select语句,以便只选择具有私有源IP地址和公共目标IP地址的行。我有两个类型为source_ip和destination_ip的列。我觉得下面的操作不是最有效的方法,因为我正在进行正则表达式匹配,以确定IP是公有IP还是私有IP:

代码语言:javascript
运行
复制
where  (text(source_ip) like '10.%'  
    or text(source_ip) like '192.168.%'
    or text(source_ip) ~ E'^172\.((1[6-9])|(2[0-9])|(3[0-1]))\..+')
    and text(destination_ip) not like '10.%'  
    and text(destination_ip) not like '192.168.%'
    and text(destination_ip) !~ E'^172\.((1[6-9])|(2[0-9])|(3[0-1]))\..+';

怎样才能使上面的where子句更有效?有没有一种方法可以不使用正则表达式,而是使用内置的postgresql函数来更快地对inet类型进行操作?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-03-19 02:57:42

代码语言:javascript
运行
复制
where
    (
        inet '10/8' >> source_ip
        or inet '192.168/16' >> source_ip
        or sourceip >= inet '172.16/16' and sourceip < inet '172.32/16'
    )
    and not inet '10/8' >> destination_ip
    and not inet '192.168/16' >> destination_ip
    and not (destination_ip >= inet '172.16/16' and destination_ip < inet '172.32/16')
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15484322

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档