0

I've large data which could be filtered on type column. I can get rows data using subquery with self-join and transform to the column, but due to large data, it would take a lot of time using a subquery. My table structure is like:

Table A

Id | ItemName | Type
 1 | Item1    | A  
 2 | Item2    | A  
 3 | Item3    | B  
 4 | Item4    | B  

Table B

Id | ItemDescTag | Ext
 1 | ItemTag     |  A  
 4 | ItemTag     |  B  

Query

Select ItemName,PurchaseOrdr.Quantity, PurchaseOrdr.PORate, PurchaseOrdr.CostAmount 
from TableA its
left join ( 
select it.Id, it.Quantity,(it.CostAmount/it.Quantity)as PORate, it.CostAmount from TableA it 
inner join Table B m on it.Type = convert(int ,m.Ext)
)as PurchaseOrdr on PurchaseOrdr.Id = its.Id  

I need an alternate way to get rid of this SUB-QUERY because I have to make 10 SUB-QUERY of a similar type to get the required data and due to millions of records it slows down ...

3

1 Answer 1

1

Depending on how similar the other 10 sub-queries are you could query the columns needed ahead of time in CTE like

;WITH PurchaseOrdr_CTE AS(
  SELECT
    it.Id,
    it.Quantity,
    it.CostAmount
  FROM TableA it
  JOIN TableB m
    ON it.Type = convert(int ,m.Ext)
)
SELECT
  ItemName,
  PurchaseOrdr.Quantity
, (PurchaseOrdr.CostAmount/PurchaseOrdr.Quantity) AS PORate, 
  PurchaseOrdr.CostAmount  
FROM TableA its
LEFT JOIN PurchaseOrdr_CTE PurchaseOrdr
  ON its.Id = PurchaseOrdr.Id                 

In this particular example I don't see the need to reference TableA twice at all. But the actual query you are using might have additional columns, logic not included in the post.

The it.Type = convert(int ,m.Ext) conversion will have some overhead. If it's possible to use consistent data types that's obviously better.

If you are dealing with millions of records and this will be an important query, a good covering index will have a large impact.

6
  • yes i've additional columns containing similar (ItemName, PurchaseOrdr.Quantity , (PurchaseOrdr.CostAmount/PurchaseOrdr.Quantity) AS PORate, PurchaseOrdr.CostAmount ) selection Commented Jan 1, 2020 at 6:47
  • what is changing in the set of 3 columns "Opening Balance", "Net Purchase", etc, etc . Only the Type? I mean, is your actual subqueries something like ON it.Type = convert(int ,m.Ext) WHERE it.Type = '2' -- Opening Balance etc Commented Jan 1, 2020 at 6:51
  • I need this highlighted col multiple times... pasteboard.co/INUHDvy.png in out put based on type... Commented Jan 1, 2020 at 7:01
  • What is the "ItemName" as listed in your query above? PhysicalVoucher or something? If you need your query result rotated as columns. Really your options are to do what you are already doing with subqueries or CTE, although CTE won't help much in this key, filtering by Extra1 in each set. Or a PIVOT might be the best option. Especially if you already have a small defined list of "Extra1" types. Won't need to get into dynamic pivot or anything like that. Commented Jan 1, 2020 at 7:16
  • Extra1 are limited but type col in Table A has more than 3 million records... Commented Jan 1, 2020 at 7:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.