[英]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.