简体   繁体   English

每个参数的TVP与循环

[英]TVP vs loop for each parameter

I am in a situation where I need to do some comparison for some products that are in a shopping cart. 我处于一种情况,需要对购物车中的某些产品进行一些比较。 The cart can have as many items as the user desires but the average over time is roughly 5-15 items per cart. 该购物车可以具有用户期望的数量,但随着时间的推移,平均每个购物车的平均物品数量约为5-15。

My question is one about optimization/overhead. 我的问题是有关优化/开销的问题。 I need to check what department each product is in and determine the shipping charge. 我需要检查每种产品所属的部门并确定运费。 The department for each product isn't directly related to the product (ie there isn't a department id column in the product definition table) but I can query and get the department id based on the product id. 每个产品的部门都不与产品直接相关(即,产品定义表中没有部门ID列),但是我可以根据产品ID查询并获取部门ID。

This is where my question is. 这是我的问题所在。 Would it be better (less overhead/faster) to pass in a list of id's to a stored procedure and use the list in the stored procedure with something like table valued parameter to query the existing table? 将ID的列表传递给存储过程并在存储过程中将该列表与诸如表值参数之类的东西一起用于查询现有表会更好(更少的开销/更快)吗? Or would it be faster to just query the products 1 at a time (assuming the aver cart size is on the high end of 5-15), get the department id, and then go onto the next item? 还是一次查询产品1(假设平均购物车大小在5-15的高端),获取部门ID,然后转到下一个项目,会更快吗?

I have never used TVP and I am trying to learn from some other questions posted on stack overflow and the msdn site. 我从未使用过TVP,并且尝试从堆栈溢出和msdn站点上发布的其他一些问题中学习。 So before I spend to much time trying to get the results I want I am posing this question. 因此,在我花大量时间尝试获得结果之前,我想提出一个问题。

Generally the TVP should be faster (and easer to make it faster if you need it). 通常,TVP应该更快(如果需要,可以更轻松地使其更快)。 The larger the list, the more favorable TVP's should be versus one query per ID. 列表越大,对应每个ID一次查询的TVP就越有利。

It's a bit more development work to set things up to use a TVP, so if you'll never have more than a small list (say 3 or less), then it may not be worth it, but otherwise it's definitely a better approach. 要使用TVP进行设置需要花费更多的开发工作,因此,如果您的清单永远很少(例如3个或更少),那可能就不值得了,但是绝对是一个更好的方法。

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

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