SQL query for table analysisHi,<br/><br/>One of my database tables (SQL Server 2005 Express) has got some 70 to 80 columns in it. After going live, I noted that practically many of these columns often do not receive a value and remain NULL. So now I am planning to split the table into two pieces, one of which would receive only the heavily used columns and the other one having the rarely used ones, and join these two thru a 1-1 relation, thus expecting SQL Server to perform better becuz in most cases it won't have to access the rarely used columns table. I have some 1.5 million records in the table and need to run a query which could tell me the percent usage of each column (means what pecentage of the total rows had non-NULL values in that particular column), something like:<br/><br/>ColumnA - 78.5%<br/>ColumnB - 60.9%<br/>ColumnC - 1.4%<br/><br/>I could have done it using COUNT(ColumnA) with IS NULL, but doing that thing 70 to 80 times seems to be a lot of work. Is there any query that could return the percentage for all columns? Also would it be a good idea to do this splitting anyway? If yes, what are the other recommendations when I do splitting, like indexes, physical files etc.<br/><br/>Thanks<br/>Shujaat© 2009 Microsoft Corporation. All rights reserved.Wed, 17 Jun 2009 07:57:45 Z0ff65aac-ab53-4c94-8b7f-064c35eaafafhttp://social.msdn.microsoft.com/Forums/en-US/ssdsgetstarted/thread/0ff65aac-ab53-4c94-8b7f-064c35eaafaf#0ff65aac-ab53-4c94-8b7f-064c35eaafafhttp://social.msdn.microsoft.com/Forums/en-US/ssdsgetstarted/thread/0ff65aac-ab53-4c94-8b7f-064c35eaafaf#0ff65aac-ab53-4c94-8b7f-064c35eaafafSyed Shujaat Hussainhttp://social.msdn.microsoft.com/Profile/en-US/?user=Syed%20Shujaat%20HussainSQL query for table analysisHi,<br/><br/>One of my database tables (SQL Server 2005 Express) has got some 70 to 80 columns in it. After going live, I noted that practically many of these columns often do not receive a value and remain NULL. So now I am planning to split the table into two pieces, one of which would receive only the heavily used columns and the other one having the rarely used ones, and join these two thru a 1-1 relation, thus expecting SQL Server to perform better becuz in most cases it won't have to access the rarely used columns table. I have some 1.5 million records in the table and need to run a query which could tell me the percent usage of each column (means what pecentage of the total rows had non-NULL values in that particular column), something like:<br/><br/>ColumnA - 78.5%<br/>ColumnB - 60.9%<br/>ColumnC - 1.4%<br/><br/>I could have done it using COUNT(ColumnA) with IS NULL, but doing that thing 70 to 80 times seems to be a lot of work. Is there any query that could return the percentage for all columns? Also would it be a good idea to do this splitting anyway? If yes, what are the other recommendations when I do splitting, like indexes, physical files etc.<br/><br/>Thanks<br/>ShujaatWed, 17 Jun 2009 06:58:54 Z2009-06-17T07:00:41Zhttp://social.msdn.microsoft.com/Forums/en-US/ssdsgetstarted/thread/0ff65aac-ab53-4c94-8b7f-064c35eaafaf#73e835cd-2863-445b-ac57-c520b5753acdhttp://social.msdn.microsoft.com/Forums/en-US/ssdsgetstarted/thread/0ff65aac-ab53-4c94-8b7f-064c35eaafaf#73e835cd-2863-445b-ac57-c520b5753acdSyed Shujaat Hussainhttp://social.msdn.microsoft.com/Profile/en-US/?user=Syed%20Shujaat%20HussainSQL query for table analysis<span style="font-size:x-small"> <p>Finally found it. Just in case it would help someone else, here's the way to do it. Replace TotalRowsCount with the result of &quot;SELECT COUNT(*) FROM YourTable&quot;.<br/><br/>DECLARE @col varchar(255), @cmd varchar(max)</p> <p>DECLARE getinfo cursor for</p> <p>SELECT c.name FROM sys.tables t JOIN sys.columns c ON t.Object_ID = c.Object_ID</p> <p>WHERE t.Name = 'YourTable'</p> <p>OPEN getinfo</p> <p>SET @cmd = ''</p> <p>FETCH NEXT FROM getinfo into @col</p> <p>WHILE @@FETCH_STATUS = 0</p> <p>BEGIN</p> <p>SET @cmd = @cmd + '(SELECT ''' + @col + ''', CONVERT(varchar,cONVERT(real, (SELECT COUNT(*) FROM YourTable WHERE ([' + @col +</p> <p>'] IS NULL) OR (LTRIM(RTRIM([' + @col + '])) = ''''))) / TotalRowsCount)) UNION '</p> <p> </p> <p>FETCH NEXT FROM getinfo into @col</p> <p>END</p> <p>SET @cmd = SUBSTRING(@cmd, 1, LEN(@cmd) - 6)</p> <p>EXEC(@cmd)</p> <p>CLOSE getinfo</p> <p>DEALLOCATE getinfo</p> </span>Wed, 17 Jun 2009 07:57:30 Z2009-06-17T07:57:30Z