如何添加行值并在添加后获取唯一行

发布于 2024-10-18 16:24:04 字数 2904 浏览 5 评论 0原文

我正在使用 Coldfusion 为我的公司进行网站搜索。我有一个在 cfc 中调用的存储过程,它返回关键字的所有结果,没有限制。

然后,我执行一个子查询,该子查询要么根据安全设置限制我有权访问的数据,要么根据我将结果过滤为 5 个数据子集之一来限制数据。

现在我们使用视图搜索数据库,该视图是一个联合查询,允许我一次搜索多个表,结果返回“id、type、title、url、rank”列。对于如何找到此特定结果具有不同的排名值,最终会产生具有不同排名值的重复结果...

现在我的老板希望我将所有排名值相加,更新 1 条记录,并删除其余的记录重复...

因此,例如,如果我正在搜索 hte 单词 Business

我有不同的结果,例如,在标题中找到,+500,与标题 +1000 完全匹配,在描述中找到 +200

但问题是,当我尝试时循环遍历所有结果,会显着增加性能消耗。

因此,我正在寻找一种替代方法来总结排名值,然后摆脱结果。

下面是我整个过程的基本逻辑流程

  1. 执行存储过程,它在视图中搜索关键字,以不同的方式在不同字段中搜索,生成具有不同排名值的重复结果。

  2. 消除那些我无权访问的搜索结果,如果我选择将结果过滤到某个结果子集中,例如书籍、杂货、植物,请组成您喜欢的任何类别:)

在这里我将向您展示代码:

 
        <!--- check if this already exists --->
        <cfquery name="check_match" dbtype="query">
            select id, sum(rank) as total
            from get_matches
            where url = '#get_matches.url#'
            group by id
        </cfquery>

        <cfif check_match.total gt 1>

            <!--- add the two rank values --->
            <cfset my_rank = val(check_match.total) />

            <!--- update the search results --->
            <cfset get_matches.rank[get_matches.currentrow] = javacast("int",my_rank) />

            <!--- get a list of rows that has that url --->

            <!--- eliminate all other rows --->
            <cfquery name="get_matches_bot" dbtype="query">
                select id, type, title, url, rank
                from get_matches
                where url <> '#get_matches.url#'
                group by id, type, title, url, rank
            </cfquery>

            <cfquery name="get_matches_top" dbtype="query">
                select id, type, title, url, rank
                from get_matches
                where url = '#get_matches.url#'
                and rank = #my_rank#
                group by id, type, title, url, rank
            </cfquery>

            <cfquery name="get_matches" dbtype="query">
                select id, type, title, url, rank
                from get_matches_top
                union
                select id, type, title, url, rank
                from get_matches_bot
                group by id, type, title, url, rank
                order by rank desc, title asc
            </cfquery>


        </cfif>

    </cfloop>

然后,完成所有这些操作后,将帮助双方获得具有排名值总数的唯一行,这些行在 #2 结果中具有记录。

    <cfquery name="get_matches" dbtype="query">
        select id, type, title, url, rank
        from get_matches
        group by id, type, title, url, rank
        order by rank desc, title asc
    </cfquery>

必须有一种更好的方法,在性能方面,对排名值求和,消除重复行而不循环。

有什么想法或建议吗?

I am using Coldfusion, to do a site search for my company. I have a stored procedure being called within a cfc, that returns all results for a keyword, no limits.

Then I do a subquery that either restricts what data I have access to based on security settings, or limits it based on me filtering the result to 1 of 5 subsets of data.

Now we are searching the database using a view, and that view is a union all query, allowing me to search multiple tables at once, and the result returns columns of "id, type, title, url, rank". With different rank values for how it found this specific results, which ends up with duplicate results with different rank values....

Now my boss, wants me to add up all the rank values, update 1 record, and remove the rest of the duplicates...

So for example, if i was searching for hte word business

And I had different results like, found in title, +500, exact match for title +1000, found in description +200

But the problem is that when I tried to loop thru all thru all the results, it added significant performance drain.

So I am looking for an alternative way to sum up the rank values, and then get rid of the results.

Below is my basic logic flow for the whole process

  1. Do Stored Procedure which searches the view for a keyword, searches in different fields in different ways generating duplicate results with different rank values.

  2. Eliminates search results of those that i do not have access to, and if i had chosen to filter results into a certain subset of results, like in books, groceries, plants, make up any categories you like :)

Here i will show you the code:

  <cfloop query="get_matches">
        <!--- check if this already exists --->
        <cfquery name="check_match" dbtype="query">
            select id, sum(rank) as total
            from get_matches
            where url = '#get_matches.url#'
            group by id
        </cfquery>

        <cfif check_match.total gt 1>

            <!--- add the two rank values --->
            <cfset my_rank = val(check_match.total) />

            <!--- update the search results --->
            <cfset get_matches.rank[get_matches.currentrow] = javacast("int",my_rank) />

            <!--- get a list of rows that has that url --->

            <!--- eliminate all other rows --->
            <cfquery name="get_matches_bot" dbtype="query">
                select id, type, title, url, rank
                from get_matches
                where url <> '#get_matches.url#'
                group by id, type, title, url, rank
            </cfquery>

            <cfquery name="get_matches_top" dbtype="query">
                select id, type, title, url, rank
                from get_matches
                where url = '#get_matches.url#'
                and rank = #my_rank#
                group by id, type, title, url, rank
            </cfquery>

            <cfquery name="get_matches" dbtype="query">
                select id, type, title, url, rank
                from get_matches_top
                union
                select id, type, title, url, rank
                from get_matches_bot
                group by id, type, title, url, rank
                order by rank desc, title asc
            </cfquery>


        </cfif>

    </cfloop>

Then after all that is done to help both get unique rows that have total number of rank values for where they had records in the #2 results.

    <cfquery name="get_matches" dbtype="query">
        select id, type, title, url, rank
        from get_matches
        group by id, type, title, url, rank
        order by rank desc, title asc
    </cfquery>

There has to be a better way, performance wise, to sum the rank values, get rid of duplicate row's without looping.

Any thoughts or suggestions?

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

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

发布评论

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

评论(1

不羁少年 2024-10-25 16:24:04

我发现答案是在排名字段周围进行求和,这样速度就快了。

I figured out the answer was to do a sum, around the rank field, and that made it fast.

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