最有效率的“多字段模糊匹配关键字查询”的解决办法

来源:互联网 发布:3d 投影 2d 矩阵 编辑:程序博客网 时间:2024/05/17 07:03
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
我们开发应用时,常常需要用到模糊查询。如果同一个条件需要匹配很多字段怎么办呢?通常,程序员会每个字段都在SQL中“fieldlike'%cond%'”一次。这样,SQL语句会长得惊人,碰上复杂一点的,甚至SQL语句会因为超长而被数据库拒绝执行。

其实,这个问题只要动动脑筋就很容易解决:

首先,将要匹配相同条件的字段连起来(field1+field2+...)成一个长字符串;
然后再Like“%cond%”就可以了。

不过这种方法有个问题,就是得权衡多表连接造成的效率降低。一般来说,单表内字段肯定应该连接后再统一like判断;表间字段,则需要先过滤后,再实行这个策略。

采取这个策略,不仅可以缩短SQL,而且能够有效地提高SQL的执行效率。

欢迎访问国内首家旅游比价搜索:
搜游记(http://www.soyoji.com)——搜索,让旅游更轻松!<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
原创粉丝点击