I have a shipping cost table with two dimensions: Zone
and Weight
. Zone
is an exact value. Weight
is a range where given value is lower inclusive one. That means that in the given example, the highest range is 200-∞.
I am looking for a cost for given Zone
and Weight
.
SELECT TOP 1 *
FROM fg.CostTest
WHERE Zone = 'A' AND Weight <= 123
ORDER BY Weight DESC
Having dozens of thousands rows in a table, is there a better way in terms of performance to achieve that?
I am using SQL Server 2022.
3条答案
按热度按时间huwehgph1#
this might help :
pieyvz9o2#
I suggest making sure your columns zone and weight has index which would definitely help in the performance.
Then if the table is going to be used several times in queries or stored procedures, it’s best to create an indexed view that contains the LowerBoundWeight and Weight, and then use that view in queries as a join.
The View would be similar to the below:
Then on the select, join it as such
4sup72z83#
Create procedure, it will help for repetition query :
Step 1 :
Step 2: Try the store procedure