How can i rewrite the below query to return results with few IO operation and less response time
select
*,
(select Product_Name from InventoryMaster J2 where J1.ParentItem=J2.ItemId) as ParentName,
(select Description from InventoryMaster J2 where J1.ParentItem=J2.ItemId) as ParentDesc
from
InventoryMaster J1
where
Flag like '%N%'
The InventoryMaster table has a parent Item column where it will store another records ItemId (for parent-child relatioship) for some records
The above like makes it difficult for the database to answer this query efficiently. It has to look at every value of the “Flag” column and check for an “N” within the string.
Could this be changed to just
Flag = 'N'?If the answer is no, then the database is designed incorrectly. You should store one thing in a column, not multiple. Search for “database normalization”.
You should consider re-writing the query as a join between the three tables rather than using the sub-select in the query list as you have.