sql_variant Vs varchar
I have done small comparison between sql_variant Vs varchar data types. not seen big different than I have expected, but it's interesting information,
use [tempdb]GOIF OBJECT_ID('tblVarChar') IS NOT NULL BEGINDROP TABLE tblVarCharENDIF OBJECT_ID('tblsql_variant') IS NOT NULL BEGINDROP TABLE tblsql_variantENDGOCREATE TABLE tblVarChar(name varchar(200));CREATE NONCLUSTERED INDEX IX_tblVarChar ON tblVarChar (name);CREATE TABLE tblsql_variant(name sql_variant);CREATE NONCLUSTERED INDEX tblsql_variant ON tblsql_variant (name);GOINSERT INTO tblVarChar VALUES('http://www.databaseusergroup.com');INSERT INTO tblsql_variant VALUES('http://www.databaseusergroup.com');GO 100INSERT INTO tblVarChar VALUES('255');INSERT INTO tblsql_variant VALUES(CAST(255 AS tinyint));GO 1000INSERT INTO tblVarChar VALUES('32767');INSERT INTO tblsql_variant VALUES(CAST(32767 AS smallint));GO 1000INSERT INTO tblVarChar VALUES('9223372036854775807');INSERT INTO tblsql_variant VALUES(CAST(9223372036854775807 AS bigint));GO 1000INSERT INTO tblVarChar VALUES('2016-02-23 12:46:07');INSERT INTO tblsql_variant VALUES(CAST('2016-02-23 12:46:07.347' AS smalldatetime));GO 1000PRINT 'tinyint'SET STATISTICS IO ONSELECT * FROM tblVarChar WHERE name='255';GOSELECT * FROM tblsql_variant WHERE name=255;SET STATISTICS IO OFFGOPRINT '----------------------------------------------------------------------------------------'PRINT 'smallint'SET STATISTICS IO ONSELECT * FROM tblVarChar WHERE name='32767';GOSELECT * FROM tblsql_variant WHERE name=32767;SET STATISTICS IO OFFGOPRINT '----------------------------------------------------------------------------------------'PRINT 'bigint'SET STATISTICS IO ONSELECT * FROM tblVarChar WHERE name='9223372036854775807';GOSELECT * FROM tblsql_variant WHERE name=9223372036854775807;SET STATISTICS IO OFFGOPRINT '----------------------------------------------------------------------------------------'PRINT 'smalldatetime'SET STATISTICS IO ONSELECT * FROM tblVarChar WHERE name='2016-02-23 12:46:07';GOSELECT * FROM tblsql_variant WHERE name='2016-02-23 12:46:07';SET STATISTICS IO OFFGOPRINT '----------------------------------------------------------------------------------------'PRINT 'varchar'SET STATISTICS IO ONSELECT * FROM tblVarChar WHERE name='http://www.databaseusergroup.com';GOSELECT * FROM tblsql_variant WHERE name='http://www.databaseusergroup.com';SET STATISTICS IO OFFGOsp_spaceused 'tblVarChar';GOsp_spaceused 'tblsql_variant';GOSELECT DISTINCT SQL_VARIANT_PROPERTY(name,'BaseType') BaseType,SQL_VARIANT_PROPERTY(name,'Precision') Precisions,SQL_VARIANT_PROPERTY(name,'Scale') Scale,SQL_VARIANT_PROPERTY(name,'TotalBytes') TotalBytes,SQL_VARIANT_PROPERTY(name,'Collation') Collation,SQL_VARIANT_PROPERTY(name,'MaxLength') MaxLengthsFROM tblsql_variant;GO
with above SQL example you will notice less page read for "smalldatetime" and "varchar" others almost same. but i'm bit wondering "tinyint" has more pages than "varchar". not really sure why this has allocated extra page. any way it's good if you are using "sql_variant" data type then ignore staring high allocated pages data types.
No comments:
Post a Comment