简体   繁体   English

比较行的值…逐行?

[英]Comparing values of rows…row by row?

I have a small query that joins together some temp tables like such 我有一个小查询,将诸如此类的一些临时表连接在一起

select u.batch_uid, u.user_id, u.firstname, u.middlename, u.lastname, u.email, u.student_id, u.row_status, uff.batch_uid, uff.user_id, uff.firstname,uff.middlename,uff.lastname,uff.email, uff.student_id,uff.row_status
from users u full outer join users_feed_file uff on u.user_id = uff.user_id
where u.data_src_pk1 = 83

The results would for example be something like this: 结果将是这样的:

(users as u) batch_uid user_name row_status (users_feed_file as uff) batch_uid user_name row_status
            johndoe   johndoe            2                           johndoe   johndoe            0

Because, the first 3 columns come from a source table that is being replicated from a live table. 因为,前3列来自正在从活动表复制的源表。 The last 3 columns come from a feed file that gets processed and inserted into a temp table and are then dropped after the run time is completed(and re-loaded later with new data). 最后3列来自供稿文件,该文件经过处理并插入到临时表中,然后在运行时间完成后被删除(并在以后重新加载新数据)。

What I'm trying to accomplish is basically looking at rows to perform various operations. 我要完成的工作基本上是查看行以执行各种操作。 I'm going to be checking nearly 25,000 rows. 我将要检查近25,000行。 So in this case, what I'd like to do is check something like 所以在这种情况下,我想做的是检查类似

if u.batch_uid, u.user_name, u.row_status is not null
and
uff.uid, uff.user_name, uff.row_status is not null
and u.row_status is equal to 2 and uff.row_status is equal to 0
add user to feed file to enable him

However these(and other kinds of conditions and checks)need to be done against all 25k rows that get returned and then processed in C# row by row to determine if my code needs to insert a line into a file or not. 但是,这些(以及其他类型的条件和检查)需要针对所有返回的25k行进行,然后在C#中逐行处理以确定我的代码是否需要在文件中插入一行。

Thank you. 谢谢。

You have a couple of different issues to address in your question. 您有几个不同的问题要解决。

First, in your initial SELECT you're using a FULL OUTER JOIN--but you're explicitly looking for records where the three fields in (table) User match the three fields in (table) UserFeed. 首先,在您的初始SELECT中,您使用的是FULL OUTER JOIN,但是您要显式地查找(表)User中的三个字段与(表)UserFeed中的三个字段匹配的记录。 You will see dramatically better performance--and process a lot fewer records--with an INNER JOIN, like this: 使用INNER JOIN,您将看到显着更好的性能-处理更少的记录,如下所示:

SELECT u.batch_uid, u.user_id, u.firstname, 
u.middlename, u.lastname, u.email, u.student_id, 
u.row_status, uff.batch_uid, uff.user_id, uff.firstname, 
uff.middlename, uff.lastname, uff.email, uff.student_id, 
uff.row_status
FROM users u 
INNER JOIN users_feed_file uff 
ON u.user_id = uff.user_id
WHERE u.data_src_pk1 = 83
AND u.row_status = 2
AND uff.row_status = 0;

That will give you just the rows that match your complete condition--it should be a relatively small set of rows. 那只会给您符合您的完整条件的行-应该是相对较小的一组行。

But--if you're just retrieving records from (table) User to compare with (table) UserFeedFile, why get the user's name, address, etc.? 但是-如果您只是从(表)用户检索记录以与(表)UserFeedFile进行比较,为什么要获得用户的姓名,地址等? No need--just get the data you want: 无需-只需获取所需的数据即可:

SELECT u.user_id
FROM user U
INNER JOIN userfeedfile UFF
ON U.user_id = UFF.user_id
WHERE U.row_status = 2
AND UFF.row_status = 0
AND U.data_src_pkt1 = @PacketNumber;   -- That's a parameter

The next question is: what are you going to do with those rows? 下一个问题是:您将如何处理这些行? If you're going to update a field value in another table (or, perhaps, in the User Feed File table) you can do it with an INSERT or UPDATE statement. 如果要更新另一个表(或者可能是User Feed File表)中的字段值,则可以使用INSERT或UPDATE语句来完成。 To update (table) UserFeedFile, do this: 要更新(表)UserFeedFile,请执行以下操作:

UPDATE userfeedfile
SET enabled = 1
FROM user U
INNER JOIN userfeedfile UFF
ON U.user_id = UFF.user_id
WHERE U.row_status = 2
AND UFF.row_status = 0
AND U.data_src_pkt1 = @PacketNumber;

(You can change the second line, with the SET statement, to update any field or fields you choose.) (您可以使用SET语句更改第二行,以更新您选择的任何一个或多个字段。)

As a general rule, SQL databases work best on sets of data. 通常,SQL数据库在数据集上的工作效果最佳。 If you find yourself iterating over a data set a row at a time, and ESPECIALLY if you are going to surface the data to a different process (potentially across the network on a difference machine) to handle the row in .Net code, stop and think of how to do it within SQL Server, using sets. 如果您发现自己一次遍历一个数据集,并且特别是要将数据显示到另一个进程(可能在另一台机器上通过网络)来处理.Net代码中的行,则停止并想一想如何在SQL Server中使用集合做到这一点。 The performance difference will be dramatic. 性能差异将是巨大的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM