C#ASP.NetアプリケーションからSQL関数(UDF)を呼び出しています。私が呼び出している関数には、「uniqueidentifier」タイプのパラメーターが1つ必要です。
呼び出しを行い、SqlCommandのCommandTextで「null」を渡すと、結果は約3秒で返されます...
SqlCommand Command = new SqlCommand();
Command.Connection = (SqlConnection)(DbDataModifier.CreateConnection());
Command.CommandText = "select * from GetLocalFirmLoginsSummary(null) order by Date asc"
Command.CommandType = CommandType.Text;
Command.Connection.Open();
SqlDataReader Reader = Command.ExecuteReader(); // takes 3 seconds
しかし、呼び出しを行ってDBNull.ValueをSqlParameterとして渡すと、結果が返されるまでに60秒以上かかります...
SqlCommand Command = new SqlCommand();
Command.Connection = (SqlConnection)(DbDataModifier.CreateConnection());
Command.CommandText = "select * from GetLocalFirmLoginsSummary(@CustomerGroupID) order by Date asc"
Command.CommandType = CommandType.Text;
SqlParameter Param = new SqlParameter();
Param.ParameterName = "@CustomerGroupID";
Param.SqlDbType = SqlDbType.UniqueIdentifier;
Param.Direction = ParameterDirection.Input;
Param.IsNullable = true;
Param.Value = DBNull.Value;
Command.Parameters.Add(Param);
Command.Connection.Open();
SqlDataReader Reader = Command.ExecuteReader(); // takes over 60 seconds
SQL Management Studioで同じクエリを実行すると、パラメータとしてnullを渡した場合でも、約3秒かかります...
declare @CustomerGroupID uniqueidentifier
set @CustomerGroupID = null
select * from GetLocalFirmLoginsSummary(@CustomerGroupID)
私が呼び出している関数は次のように定義されています。
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER FUNCTION [dbo].[GetLocalFirmLoginsSummary]
(
@CustomerGroupID uniqueidentifier
)
RETURNS TABLE
AS
RETURN
(
SELECT CustomerGroupID, CustomerGroupName, USR, DATEADD(MONTH, DATEDIFF(MONTH, 0, createdDate), 0) AS Date, COUNT(*) AS Quantity
FROM dbo.GetLocalFirmLogins(@CustomerGroupID) AS Logins
GROUP BY USR, CustomerGroupID, CustomerGroupName, DATEADD(MONTH, DATEDIFF(MONTH, 0, createdDate), 0)
)
..。
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER FUNCTION [dbo].[GetLocalFirmLogins]
(
@CustomerGroupID uniqueidentifier
)
RETURNS TABLE
AS
RETURN
(
SELECT vw_CustomerGroupAccountDetails.CustomerGroupID, vw_CustomerGroupAccountDetails.CustomerGroupName, Organisations.USR, Organisations.town AS FirmTown,
Users.id AS ID, Users.userName, Users.password, Users.isPartner, Users.isFeeEarner, Users.nlisUserName, Users.nlisPassword, Users.email, Users.lastLoginDate, Users.createdDate
FROM vw_CustomerGroupAccountDetails RIGHT OUTER JOIN
Organisations ON vw_CustomerGroupAccountDetails.AccountID = CAST(Organisations.id AS nvarchar(50)) RIGHT OUTER JOIN
Users ON Organisations.clientId = Users.uploadedBy
WHERE (Users.canLogin = 1) AND (Users.isDeleted = 0) AND (NOT (Organisations.USR IS NULL)) AND
((vw_CustomerGroupAccountDetails.CustomerGroupID = @CustomerGroupID) OR (@CustomerGroupID IS NULL))
)
では、SqlCommandのSqlParameterを使用してC#からSQL関数を呼び出すと、何が原因でこれほど時間がかかるのでしょうか。
SQLServer2000を使用しています。
私はどこでも、考えられるあらゆる方法で検索しましたが、同じ問題を抱えている人を他に見つけることはできません。