<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>youdidwhatwithtsql.com &#187; sp_spaceused</title>
	<atom:link href="http://www.youdidwhatwithtsql.com/tag/sp_spaceused/feed" rel="self" type="application/rss+xml" />
	<link>http://www.youdidwhatwithtsql.com</link>
	<description>making DBAs everywhere curse!</description>
	<lastBuildDate>Tue, 31 Jan 2012 12:21:23 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>MySQL clone of sp_spaceused</title>
		<link>http://www.youdidwhatwithtsql.com/mysql-clone-of-sp_spaceused/636</link>
		<comments>http://www.youdidwhatwithtsql.com/mysql-clone-of-sp_spaceused/636#comments</comments>
		<pubDate>Sun, 14 Feb 2010 16:58:03 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[DBA]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[sp_spaceused]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/mysql-clone-of-sp_spaceused/636</guid>
		<description><![CDATA[Following on from yesterdays blog post, a MySQL clone of sp_MsForEachTable, here&#8217;s an attempt at a clone of sp_spaceused. There&#8217;s a few issues to be aware of involving the storage engine in use. For example the row count is accurate for MyISAM while with InnoDb it seems to be just an estimate. This estimate can [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/mysql-clone-of-sp_spaceused/636">MySQL clone of sp_spaceused</a></p>
]]></description>
			<content:encoded><![CDATA[<p>Following on from yesterdays blog post, a <a href="http://www.youdidwhatwithtsql.com/mysql-clone-of-sp_msforeachtable/624" target="_blank">MySQL clone of sp_MsForEachTable</a>, here&#8217;s an attempt at a clone of <a href="http://msdn.microsoft.com/en-us/library/ms188776.aspx" target="_blank">sp_spaceused</a>. There&#8217;s a few issues to be aware of involving the storage engine in use. For example the row count is accurate for <a href="http://en.wikipedia.org/wiki/MyISAM" target="_blank">MyISAM</a> while with <a href="http://en.wikipedia.org/wiki/InnoDB" target="_blank">InnoDb</a> it seems to be just an estimate. This estimate can vary from execution-to-execution. The procedure derives its information from <a href="http://dev.mysql.com/doc/refman/5.1/en/tables-table.html" target="_blank">INFORMATION_SCHEMA.TABLES</a>. To get started create the below procedure in the database you wish to use it in.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p636code4'); return false;">View Code</a> MYSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p6364"><td class="code" id="p636code4"><pre class="mysql" style="font-family:monospace;">DELIMITER $$
&nbsp;
<span style="color: #990099; font-weight: bold;">DROP</span> <span style="color: #990099; font-weight: bold;">PROCEDURE</span> <span style="color: #009900;">IF</span> <span style="color: #990099; font-weight: bold;">EXISTS</span> <span style="color: #008000;">`usp<span style="color: #008080; font-weight: bold;">_</span>mysql<span style="color: #008080; font-weight: bold;">_</span>spaceused`</span>$$
&nbsp;
<span style="color: #990099; font-weight: bold;">CREATE</span> <span style="color: #990099; font-weight: bold;">PROCEDURE</span> <span style="color: #008000;">`usp<span style="color: #008080; font-weight: bold;">_</span>mysql<span style="color: #008080; font-weight: bold;">_</span>spaceused`</span><span style="color: #FF00FF;">&#40;</span><span style="color: #990099; font-weight: bold;">IN</span> var_tablename <span style="color: #999900; font-weight: bold;">VARCHAR</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">255</span><span style="color: #FF00FF;">&#41;</span><span style="color: #FF00FF;">&#41;</span>
    <span style="color: #990099; font-weight: bold;">LANGUAGE SQL</span>
    <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #990099; font-weight: bold;">DETERMINISTIC</span>
    <span style="color: #990099; font-weight: bold;">COMMENT</span> <span style="color: #008000;">'Clone of MSSQL sp<span style="color: #008080; font-weight: bold;">_</span>spaceused'</span>
    <span style="color: #990099; font-weight: bold;">BEGIN</span>
	<span style="color: #808080; font-style: italic;"># Based on http://msdn.microsoft.com/en-us/library/ms188776.aspx</span>
	<span style="color: #009900;">IF</span> var_tablename <span style="color: #CC0099;">=</span> <span style="color: #008000;">''</span> <span style="color: #CC0099; font-weight: bold;">OR</span> var_tablename <span style="color: #CC0099; font-weight: bold;">IS</span> <span style="color: #9900FF; font-weight: bold;">NULL</span> <span style="color: #009900;">THEN</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">-- db info, unallocated_space only reported for InnoDB tables</span>
		<span style="color: #990099; font-weight: bold;">SELECT</span> table_schema <span style="color: #990099; font-weight: bold;">AS</span> database_name<span style="color: #000033;">,</span>
		       <span style="color: #000099;">CONCAT</span><span style="color: #FF00FF;">&#40;</span><span style="color: #000099;">ROUND</span><span style="color: #FF00FF;">&#40;</span><span style="color: #000099;">SUM</span><span style="color: #FF00FF;">&#40;</span>data_length <span style="color: #CC0099;">+</span> index_length <span style="color: #CC0099;">+</span> data_free<span style="color: #FF00FF;">&#41;</span><span style="color: #CC0099;">/</span> <span style="color: #008080;">1024</span> <span style="color: #CC0099;">/</span> <span style="color: #008080;">1024</span><span style="color: #000033;">,</span> <span style="color: #008080;">2</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">,</span> <span style="color: #008000;">'(MB)'</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #990099; font-weight: bold;">AS</span> database_size<span style="color: #000033;">,</span>
		       <span style="color: #000099;">CONCAT</span><span style="color: #FF00FF;">&#40;</span><span style="color: #000099;">ROUND</span><span style="color: #FF00FF;">&#40;</span><span style="color: #000099;">SUM</span><span style="color: #FF00FF;">&#40;</span>data_free<span style="color: #FF00FF;">&#41;</span> <span style="color: #CC0099;">/</span> <span style="color: #008080;">1024</span> <span style="color: #CC0099;">/</span><span style="color: #008080;">1024</span><span style="color: #000033;">,</span> <span style="color: #008080;">2</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">,</span> <span style="color: #008000;">'(MB)'</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #990099; font-weight: bold;">AS</span> unallocated_space
		<span style="color: #990099; font-weight: bold;">FROM</span> information_schema.<span style="color: #990099; font-weight: bold;">TABLES</span>
		<span style="color: #990099; font-weight: bold;">WHERE</span> table_schema <span style="color: #CC0099;">=</span> <span style="color: #000099;">DATABASE</span><span style="color: #FF00FF;">&#40;</span><span style="color: #FF00FF;">&#41;</span>
		<span style="color: #990099; font-weight: bold;">GROUP BY</span> table_schema<span style="color: #000033;">;</span>
&nbsp;
		<span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #000099;">CONCAT</span><span style="color: #FF00FF;">&#40;</span><span style="color: #000099;">ROUND</span><span style="color: #FF00FF;">&#40;</span><span style="color: #000099;">SUM</span><span style="color: #FF00FF;">&#40;</span>data_length <span style="color: #CC0099;">+</span> index_length <span style="color: #CC0099;">+</span> data_free<span style="color: #FF00FF;">&#41;</span> <span style="color: #CC0099;">/</span> <span style="color: #008080;">1024</span> <span style="color: #CC0099;">/</span><span style="color: #008080;">1024</span><span style="color: #000033;">,</span> <span style="color: #008080;">2</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">,</span> <span style="color: #008000;">'(MB)'</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #990099; font-weight: bold;">AS</span> reserved<span style="color: #000033;">,</span>
		       <span style="color: #000099;">CONCAT</span><span style="color: #FF00FF;">&#40;</span><span style="color: #000099;">ROUND</span><span style="color: #FF00FF;">&#40;</span><span style="color: #000099;">SUM</span><span style="color: #FF00FF;">&#40;</span>data_length<span style="color: #FF00FF;">&#41;</span> <span style="color: #CC0099;">/</span> <span style="color: #008080;">1024</span> <span style="color: #CC0099;">/</span><span style="color: #008080;">1024</span><span style="color: #000033;">,</span> <span style="color: #008080;">2</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">,</span> <span style="color: #008000;">'(MB)'</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #990099; font-weight: bold;">AS</span> <span style="color: #990099; font-weight: bold;">data</span><span style="color: #000033;">,</span>
		       <span style="color: #000099;">CONCAT</span><span style="color: #FF00FF;">&#40;</span><span style="color: #000099;">ROUND</span><span style="color: #FF00FF;">&#40;</span><span style="color: #000099;">SUM</span><span style="color: #FF00FF;">&#40;</span>index_length<span style="color: #FF00FF;">&#41;</span> <span style="color: #CC0099;">/</span> <span style="color: #008080;">1024</span> <span style="color: #CC0099;">/</span><span style="color: #008080;">1024</span><span style="color: #000033;">,</span> <span style="color: #008080;">2</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">,</span> <span style="color: #008000;">'(MB)'</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #990099; font-weight: bold;">AS</span> index_size<span style="color: #000033;">,</span>
		       <span style="color: #000099;">CONCAT</span><span style="color: #FF00FF;">&#40;</span><span style="color: #000099;">ROUND</span><span style="color: #FF00FF;">&#40;</span><span style="color: #000099;">SUM</span><span style="color: #FF00FF;">&#40;</span>data_free<span style="color: #FF00FF;">&#41;</span> <span style="color: #CC0099;">/</span> <span style="color: #008080;">1024</span> <span style="color: #CC0099;">/</span><span style="color: #008080;">1024</span><span style="color: #000033;">,</span> <span style="color: #008080;">2</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">,</span> <span style="color: #008000;">'(MB)'</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #990099; font-weight: bold;">AS</span> unused
		<span style="color: #990099; font-weight: bold;">FROM</span> information_schema.<span style="color: #990099; font-weight: bold;">TABLES</span>
		<span style="color: #990099; font-weight: bold;">WHERE</span> TABLE_SCHEMA <span style="color: #CC0099;">=</span> <span style="color: #000099;">DATABASE</span><span style="color: #FF00FF;">&#40;</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span>
&nbsp;
	<span style="color: #009900;">ELSE</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">-- table info</span>
		<span style="color: #808080; font-style: italic;">-- does the table exist in the current db?</span>
		<span style="color: #990099; font-weight: bold;">IF <span style="color: #CC0099; font-weight: bold;">NOT</span> EXISTS</span> <span style="color: #FF00FF;">&#40;</span><span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #008080;">1</span> <span style="color: #990099; font-weight: bold;">FROM</span> information_schema.<span style="color: #990099; font-weight: bold;">TABLES</span> <span style="color: #990099; font-weight: bold;">WHERE</span> TABLE_NAME <span style="color: #CC0099;">=</span> var_tablename<span style="color: #FF00FF;">&#41;</span> <span style="color: #009900;">THEN</span>
&nbsp;
			<span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #008000;">'Table does not exist.'</span> <span style="color: #990099; font-weight: bold;">AS</span> error<span style="color: #000033;">;</span>
		<span style="color: #009900;">ELSE</span>
&nbsp;
			<span style="color: #808080; font-style: italic;">-- rows is estimate based for InnoDb (often a fair bit out)</span>
			<span style="color: #808080; font-style: italic;">-- MyISAM is accurate</span>
			<span style="color: #990099; font-weight: bold;">SELECT</span> var_tablename <span style="color: #990099; font-weight: bold;">AS</span> <span style="color: #008000;">`name`</span><span style="color: #000033;">,</span>
			       table_rows <span style="color: #990099; font-weight: bold;">AS</span> rows<span style="color: #000033;">,</span>
			       <span style="color: #000099;">CONCAT</span><span style="color: #FF00FF;">&#40;</span><span style="color: #000099;">ROUND</span><span style="color: #FF00FF;">&#40;</span><span style="color: #000099;">SUM</span><span style="color: #FF00FF;">&#40;</span>data_length <span style="color: #CC0099;">+</span> index_length <span style="color: #CC0099;">+</span> data_free<span style="color: #FF00FF;">&#41;</span> <span style="color: #CC0099;">/</span> <span style="color: #008080;">1024</span> <span style="color: #CC0099;">/</span><span style="color: #008080;">1024</span><span style="color: #000033;">,</span> <span style="color: #008080;">2</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">,</span> <span style="color: #008000;">'(MB)'</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #990099; font-weight: bold;">AS</span> reserved<span style="color: #000033;">,</span>
			       <span style="color: #000099;">CONCAT</span><span style="color: #FF00FF;">&#40;</span><span style="color: #000099;">ROUND</span><span style="color: #FF00FF;">&#40;</span><span style="color: #000099;">SUM</span><span style="color: #FF00FF;">&#40;</span>data_length<span style="color: #FF00FF;">&#41;</span> <span style="color: #CC0099;">/</span> <span style="color: #008080;">1024</span> <span style="color: #CC0099;">/</span><span style="color: #008080;">1024</span><span style="color: #000033;">,</span> <span style="color: #008080;">2</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">,</span> <span style="color: #008000;">'(MB)'</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #990099; font-weight: bold;">AS</span> <span style="color: #990099; font-weight: bold;">data</span><span style="color: #000033;">,</span>
			       <span style="color: #000099;">CONCAT</span><span style="color: #FF00FF;">&#40;</span><span style="color: #000099;">ROUND</span><span style="color: #FF00FF;">&#40;</span><span style="color: #000099;">SUM</span><span style="color: #FF00FF;">&#40;</span>index_length<span style="color: #FF00FF;">&#41;</span> <span style="color: #CC0099;">/</span> <span style="color: #008080;">1024</span> <span style="color: #CC0099;">/</span><span style="color: #008080;">1024</span><span style="color: #000033;">,</span> <span style="color: #008080;">2</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">,</span> <span style="color: #008000;">'(MB)'</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #990099; font-weight: bold;">AS</span> index_size<span style="color: #000033;">,</span>
			       <span style="color: #000099;">CONCAT</span><span style="color: #FF00FF;">&#40;</span><span style="color: #000099;">ROUND</span><span style="color: #FF00FF;">&#40;</span><span style="color: #000099;">SUM</span><span style="color: #FF00FF;">&#40;</span>data_free<span style="color: #FF00FF;">&#41;</span> <span style="color: #CC0099;">/</span> <span style="color: #008080;">1024</span> <span style="color: #CC0099;">/</span><span style="color: #008080;">1024</span><span style="color: #000033;">,</span> <span style="color: #008080;">2</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">,</span> <span style="color: #008000;">'(MB)'</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #990099; font-weight: bold;">AS</span> unused <span style="color: #808080; font-style: italic;">-- InnoDb only MySQL 5.1.28</span>
			<span style="color: #990099; font-weight: bold;">FROM</span> information_schema.<span style="color: #990099; font-weight: bold;">TABLES</span>
			<span style="color: #990099; font-weight: bold;">WHERE</span> TABLE_SCHEMA <span style="color: #CC0099;">=</span> <span style="color: #000099;">DATABASE</span><span style="color: #FF00FF;">&#40;</span><span style="color: #FF00FF;">&#41;</span>
			<span style="color: #CC0099; font-weight: bold;">AND</span> TABLE_NAME <span style="color: #CC0099;">=</span> var_tablename<span style="color: #000033;">;</span>
&nbsp;
		<span style="color: #009900;">END</span> <span style="color: #009900;">IF</span><span style="color: #000033;">;</span>
&nbsp;
	<span style="color: #009900;">END</span> <span style="color: #009900;">IF</span><span style="color: #000033;">;</span>
&nbsp;
    <span style="color: #009900;">END</span>$$
&nbsp;
DELIMITER <span style="color: #000033;">;</span></pre></td></tr></table></div>

<p>Usage is as follows.</p>
<p><strong>Get database size information</strong></p>
<p>To get database level information the procedure should be called with an empty string or NULL.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p636code5'); return false;">View Code</a> MYSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p6365"><td class="code" id="p636code5"><pre class="mysql" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">-- Empty string or null to get db info</span>
<span style="color: #990099; font-weight: bold;">CALL</span> usp_mysql_spaceused<span style="color: #FF00FF;">&#40;</span><span style="color: #008000;">''</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span></pre></td></tr></table></div>

<p>Two resultset are returned. Similar to the ones described on the <a href="http://msdn.microsoft.com/en-us/library/ms188776.aspx" target="_blank">sp_spaceused documentation</a> page.</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/02/usp_mysql_spaceused_resultset_1.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="usp_mysql_spaceused resultset 1" border="0" alt="usp mysql spaceused resultset 1 thumb MySQL clone of sp spaceused" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/02/usp_mysql_spaceused_resultset_1_thumb.png" width="644" height="160" /></a> </p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/02/usp_mysql_spaceused_resultset_2.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="usp_mysql_spaceused resultset 2" border="0" alt="usp mysql spaceused resultset 2 thumb MySQL clone of sp spaceused" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/02/usp_mysql_spaceused_resultset_2_thumb.png" width="644" height="175" /></a> </p>
<p><strong>Get table size information</strong></p>
<p><font color="#666666">To get information on the size of a table just call the procedure with the appropriate table name.</font></p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p636code6'); return false;">View Code</a> MYSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p6366"><td class="code" id="p636code6"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">CALL</span> usp_mysql_spaceused<span style="color: #FF00FF;">&#40;</span><span style="color: #008000;">'City'</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span></pre></td></tr></table></div>

<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/02/usp_mysql_spaceused_table_resultset.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="usp_mysql_spaceused_table_resultset" border="0" alt="usp mysql spaceused table resultset thumb MySQL clone of sp spaceused" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/02/usp_mysql_spaceused_table_resultset_thumb.png" width="644" height="131" /></a></p>
<p><map name='google_ad_map_636_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/636?pos=0' coords='1,2,367,28' />
<area shape='rect' href='http://services.google.com/feedback/abg' coords='384,10,453,23'/></map>
<img usemap='#google_ad_map_636_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=636&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Fmysql-clone-of-sp_spaceused%2F636' title="MySQL clone of sp spaceused" alt=" MySQL clone of sp spaceused" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/mysql-clone-of-sp_spaceused/636">MySQL clone of sp_spaceused</a></p>
<div class="none"><div class="g-plusone" data-href="http://www.youdidwhatwithtsql.com/mysql-clone-of-sp_spaceused/636" size="standard" count="true"></div></div>]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/mysql-clone-of-sp_spaceused/636/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

