ORACLE PL/SQL:按情况排序性能

发布于 2024-10-11 04:48:55 字数 885 浏览 3 评论 0原文

我目前有一个服务器端分页查询:

SELECT * FROM (SELECT a.*, rownum rnum FROM (
        ( /* Insert Query Here */ ) ) a
      WHERE rownum <= ((page_number + 1) * page_size))
      WHERE rnum >= (((page_number  + 1) * page_size)) - (page_size - 1);

然而,问题是试图确定用户正在排序的内容,因为它与网格视图相关联。目前,我正在使用:

ORDER BY 
        CASE sort_direction
          WHEN 'ASC' THEN
            CASE sort_column
              WHEN 'PRIMARY_KEY' THEN
                primary_key
              ELSE key
            END
        END    
        ASC,
        CASE sort_direction
          WHEN 'DESC' THEN
            CASE sort_column
              WHEN 'PRIMARY_KEY' THEN
                primary_key
              ELSE key
            END
        END    
        DESC

我在坚持服务器端分页方案的每个查询上都使用它。问题是,当我的网格包含相当多的字段时,性能会大幅下降。还有其他方法可以做到这一点吗?我是否需要简单地设置更少的字段来允许分页?

I currently have a server side paging query as such:

SELECT * FROM (SELECT a.*, rownum rnum FROM (
        ( /* Insert Query Here */ ) ) a
      WHERE rownum <= ((page_number + 1) * page_size))
      WHERE rnum >= (((page_number  + 1) * page_size)) - (page_size - 1);

The problem, however, is trying to determine what the user is sorting on as this is tied to a gridview. Currently, I'm using:

ORDER BY 
        CASE sort_direction
          WHEN 'ASC' THEN
            CASE sort_column
              WHEN 'PRIMARY_KEY' THEN
                primary_key
              ELSE key
            END
        END    
        ASC,
        CASE sort_direction
          WHEN 'DESC' THEN
            CASE sort_column
              WHEN 'PRIMARY_KEY' THEN
                primary_key
              ELSE key
            END
        END    
        DESC

I'm using this on every query I stick into the server side paging scheme. The problem is that when I have a grid that has quite a few fields, the performance degrades substantially. Is there any other way to do this? Do I need to simply set less fields to allow paging?

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

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

发布评论

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

评论(3

是伱的 2024-10-18 04:48:55

使用动态 SQL 并在运行时构建 ORDER BY。 Oracle 的 SQL 引擎将看到一个简单的 ORDER BY。

Use Dynamic SQL and build the ORDER BY at runtime. Oracle's SQL engine will see a simple ORDER BY.

悸初 2024-10-18 04:48:55

我怀疑优化器无法弄清楚这一点,因此无法使用索引。检查“解释计划”的内容。

明显的解决方案是让您的应用程序“评估”这种情况,并发送一个更简单的查询。我想您会发现 ORDER BY Primary_key ASC 速度要快得多。

I suspect the optimizer can't figure this out, and thus can't use indices. Check what EXPLAIN PLAN says.

Obvious solution is to have your app "evaluate" that case, and send a much simpler query. I think you'll find ORDER BY primary_key ASC to be much faster.

一向肩并 2024-10-18 04:48:55

如果primary_key和key都是数字,那么降序查询与升序乘以-1相同。


order by direction * CASE sort_column
                       WHEN 'PRIMARY_KEY' THEN primary_key
                       ELSE key              END

上式中,direction 为整数。传入 1 或 -1。这是一个更简单的表达式,并且运行速度应该更快。

If the primary_key and key are numbers then a descending query is the same as an ascending multiplied by -1.


order by direction * CASE sort_column
                       WHEN 'PRIMARY_KEY' THEN primary_key
                       ELSE key              END

In the above, direction is an integer. Pass in 1 or -1. This is a much simpler expression and should run faster.

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