Dears, I'm calling who can defeat any SQL query performance issue
I'm in the process of generating an Inventory Report that handles locations for items in stock and I want to show for the user a flag say "Not available any more" that mean the location is not available because it's taken from another item.
That is mainly done by this part of the code. Using below condition:
IIF(ItemLocationsCount > 1 and ItemsInStock = 0 and StoreId <> MainStoreId, 'Not available any more', Store)
this condition is showing the flag only for items that has more than one location and with 0 Quantity
This aggregate is counting number of loations taken by an item:
ItemsInStockByCurrentItemLocation as (
select *
,( select
count(*)
from
ItemsInStock ItemsInStockLocationsCount
where
ItemsInStock.StoreId = ItemsInStockLocationsCount.StoreId
group by MainStoreId) ItemLocationsCount
from
ItemsInStock
)
This is the view that is showing the whole data with the required flag (This run too slowly 30 minutes):
,FilteredUnitifiedConsederingLocation as (
select (select
IIF(ItemLocationsCount > 1 and ItemsInStock = 0 and StoreId <> MainStoreId, 'Not available any more', Store)
from
ItemsInStockByCurrentItemLocation ItemsInStockByCurrentItemLocationAvalability
where
ItemsInStockByCurrentItemLocationAvalability.StoreId = ItemsInStockByCurrentItemLocation.StoreId and
ItemsInStockByCurrentItemLocationAvalability.ItemId = ItemsInStockByCurrentItemLocation.ItemId) StoreAvalability,
ItemsInStockByCurrentItemLocation.*,
MainAndWorkFlowUnitified.Date_P,
MainAndWorkFlowUnitified.Date
from ItemsInStockByCurrentItemLocation
inner join MainAndWorkFlowUnitified
on MainAndWorkFlowUnitified.StoreId = ItemsInStockByCurrentItemLocation.StoreId and
MainAndWorkFlowUnitified.ItemId = ItemsInStockByCurrentItemLocation.ItemId
where
--(ItemLocationsCount = 1 or
--(ItemLocationsCount > 1 and ItemsInStock <> 0)) and
(ItemsInStockByCurrentItemLocation.StorePath Like '%' + @StorePath + '%'
or (ItemsInStockByCurrentItemLocation.StorePath is null and MSId is not null))
and ItemsInStockByCurrentItemLocation.ItemPath Like @ItemPath + '%'
and (ItemsInStockByCurrentItemLocation.Description is null or ItemsInStockByCurrentItemLocation.Description like '%' + @Description + '%' )
and (Date_P is null or (Date >= @FromDate and Date <= @ToDate))
)
This is the view that is showing the whole data without the section that generate the required flag (This run too fast 1 second):
,FilteredUnitified as (
select ItemsInStock.*,
MainAndWorkFlowUnitified.Date_P,
MainAndWorkFlowUnitified.Date
from ItemsInStock
inner join MainAndWorkFlowUnitified
on MainAndWorkFlowUnitified.StoreId = ItemsInStock.StoreId and
MainAndWorkFlowUnitified.ItemId = ItemsInStock.ItemId
where
(ItemsInStock.StorePath Like '%' + @StorePath + '%'
or (ItemsInStock.StorePath is null and MSId is not null))
and ItemsInStock.ItemPath Like @ItemPath + '%'
and (ItemsInStock.Description is null or ItemsInStock.Description like '%' + @Description + '%' )
and (Date_P is null or (Date >= @FromDate and Date <= @ToDate))
)
This is the view that change between both on a parameter value:
ItemsInStocks as (
select
distinct
ItemId,
ParentGroup,
Unit,
Item, ItemPath,
StoreAvalability Store,
ItemLocationsCount,
MainStorePath,
--Store,
StorePath,
ItemsInStock,
Description
,Code,
Name,
NameEn
from FilteredUnitifiedConsederingLocation where @ShowItemsWithUnavailableLocations = 1
union all
select
distinct
ItemId,
ParentGroup,
Unit,
Item, ItemPath,
Store,
null ItemLocationsCount,
MainStorePath,
StorePath,
ItemsInStock,
Description
,Code,
Name,
NameEn
from FilteredUnitified where @ShowItemsWithUnavailableLocations = 0
I have tried to not showing the row instead of putting a flag. (You can see the related code commented)
--(ItemLocationsCount = 1 or
--(ItemLocationsCount > 1 and ItemsInStock <> 0))
But same is happing. the view is still running too slowly
I have refered to the related questions in the knowledge base, and find one suggesting to use indexes: I figure that all required columns is indexed like ItemId, StoreId, and MainStoreId because they are foreign keys. Kindly, if there is any additional column should be indexed to optimize performance let me know.