Background. I'm using SQL Server. I have two tables in database:
Vendors(Id, Name, Description)
Products(Id, VendorId, Name, Description)
Values in Id
column are formatted with prefix 'ID_'
in Vendor
table.
Values in VendorId
column are formatted with prefix 'VE_'
in Products
table.
E.g. 'VE_001245'
in Products
refers to 'ID_001245'
in Vendors
.
(Please, do not propose to change this concept, do not care about database scheme, do not suggest adding foreign key. All it is just for illustration.)
Question: which one of following queries is best in performance context and why?
Use
replace
function in innerselect
:select v.* from Vendors v inner join ( select distinct replace(VendorId, 'VE_', 'ID_') as Id from Products ) list on v.Id = list.Id
Use
replace
function inon
-statement:select v.* from Vendors v inner join ( select distinct VendorId as Id from Products ) list on v.Id = replace(list.Id, 'VE_', 'ID_')
Edit. There is only clustered index in each table (by Id
column). Each table can contains millions rows.