是否可以在sql server中避免光标(或while循环)100%? [英] is it possible to avoid cursor (or while loop) 100% in sql server ?

查看:78
本文介绍了是否可以在sql server中避免光标(或while循环)100%?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我在YouTube上看到一个视频,其中一位SQL开发人员说我从未使用过光标。当开发人员没有其他想法时,他们会使用光标。

现在这是很奇怪。如何在不使用游标和while循环的情况下进行迭代。是连接可以是一个选项。但有可能我们可以100%使用光标。



我写了一个存储过程,我在使用光标并获取所有记录而我我正在为所有记录做计算类型的事情然后最后一行我显示平均值(特定列),总和(特定列)。



例如,我截至目前我只显示3列。现在我使用光标来获取每个集线器(A,B ..)

然后计算。



- >计算全部请求数

我计算总评价

我计算总分数

totalrequest = totalpick + totaldrop

(使用的连接数很多)



- > avgrequest(使用多个表中的连接)

- >占用(使用多个表中的连接)

==

然后光标结束

- >计算总要求的总数

- >计算avgrequest的平均值

- >计算入住率平均值

 hub totalrequest avgrequest occupancy 

A 34 20 2.4

B 16 20 1.6

C 20 20 1.0

D 10 10 2.5

E 20 30 2.5

FINAL 100 20 2.5





是否可以在基于集合的操作中处理它。这个SP运行速度很慢,实时我正在做一个月的数据。



请你干预..请帮忙。

解决方案

几乎总是可以不使用游标。可以帮助的一件事是使用CTE(公用表表达式)将任务的元素转换为离散语句。您可以在SQL中内联进行大多数计算。您可以使用分组来计算组内的总计和平均值。有各种方法可以避免RBAR(通过令人痛苦的行划分)查询,以及执行基于集合的操作,这就是SQL的用途。


过去,我也回答过类似的问题,查看替代品

光标替代方案 [ ^ ]

Recently I saw a video in YouTube where one of the SQL developers was saying that "I never used a cursor. When developers have no other idea then they use it cursor".
Now this is quite strange. How will one iterate without using cursor and while loop. Yes joins can be an option. But is it possible we can get ride of cursor 100%.

I have written a stored procedure, where I am using a cursor and it fetches all the records and I am doing calculation type of things for all the records and then final row I am showing the avg(particular column), sum(particular column).

for eg, I am showing only 3 columns as of now. Now I m using cursor to fetch each hub(A,B..)
and then calculate.

-->to calcualte totalrequest
I calculate totalpick
I calculate totaldrop
totalrequest=totalpick + totaldrop
(lot of joins used)

-->avgrequest(using joins from multiple tables)
-->occupancy(using joins from multiple tables)
==
then after the cursor ends
-->calculate totalsum of totalrequest
-->calculate average of avgrequest
-->calculate occupancy average

hub   totalrequest  avgrequest  occupancy  

A         34          20         2.4

B         16          20         1.6

C         20          20         1.0

D         10          10         2.5

E         20          30         2.5

FINAL     100         20         2.5



Is it possible to handle it in set based operation. This SP runs slow as in realtime I am doing it for one month data.

Kindly need your intervention..please help.

解决方案

It is almost always possible to not use cursors. One thing that can help is to use CTEs ( common table expressions ) to turn elements of your task in to discrete statements. You can do most calculations inline in your SQL. You can use group by to calculate totals and averages within groups. There are all sorts of ways to avoid RBAR (row by agonising row ) queries, and to do set based operations, which is what SQL is for.


In past, I have answered similar question, check it out alternatives
Cursor alternatives[^]


这篇关于是否可以在sql server中避免光标(或while循环)100%?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