ROW_NUMBER

news2024/11/26 21:34:38

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.coloradmin.cn/o/2192916.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Linux高阶——0928—Github数据上传markdown语言

1、Github三要素 仓库、提交、分支 提交 绿色&#xff1a;新加 红色&#xff1a;删除 主分支master或main 项目的存储单位——仓库 实际代码的存储单位——分支 分支的协同开发 2、本地数据上传到云端的过程 3、markdown

OpenHarmony(鸿蒙南向开发)——轻量和小型系统三方库移植指南(一)

往期知识点记录&#xff1a; 鸿蒙&#xff08;HarmonyOS&#xff09;应用层开发&#xff08;北向&#xff09;知识点汇总 鸿蒙&#xff08;OpenHarmony&#xff09;南向开发保姆级知识点汇总~ 持续更新中…… 概述 本文为OpenHarmony开发者提供一些组织编译形式比较常见&…

【C/C++】错题记录(四)

题目一 一个函数可以有很多个返回值&#xff08;有很多个return语句&#xff09;&#xff0c;但是最终只能有一个return语句执行。 题目二 题目三 题目四 题目五 程序数据结构算法 题目六 题目七 题目八 题目九 D选项是语句……

leetcode第189题:轮转数组(C语言版)

思路1&#xff08;不推荐&#xff09; 保存数组最后一个元素&#xff0c;然后数组全体元素后移一位&#xff0c;把保存的最后一个元素存放到数组的第一个位置&#xff0c;重复这一操作&#xff0c;直到执行完了k次。 时间复杂度&#xff1a;需要用k次循环&#xff0c;里面套一层…

【2024】前端学习笔记14-JavaScript常用数据类型-变量常量

学习笔记 1.JavaScript常用数据类型1.1.Number&#xff08;数字&#xff09;1.2.String&#xff08;字符串&#xff09;1.3.Boolean&#xff08;布尔值&#xff09;1.4.Null&#xff08;空值&#xff09;1.5.Undefined&#xff08;未定义&#xff09;1.6.Object&#xff08;对象…

从WIFI到NB-IoT,探秘智能门锁的高科技接入方式

我是小米,一个喜欢分享技术的29岁程序员。如果你喜欢我的文章,欢迎关注我的微信公众号“软件求生”,获取更多技术干货! Hello大家好!我是小米,一个29岁、活力满满、热爱分享技术的小米!今天,我想和大家聊聊一个与智能家居密切相关的技术话题——智能门锁的接入方式。无…

标准正态分布的数据 tensorflow 实现正态分布图,python 编程,数据分析和人工智能

import tensorflow as tf import matplotlib.pyplot as plt # 设置随机种子以获得可重复的结果 tf.random.set_seed(42) # 生成正态分布的数据 # mean0 和 stddev1 表示生成标准正态分布的数据 # shape(1000,) 表示生成1000个数据点 data tf.random.normal(mean0, stddev1, …

DOS 命令学习笔记

一、DOS 简介 DOS 是 Disk Operating System 的缩写&#xff0c;即磁盘操作系统。它是一种早期的计算机操作系统&#xff0c;用于管理计算机的硬件资源和软件资源&#xff0c;提供用户与计算机交互的界面。 二、基本 DOS 命令 &#xff08;一&#xff09;Dir 命令 功能&…

【汇编语言】寄存器(CPU工作原理)(二)—— 汇编指令的基础操作

文章目录 前言正文——&#xff08;一气呵成解决本文内容&#xff09;结语 前言 &#x1f4cc; 汇编语言是很多相关课程&#xff08;如数据结构、操作系统、微机原理&#xff09;的重要基础。但仅仅从课程的角度出发就太片面了&#xff0c;其实学习汇编语言可以深入理解计算机底…

YoloV8改进策略:BackBone改进|CAFormer在YoloV8中的创新应用,显著提升目标检测性能

摘要 在目标检测领域,模型性能的提升一直是研究者和开发者们关注的重点。近期,我们尝试将CAFormer模块引入YoloV8模型中,以替换其原有的主干网络,这一创新性的改进带来了显著的性能提升。 CAFormer,作为MetaFormer框架下的一个变体,结合了深度可分离卷积和普通自注意力…

【简码短链】使用Selenium实现UI自动化测试

1.环境准备 Chrome浏览器 版本为版本 129.0.6668.90&#xff08;正式版本&#xff09; &#xff08;64 位&#xff09; 129版本的Chrome浏览器的驱动,将webdriver放到jdk所在的bin目录下 在命令行中输入:chromedriver验证是否成功 打开IDEA,创建Maven项目,在pom.xml导入所需…

Gridview配置数据源--信任服务器证书

目录 背景过程Gridview配置数据源GridView与数据源&#xff1a;数据库连接与安全&#xff1a;信任服务器证书&#xff1a;配置信任服务器证书&#xff1a;注意事项&#xff1a; 生成连接字符串程序运行报错问题解决 总结 背景 Gridview配置数据源之后&#xff0c;程序报错 过…

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

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

[Linux] Linux 初识进程地址空间 (进程地址空间第一弹)

标题&#xff1a;[Linux] Linux初识进程地址空间 个人主页水墨不写bug &#xff08;图片来源于AI&#xff09; 目录 一、什么是进程地址空间 二、为什么父子进程相同地址的变量的值不同 三、初识虚拟地址、页表 一、什么是进程地址空间 其实&#xff0c;在很久之前&#xf…

数据结构之树(3)

一、森林和树的转换 重要&#xff01; 树->二叉树 由于孩子兄弟链式存储和二叉树链式存储本质相同&#xff0c;故树可转换为二叉树。 森林->二叉树 森林&#xff1a;m棵互不相交的树的集合 森林->树 树->二叉树 森林中各个树的根节点之间视为兄弟关系 二、树…

ORB-SLAM复现时遇到的问题(复现失败,切莫当做教程)

背景 本人的环境&#xff1a;使用ubuntu20.04&#xff0c;opencv4 问题 进行Build DBoW2. Go into Thirdparty/DBoW2/ and execute:时&#xff0c;运行make时出错 我安装的opencv4&#xff0c;在 OpenCV 3 和更高版本中&#xff0c;头文件的路径可能已更改。例如&#xff0…

科普篇--- 什么是硬件在环测试?

我是穿拖鞋的汉子&#xff0c;魔都中坚持长期主义的汽车电子工程师。 老规矩&#xff0c;分享一段喜欢的文字&#xff0c;避免自己成为高知识低文化的工程师&#xff1a; 屏蔽力是信息过载时代一个人的特殊竞争力&#xff0c;任何消耗你的人和事&#xff0c;多看一眼都是你的不…

线程安全的单例模式 | 可重入 | 线程安全 |死锁(理论)

&#x1f308;个人主页&#xff1a; 南桥几晴秋 &#x1f308;C专栏&#xff1a; 南桥谈C &#x1f308;C语言专栏&#xff1a; C语言学习系列 &#x1f308;Linux学习专栏&#xff1a; 南桥谈Linux &#x1f308;数据结构学习专栏&#xff1a; 数据结构杂谈 &#x1f308;数据…

业务封装与映射 -- OTUk/ODUk/OPUk比特速率和容量

介绍OTUk&#xff0c;ODUk&#xff0c;OPUk&#xff0c;OTUCn&#xff0c;ODUCn&#xff0c;OPUCn的比特速率和容量。 OTN支持超100 Gbit/s&#xff0c;100 Gbit/s&#xff0c;40 Gbit/s&#xff0c;10 Gbit/s&#xff0c;2.5 Gbit/s&#xff0c;1.25 Gbit/s等多种线路速率。 …