ROW_NUMBER

How to rewrite a query which uses the ROW_NUMBER() window function in versions 5.7 or earlier before window functions were supported

e.g.,

SELECT ROW_NUMBER() OVER (PARTITION BY fieldA) AS rownum, myTable.* FROM myTable;

index 用不上的

Solution

Assuming the table has a unique or primary key field named 'id', this query produces equivalent results:

SELECT COUNT(t.id) + 1 AS rownum, myTable.*
FROM myTable
LEFT JOIN myTable AS t ON
  myTable.fieldA = t.fieldA
  AND myTable.id > t.id
GROUP BY
myTable.id
;

---------Oracle 不适合

-----order by 1 不同于 select 中的order by

Applies to:

Oracle Database - Enterprise Edition - Version 19.1.0.0.0 and later
Information in this document applies to any platform.

Symptoms

A query with row_number() over (order by 1) runs very slow with a bad plan.


The optimizer mode for the session is "all_rows." 10053 trace shows selectivity which is "not sane," for the switched optimizer mode "First K Rows". 

SINGLE TABLE ACCESS PATH (First K Rows)
  Single Table Cardinality Estimation for T[T]
  SPD: Return code in qosdDSDirSetup: NOCTX, estType = TABLE

 kkecdn: Single Table Predicate:"T"."DT">=SYSDATE@!
  The computed sel: -1.5072e+00 is not sane.                                                                             <<<--------
  Using density: 0.010000 of col #1 as selectivity of pred does not have a sane value. The wrong sel was: -1.5072e+00
  Table: T  Alias: T
    Card: Original: 101.000000  Rounded: 1  Computed: 1.010000  Non Adjusted: 1.010000

Changes

Cause

Incorrect syntax.
 
ROW_NUMBER () OVER (ORDER BY 1) is the same as ROW_NUMBER () OVER (ORDER BY NULL)

ORDER BY <NUMBER> in a window function is not the same as in a regular ORDER BY clause where the constant would mean a column number for ordering.


 

Solution

Replace the number with the correct column name.

row_number() over ( order by <NUMBER>)  ===>  row_number() over ( order by <COLUMN_NAME>)

---------ORDER BY b, c, d  要是一个unique key才能保证结果每次一样

Symptoms

When SQL statements use analytic functions ROW_NUMBER, FIRST_VALUE or LAST_VALUE it is sometimes possible that inconsistent results are produced.
The same SQL executed repeatedly on the same unchanging table data can produce different results.
This can be mistaken for an intermittent wrong results bug when in fact it could be a SQL coding issue.

Changes

This type of problem can occur when coding new SQL statements using the above analytic functions.

Cause

Consider a table T having columns A,B,C,D used in a query like the following:

SELECT a, b, c, d ROW_NUMBER() OVER (PARTITION BY a ORDER BY b) AS rn
FROM t;

The analytic clause (the OVER clause) uses the columns A and B of this table: the rows in T are grouped in partitions with the same value for column A and within each such group they are ordered by column B.
After this grouping and ordering is done, the analytic function assigns a row number (aliased as RN) starting from 1 and increasing to each row within each group. The columns A, B and RN are then returned together with the remaining columns C and D as the result.
It can sometimes occur that the result of the query is inconsistent across executions i.e. the value for RN can be assigned differently to rows with particular values for A, B, C and D.

Here is an example of why this can happen: the table T has more columns than the ones which appear in the OVER clause, i.e. columns C and D. So it is possible that rows exist with the same values for A and B which have different values for C and D. Such rows can be considered duplicates as far as the OVER clause is concerned. The ROW_NUMBER function will assign row numbers (RN) to all of them but could do so differently from execution to execution as there is no condition in the OVER clause which enforces a particular assignment. The only ordering is on column B therefore the rows with various values for C and D could be assigned row numbers in no particular way. (This is similar in concept to the lack of ordering when rows are fetched in a query but no ORDER BY clause has been speficied: rows can be returned in any order whatsoever.) 

Solution

To solve this problem, the SQL needs to be coded so that a specific ordering is enforced in the OVER clause.

In this example, the OVER clause was written as follows:

SELECT a, b, c, d ROW_NUMBER() OVER (PARTITION BY a ORDER BY b, c, d) AS rn
FROM t;

i.e. the remaining columns C and D are included in the ORDER BY of the OVER clause.

In fact it is not necessary to include all the columns of the table in the ORDER BY clause. It is sufficient to include the columns of a unique (or primary) key as there can be no duplicate rows in this case.

The example in this article used ROW_NUMBER as the analytic function, however the issue also applies to FIRST_VALUE and LAST_VALUE. The difference is that instead of a row number being assigned to "duplicate" rows non-deterministically, a different row may be chosen as first or last value from those "duplicates".

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.rhkb.cn/news/439955.html

如若内容造成侵权/违法违规/事实不符,请联系长河编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

保姆级手把手使用YOLOv11训练自己数据集(含源代码、网络结构、模型检测和转换 、数据集查找、模型训练)

文章目录 前言项目地址项目内容&#xff1a;网络模型结构性能测试任务描述任务内容 项目运行模型训练 前言 本教程内含YOLOv11网络结构图训练教程推理教程数据集获取等有关的内容~ 项目地址 YOLO11是Ultralytics YOLO系列实时目标检测器的最新迭代版本&#xff0c;它以尖端的…

打卡第三天 P5729 【深基5.例7】工艺品制作

今天是我打卡第三天&#xff0c;做个入门题吧(#^.^#) 原题链接&#xff1a;【深基5.例7】工艺品制作 - 洛谷 题目描述 输入格式 输出格式 输出一个整数表示答案。 输入输出样例 输入 #1 4 4 4 1 1 1 1 2 2 2 输出 #1 56 说明/提示 C&#xff1a; #include<bits/std…

使用 classification_report 评估 scikit-learn 中的分类模型

介绍 在机器学习领域&#xff0c;评估分类模型的性能至关重要。scikit-learn 是一个功能强大的 Python 机器学习工具&#xff0c;提供了多种模型评估工具。其中最有用的函数之一是 classification_report&#xff0c;它可以全面概述分类模型的关键指标。在这篇文章中&#xff…

低组装滚珠导轨:承载力强,适应多样工况!

在自动化行业中&#xff0c;高质量、高效率的生产线是确保产品品质和生产效率的关键。而低组装型滚珠导轨作为生产线中的重要组件之一&#xff0c;能够提供精准的直线运动控制&#xff0c;为自动化设备的稳定运行和高精度检测提供可靠支持。 相对于传统的导轨系统来说&#xff…

汇编语言笔记2

7.MASM,NASM,ATT,ARM的介绍 MASM:Windows下编译汇编指令的软件,可以在DOSBox下运行 NASM:优化版的MASM,主要用于Linux操作系统 ATT:Linux默认的汇编风格(但不友好) ARM:非PC(IOT设备)的汇编,比如写51单片机打开keil4的界面可以看到ARM 8.汇编 C语言 C 之间的关系 发展历程…

Arduino UNO R3自学笔记21 之 Arduino电机的闭环控制

注意&#xff1a;学习和写作过程中&#xff0c;部分资料搜集于互联网&#xff0c;如有侵权请联系删除。 前言&#xff1a;上篇写了电机速度测定&#xff0c;这篇主要是讲测定出的速度用于反馈&#xff0c;使得实际速度快速响应到需要的速度。 1.控制系统介绍 分2大类&#x…

《深度学习》【项目】OpenCV 发票识别 透视变换、轮廓检测解析及案例解析

目录 一、透视变换 1、什么是透视变换 2、操作步骤 1&#xff09;选择透视变换的源图像和目标图像 2&#xff09;确定透视变换所需的关键点 3&#xff09;计算透视变换的变换矩阵 4&#xff09;对源图像进行透视变换 5&#xff09;对变换后的图像进行插值处理 二、轮廓检测…

idea插件市场安装没反应

https://plugins.jetbrains.com/idea重启后还是不行那就

Docker:安装 MongoDB 的详细指南

请关注微信公众号&#xff1a;拾荒的小海螺 博客地址&#xff1a;http://lsk-ww.cn/ 1、简述 MongoDB 是一个流行的 NoSQL 数据库&#xff0c;可以在 Docker 容器中轻松安装和运行。本文将介绍如何在 Docker 中安装 MongoDB&#xff0c;并展示如何在 Java 应用中使用 MongoDB…

kafka发送消费核心参数与设计原理详解

核心参数详解&#xff1a; 发送端参数&#xff1a; 发送方式&#xff1a;默认值一般都是1: 重试参数 &#xff1a; 批量参数&#xff1a; 消费端参数&#xff1a; 自动提交参数&#xff1a; 如果是false&#xff0c;就是说消费完后不提交位移。也就是说比如之前消费的1-5.…

