简体   繁体   English

使用T-SQL用重复字段查询表的有效/有效方式是什么?

[英]What is a productive / efficient way to query a table with repeating fields using T-SQL?

I have a tables (simplified) like this: 我有一个这样的表(简化):

Lender
   Id (PK)
   AgencyCode1
   AgencyCode2
   AgencyCode3...
   AgencyCode20

Agency
   AgencyCode
   AgencyName

The Lender table is denormalized data coming from a view for reporting. 贷方表是来自视图的非规范化数据,用于报告。 I need one row in my output for each Lender and I need to join the agency codes to an agency table to get the display name for each agency. 每个贷方在输出中都需要一行,并且需要将代理商代码加入代理商表中,以获取每个代理商的显示名称。

The resulting output I am looking for in each row is: 我在每一行中寻找的结果输出是:

LenderId, AgencyCode1, AgencyName1, ... AgencyCode20, AgencyName20

What is the pattern that strikes the best balance between performance and (developer) productivity to query something like this? 在这样的情况下,如何在性能和(开发人员)生产率之间达到最佳平衡的模式?

--Edit, Agency Code wasn't a primary key, as I had originally indicated. --Edit,代理商代码不是我最初指出的主键。

Echoing @Martin Smith's comment, unpivot, join, and pivot would probably be the most efficient, performance wise -- and, through working out the intricacies of how to unpivot and pivot, your developers would be more expereienced and thus more productive over time. 回应@Martin Smith的评论,取消枢纽,加入和枢纽可能是最高效,最明智的选择-并且通过弄清如何取消枢纽和枢纽的复杂性,随着时间的推移,您的开发人员将变得更加经验丰富,因此工作效率更高。

Alternatively, if there are 20 and always 20 denormalized columns, you could just write out 20 left outer joins (under the assumption that ever Lender is not related with 20 Agencies). 或者,如果有20个并且总是20个非规范化列,则可以只写出20个左外部联接(假设Lender与20个代理商无关)。 This is ugly code, and would require SQL to process 20 joins... but if the Agency table is small (where I'd call 8 pages/1 extent small), then the overall processing time might be relatively short. 这是丑陋的代码,需要SQL处理20个连接...但是,如果Agency表很小(在这里我将其称为8页/ 1小范围),那么整个处理时间可能会相对较短。 Testing would be called for, to determine which performs best. 需要进行测试,以确定哪个执行效果最好。

As regards to developer productivity overall, keep track of how long everything takes (including posting to SO). 关于开发人员的整体生产力,请跟踪所有操作需要多长时间(包括发布到SO)。 When it's done, tally everything up, and use it to show how much time was wasted by your developers having to work with poorly designed tables. 完成后,汇总所有内容,并使用它来显示开发人员不得不使用设计不良的表浪费了多少时间。

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

相关问题 用分层数据类型查询表的更有效的T-SQL方法是什么 - What is the more efficient T-SQL way to query a table with Hierarchical data type 编写T-SQL语句的这一部分的最有效方法是什么? - What is the most efficient way to code this part of a T-SQL statement? t-sql:学术界,寻求更高效/更好的查询方式 - t-sql: academic, looking for more efficient/better way to query 使用 T-SQL 连接所有父行中的字符串的最有效方法是什么? - What is the most efficient way to concatenate a string from all parent rows using T-SQL? T-SQL查询以识别由单个重复字符/数字组成的varchar字段? - T-SQL Query to identify varchar fields that consist of a single repeating char/digit? 使用T-SQL / MS-SQL将字符串附加到现有表格单元格的最简单方法是什么? - What is the easiest way using T-SQL / MS-SQL to append a string to existing table cells? T-SQL差距和排名查询的最佳方法是什么 - What is the best way for T-SQL Gaps and Ranking query 删除t-sql中所有大表的最佳方法是什么? - What is the best way to delete all of a large table in t-sql? T-SQL:使用“来自变量”查询更新临时表 - T-SQL: Update temp table using “from variable” query 在T-SQL中比较答案字符串和答案分数以对考试评分的最有效方法是什么 - What is the most efficient way in T-SQL to compare answer strings to answer keys for scoring an exam
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM