जोनाथा डिकिंसन के साथ मेरी चर्चा (उनके उत्तर को देखें) ने मुझे कुछ त्वरित परीक्षण करने के लिए प्रेरित किया:
यह पता चला है, कि शुद्ध,एम्बेडेड स्केलर उप-चयन इतना बुरा नहीं है। केवल एक मान क्वेरी करना यह और भी तेज़ है . जैसा कि अपेक्षित था अदिश समारोह खराब है। TVF द्वारा जितने अधिक क्षेत्र वापस दिए जाते हैं, सापेक्ष प्रदर्शन उतना ही बेहतर होता है।
एकमात्र निश्चित उत्तर है:स्केलर फ़ंक्शन सबसे खराब है और एक बहु-पंक्ति TVF है - अधिकांश समय - इनलाइन की तुलना में धीमा। कोई भी तदर्थ दृष्टिकोण अधिक तेज़ होता है ।
लेकिन मैं सभी स्थितियों (स्केलर फ़ंक्शन को छोड़कर) के लिए विशेष मामले सेट कर सकता था, जहां एक दृष्टिकोण सबसे तेज़ था।
निष्कर्ष:(हमेशा की तरह :-)) यह निर्भर करता है...
संकेत:इसे कई तालिकाओं और स्तंभों वाले बड़े डेटाबेस के विरुद्ध जाने देना सबसे अच्छा है।
CREATE FUNCTION dbo.CountColumnScalar(@TableSchema AS VARCHAR(100),@TableName AS VARCHAR(100))
RETURNS INT
AS
BEGIN
RETURN(SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS AS c WHERE [email protected] AND [email protected]);
END
GO
CREATE FUNCTION dbo.CountConstraintScalar(@TableSchema AS VARCHAR(100),@TableName AS VARCHAR(100))
RETURNS INT
AS
BEGIN
RETURN(SELECT COUNT(*) FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE AS c WHERE [email protected] AND [email protected]);
END
GO
CREATE FUNCTION dbo.CountAllTVF(@TableSchema AS VARCHAR(100),@TableName AS VARCHAR(100))
RETURNS TABLE
RETURN SELECT (SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS AS c WHERE [email protected] AND [email protected] GROUP BY c.TABLE_SCHEMA,c.TABLE_NAME) AS ColCounter
,(SELECT COUNT(*) FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE AS c WHERE [email protected] AND [email protected] GROUP BY c.TABLE_SCHEMA,c.TABLE_NAME) AS ConstraintCounter ;
GO
CREATE FUNCTION dbo.CountAllTVF_multiline(@TableSchema AS VARCHAR(100),@TableName AS VARCHAR(100))
RETURNS @tbl TABLE (ColCounter INT,ConstraintCounter INT)
AS
BEGIN
INSERT INTO @tbl
SELECT (SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS AS c WHERE [email protected] AND [email protected] GROUP BY c.TABLE_SCHEMA,c.TABLE_NAME) AS ColCounter
,(SELECT COUNT(*) FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE AS c WHERE [email protected] AND [email protected] GROUP BY c.TABLE_SCHEMA,c.TABLE_NAME) AS ConstraintCounter;
RETURN;
END
GO
DECLARE @time DATETIME=GETDATE();
SELECT TABLE_SCHEMA,TABLE_NAME
,(SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS AS c WHERE c.TABLE_SCHEMA=t.TABLE_SCHEMA AND c.TABLE_NAME=t.TABLE_NAME ) AS ColCounter
,(SELECT COUNT(*) FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE AS c WHERE c.TABLE_SCHEMA=t.TABLE_SCHEMA AND c.TABLE_NAME=t.TABLE_NAME ) AS ConstraintCounter
FROM INFORMATION_SCHEMA.TABLES AS t;
PRINT 'pure embedded scalar sub-select: ' + CAST(CAST(GETDATE()[email protected] AS TIME) AS VARCHAR(MAX)); SET @time=GETDATE();
SELECT TABLE_SCHEMA,TABLE_NAME
,dbo.CountColumnScalar(t.TABLE_SCHEMA,t.TABLE_NAME ) AS ColCounter
,dbo.CountConstraintScalar(t.TABLE_SCHEMA,t.TABLE_NAME ) AS ConstraintCount
FROM INFORMATION_SCHEMA.TABLES AS t
PRINT 'scalar function: ' + CAST(CAST(GETDATE()[email protected] AS TIME) AS VARCHAR(MAX)); SET @time=GETDATE();
SELECT t.TABLE_SCHEMA,t.TABLE_NAME
,colJoin.ColCount
,conJoin.ConstraintCount
FROM INFORMATION_SCHEMA.TABLES AS t
INNER JOIN (SELECT COUNT(*) As ColCount,c.TABLE_SCHEMA,c.TABLE_NAME
FROM INFORMATION_SCHEMA.COLUMNS AS c
GROUP BY c.TABLE_SCHEMA,c.TABLE_NAME) AS colJoin ON colJoin.TABLE_SCHEMA=t.TABLE_SCHEMA AND colJoin.TABLE_NAME=t.TABLE_NAME
INNER JOIN (SELECT COUNT(*) As ConstraintCount,c.TABLE_SCHEMA,c.TABLE_NAME
FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE AS c
GROUP BY c.TABLE_SCHEMA,c.TABLE_NAME) AS conJoin ON conJoin.TABLE_SCHEMA=t.TABLE_SCHEMA AND conJoin.TABLE_NAME=t.TABLE_NAME
PRINT 'JOINs on sub-selects: ' + CAST(CAST(GETDATE()[email protected] AS TIME) AS VARCHAR(MAX)); SET @time=GETDATE();
SELECT t.TABLE_SCHEMA,t.TABLE_NAME
,ColCounter.*
FROM INFORMATION_SCHEMA.TABLES AS t
CROSS APPLY dbo.CountAllTVF(t.TABLE_SCHEMA,t.TABLE_NAME) AS ColCounter
PRINT 'TVF inline: ' + CAST(CAST(GETDATE()[email protected] AS TIME) AS VARCHAR(MAX)); SET @time=GETDATE();
SELECT t.TABLE_SCHEMA,t.TABLE_NAME
,ColCounter.*
FROM INFORMATION_SCHEMA.TABLES AS t
CROSS APPLY dbo.CountAllTVF_multiline(t.TABLE_SCHEMA,t.TABLE_NAME) AS ColCounter
PRINT 'TVF multiline: ' + CAST(CAST(GETDATE()[email protected] AS TIME) AS VARCHAR(MAX)); SET @time=GETDATE();
GO
DROP FUNCTION dbo.CountColumnScalar;
DROP FUNCTION dbo.CountAllTVF;
DROP FUNCTION dbo.CountAllTVF_multiline;
DROP FUNCTION dbo.CountConstraintScalar;