Qt教程(002):Qt项目创建于框架介绍

二、创建Qt项目 2.1 创建项目 【1、New Project】 【2、选择Qt Widgets Application】 【3、设置项目名称和保存路径】 注意&#xff0c;项目名称和路径不要带中文。 【4、选择QWidget】 带菜单栏的窗口QMainWindow空白窗口QWidget对话框窗口QDialog 【5、编译】 2.2 项目框…

前端编程艺术(4)---JavaScript进阶(vue前置知识)

目录 1.变量和常量 2.模版字符串 3.对象 4.解构赋值 1.数组的解构 2.对象的解构 5.箭头函数 6.数组和对象的方法 7.扩展运算符 8.Web存储 9.Promise 10.AsyncAwait 11.模块化 1.变量和常量 JavaScript 中的变量和常量是用于存储数据的标识符。变量可以被重新赋值&am…

基于阻塞队列及环形队列的生产消费模型

目录 条件变量函数 等待条件满足 阻塞队列 升级版 信号量 POSIX信号量 环形队列 条件变量函数 等待条件满足 int pthread_cond_wait(pthread_cond_t *restrict cond,pthread_mutex_t *restrict mutex); 参数&#xff1a; cond&#xff1a;要在这个条件变量上等待 mutex…

windows下,在vscode中使用cuda进行c++编程

安装cuda CUDA Toolkit Downloads | NVIDIA Developer 这里网上教程多的是&#xff0c;在这个网址下载安装即可 我这台电脑因为重装过&#xff0c;所以省去了安装步骤&#xff0c;但是要重新配置环境变量。我重新找到了重装之前的CUDA位置(关注这个bin文件夹所在的目录) 在…

微信第三方开放平台接入本地消息事件接口报错问题java.security.InvalidKeyException: Illegal key size

先看报错&#xff1a; java.security.InvalidKeyException: Illegal key sizeat javax.crypto.Cipher.checkCryptoPerm(Cipher.java:1039)at javax.crypto.Cipher.implInit(Cipher.java:805)at javax.crypto.Cipher.chooseProvider(Cipher.java:864)at javax.crypto.Cipher.in…

九、3 串口发送+printf函数移植+打印汉字

1、接线图 TX与RX交叉连接&#xff0c;TXD接STM32的PA10&#xff0c;RXD接STM32的PA9 VCC与3.3V用跳线帽连接 2、函数介绍 3、代码部分 &#xff08;1&#xff09;发送字节的函数&#xff08;Byte&#xff09; 可直接发送十六进制数 如0x41&#xff0c;也可直接发送字符 如A …

【重学 MySQL】五十六、位类型

【重学 MySQL】五十六、位类型 定义赋值与使用注意事项应用场景 在MySQL数据库中&#xff0c;位类型&#xff08;BIT类型&#xff09;是一种用于存储位字段值的数据类型。 定义 BIT(n)表示n个位字段值&#xff0c;其中n是一个范围从1到64的整数。这意味着你可以存储从1位到64…

【AIGC】AI时代的数据安全:使用ChatGPT时的自查要点

博客主页&#xff1a; [小ᶻZ࿆] 本文专栏: AIGC | ChatGPT 文章目录 &#x1f4af;前言&#x1f4af;法律法规背景中华人民共和国保守秘密法中华人民共和国网络安全法中华人民共和国个人信息保护法遵守法律法规的重要性 &#x1f4af;ChatGPT的数据使用特点ChatGPT数据安全…

YOLOv11 vs YOLOv8:谁才是真正的AI检测之王?

《博主简介》 小伙伴们好&#xff0c;我是阿旭。专注于人工智能、AIGC、python、计算机视觉相关分享研究。 ✌更多学习资源&#xff0c;可关注公-仲-hao:【阿旭算法与机器学习】&#xff0c;共同学习交流~ &#x1f44d;感谢小伙伴们点赞、关注&#xff01; 《------往期经典推…

Js逆向分析+Python爬虫结合

JS逆向分析Python爬虫结合 特别声明&#x1f4e2;&#xff1a;本教程只用于教学&#xff0c;大家在使用爬虫过程中需要遵守相关法律法规&#xff0c;否则后果自负&#xff01;&#xff01;&#xff01; 完整代码地址Github&#xff1a;https://github.com/ziyifast/ziyifast-co…