Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
SQL
CREATE FUNCTION [dbo].[getVesselposition]
 (@VesselPositionId varchar(10))

RETURNS varchar(50)
AS
begin
declare @VesselPosition varchar(50)

select @VesselPosition = VesselPosition
FROM  dbo.PMS_TBL_EUIPMENT_VESSELPOSITION_MASTER  where VesselPositionId =@VesselPositionId

if @VesselPosition is null

 select @VesselPosition=''

return  @VesselPosition
end
Posted
Comments
ZurdoDev 12-Oct-15 10:25am    
What is your question?
[no name] 12-Oct-15 10:28am    
my question is , i want to use the same logic in the sub query or other forms for better performance ,because it is used in a joining of four tables which leads too much of time to execute...
ZurdoDev 12-Oct-15 10:30am    
I only see 1 table. And sub query will not likely be faster than joining.
[no name] 12-Oct-15 10:32am    
i was using the above function in the following query


SELECT ROW_NUMBER()OVER (ORDER BY M.EquipmentId) AS Row,
dbo.getEquipmentName(M.EquipmentId)EquipmentName,
MaintananceId,
dbo.getVesselposition(E.PositionInVessal)PositionInVessal,
ClassCode,
MaintananceName,
dbo.getMaintananceType(MaintanaceType)MaintanaceType,
dbo.getFrequecyType(FrequencyType)FrequencyType,
Frequency,
CONVERT(VARCHAR(10), M.LastDoneDate, 105)LastDoneDate,
LastDoneRunningHours,CONVERT(VARCHAR(10), NextDueDate, 105) NextDueDate,
NextDueRunningHours, dbo.getResposibility(Responsibility)Responsibility,
C.Remarks,
C.JobCompletedAt,
C.NameOfPort,
C.JobCarriedOutBy,
C.NameOfWorkShop,
C.MaintanaceStatus
FROM dbo.PMS_TBL_MAINTANANCE_MASTER M
INNER JOIN PMS_TBL_MAINTANANCE_COMPLETION C ON C.MaintenanceId=M.MaintananceId
LEFT OUTER JOIN PMS_TBL_EQUIPMENT_MASTER E ON M.EquipmentId=convert(varchar(36),E.UniqueId)
LEFT OUTER JOIN PMS_TBL_SUB_EQUIPMENT_MASTER S ON M.EquipmentId=convert(varchar(36),S.UniqueId)

so, please write the exact code which replace the function in the query i listed

ZurdoDev 12-Oct-15 10:44am    
It's relatively easy. Do you understand SQL. Why are you stuck on this?

1 solution

Join to the appropriate tables. E.g. to avoid calling GetVesselPosition

SQL
SELECT ROW_NUMBER()OVER (ORDER BY M.EquipmentId) AS Row,
 dbo.getEquipmentName(M.EquipmentId)EquipmentName,
 MaintananceId,
 ISNULL(VP.VesselPosition,'') AS PositionInVessal,
 ClassCode,
 MaintananceName,
 dbo.getMaintananceType(MaintanaceType)MaintanaceType,
 dbo.getFrequecyType(FrequencyType)FrequencyType,
 Frequency,
 CONVERT(VARCHAR(10), M.LastDoneDate, 105)LastDoneDate,
 LastDoneRunningHours,CONVERT(VARCHAR(10), NextDueDate, 105) NextDueDate,
 NextDueRunningHours, dbo.getResposibility(Responsibility)Responsibility,
 C.Remarks,
 C.JobCompletedAt,
 C.NameOfPort,
 C.JobCarriedOutBy,
 C.NameOfWorkShop,
 C.MaintanaceStatus
 FROM dbo.PMS_TBL_MAINTANANCE_MASTER M
 INNER JOIN PMS_TBL_MAINTANANCE_COMPLETION C ON C.MaintenanceId=M.MaintananceId
 LEFT OUTER JOIN PMS_TBL_EQUIPMENT_MASTER E ON M.EquipmentId=convert(varchar(36),E.UniqueId)
 LEFT OUTER JOIN PMS_TBL_SUB_EQUIPMENT_MASTER S ON M.EquipmentId=convert(varchar(36),S.UniqueId)
 INNER JOIN PMS_TBL_EUIPMENT_VESSELPOSITION_MASTER  VP where VP.VesselPositionId = E.PositionInVessal

Do the same for
- getMaintananceType
- getFrequecyType
- getEquipmentName
- getResponsibility

Potentially ne of the reasons you are having slow running queries with these joins is because you are converting Id's to varchar for each join - consider changing your schema so that ALL ids are int or bigint.

Are you actually retrieving data from PMS_TBL_SUB_EQUIPMENT_MASTER as you haven't use the S alias anywhere in your query.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900