<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: Using VIEW to reduce number of tables used</title>
	<atom:link href="http://www.mysqlperformanceblog.com/2007/09/28/using-view-to-reduce-number-of-tables-used/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.mysqlperformanceblog.com/2007/09/28/using-view-to-reduce-number-of-tables-used/</link>
	<description>Everything about MySQL Performance</description>
	<lastBuildDate>Sat, 21 Nov 2009 05:23:57 -0800</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: http://kathyravan.blogspot.com/</title>
		<link>http://www.mysqlperformanceblog.com/2007/09/28/using-view-to-reduce-number-of-tables-used/comment-page-1/#comment-399796</link>
		<dc:creator>http://kathyravan.blogspot.com/</dc:creator>
		<pubDate>Tue, 02 Dec 2008 18:19:42 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2007/09/28/using-view-to-reduce-number-of-tables-used/#comment-399796</guid>
		<description>A brief description about Blackhole - MySQL Storage Engine

http://kathyravan.blogspot.com/2008/12/blackhole-mysql-storage-engine.html</description>
		<content:encoded><![CDATA[<p>A brief description about Blackhole &#8211; MySQL Storage Engine</p>
<p><a href="http://kathyravan.blogspot.com/2008/12/blackhole-mysql-storage-engine.html" rel="nofollow">http://kathyravan.blogspot.com/2008/12/blackhole-mysql-storage-engine.html</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: peter</title>
		<link>http://www.mysqlperformanceblog.com/2007/09/28/using-view-to-reduce-number-of-tables-used/comment-page-1/#comment-175011</link>
		<dc:creator>peter</dc:creator>
		<pubDate>Fri, 05 Oct 2007 10:33:54 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2007/09/28/using-view-to-reduce-number-of-tables-used/#comment-175011</guid>
		<description>Thanks Alexey, 

This is indeed good idea and a bit different approach - instead of using different set of tables you patch application a bit so it can use same &quot;table&quot; for  multiple users at the same time. 

and indeed such views can be used for Inserts unless you do not have columns specified.</description>
		<content:encoded><![CDATA[<p>Thanks Alexey, </p>
<p>This is indeed good idea and a bit different approach &#8211; instead of using different set of tables you patch application a bit so it can use same &#8220;table&#8221; for  multiple users at the same time. </p>
<p>and indeed such views can be used for Inserts unless you do not have columns specified.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Olexandr Melnyk</title>
		<link>http://www.mysqlperformanceblog.com/2007/09/28/using-view-to-reduce-number-of-tables-used/comment-page-1/#comment-175008</link>
		<dc:creator>Olexandr Melnyk</dc:creator>
		<pubDate>Fri, 05 Oct 2007 10:01:26 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2007/09/28/using-view-to-reduce-number-of-tables-used/#comment-175008</guid>
		<description>peter,

what about using session variables to achive the effect with inserts and reduce the number of views?

At the start of the script you do:

[CODE]SET @owner_id = 12345[/CODE]

Then use a BEFORE INSERT trigger for the shared table (owner_id field should be in the view as well):

[CODE]CREATE TRIGGER post_insert BEFORE INSERT ON posts AS
  FOR EACH ROW BEGIN
    NEW.owner_id = COALESCE(NEW.owner_id, @owner_id);
  END;[/CODE]

Also, you don&#039;t even need an own set of views for each user. You just need a one set of views. Think of doing:

[CODE]CREATE VIEW owner_posts AS
SELECT * FROM posts
WHERE owner_id = @owner_id;[/CODE]

But, yes, variables can&#039;t be used inside a view definition. I have [URL=http://omelnyk.net/blog/2007/06/18/using-views-with-variables-in-mysql/]outlined[/URL] a simple workaround for that. Basically, create a function:

[CODE]CREATE FUNCTION owner_id
RETURNS INTEGER AS
RETURN @owner_id;[/CODE]

That way, you get two benefits:

- Most of INSERTs inside an application can remain unchanged. INSERTS that have to be changed are INSERTs without explicitly specified field list. But it is not the case, except for maybe several exceptions, for the applications you have mentioned above, as they have been designed to allow possible table structure extensions;
- You don&#039;t have to use a huge set of views, one set of views for all users would work just fine;</description>
		<content:encoded><![CDATA[<p>peter,</p>
<p>what about using session variables to achive the effect with inserts and reduce the number of views?</p>
<p>At the start of the script you do:</p>
<p>[CODE]SET @owner_id = 12345[/CODE]</p>
<p>Then use a BEFORE INSERT trigger for the shared table (owner_id field should be in the view as well):</p>
<p>[CODE]CREATE TRIGGER post_insert BEFORE INSERT ON posts AS<br />
  FOR EACH ROW BEGIN<br />
    NEW.owner_id = COALESCE(NEW.owner_id, @owner_id);<br />
  END;[/CODE]</p>
<p>Also, you don&#8217;t even need an own set of views for each user. You just need a one set of views. Think of doing:</p>
<p>[CODE]CREATE VIEW owner_posts AS<br />
SELECT * FROM posts<br />
WHERE owner_id = @owner_id;[/CODE]</p>
<p>But, yes, variables can&#8217;t be used inside a view definition. I have [URL=http://omelnyk.net/blog/2007/06/18/using-views-with-variables-in-mysql/]outlined[/URL] a simple workaround for that. Basically, create a function:</p>
<p>[CODE]CREATE FUNCTION owner_id<br />
RETURNS INTEGER AS<br />
RETURN @owner_id;[/CODE]</p>
<p>That way, you get two benefits:</p>
<p>- Most of INSERTs inside an application can remain unchanged. INSERTS that have to be changed are INSERTs without explicitly specified field list. But it is not the case, except for maybe several exceptions, for the applications you have mentioned above, as they have been designed to allow possible table structure extensions;<br />
- You don&#8217;t have to use a huge set of views, one set of views for all users would work just fine;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: peter</title>
		<link>http://www.mysqlperformanceblog.com/2007/09/28/using-view-to-reduce-number-of-tables-used/comment-page-1/#comment-174528</link>
		<dc:creator>peter</dc:creator>
		<pubDate>Wed, 03 Oct 2007 08:14:26 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2007/09/28/using-view-to-reduce-number-of-tables-used/#comment-174528</guid>
		<description>Dormando,

If it is create the test case and submit bug to MySQL :)
I expect it is something different.   

I did access all views created in my test.</description>
		<content:encoded><![CDATA[<p>Dormando,</p>
<p>If it is create the test case and submit bug to MySQL <img src='http://www.mysqlperformanceblog.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /><br />
I expect it is something different.   </p>
<p>I did access all views created in my test.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: dormando</title>
		<link>http://www.mysqlperformanceblog.com/2007/09/28/using-view-to-reduce-number-of-tables-used/comment-page-1/#comment-173607</link>
		<dc:creator>dormando</dc:creator>
		<pubDate>Sat, 29 Sep 2007 22:02:27 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2007/09/28/using-view-to-reduce-number-of-tables-used/#comment-173607</guid>
		<description>Can&#039;t get an apples to apples here... I&#039;ll ask...

There was a ratio of 100 views per innodb table. The system _used_ to be 40,000+ innodb and/or myisam tables, which then changed to 80,000+ views then a few hundred innodb tables. The views used 600 megabytes over that bit.

It would bloat as the views were first accessed, then the memory never went down. Temp tables/files?</description>
		<content:encoded><![CDATA[<p>Can&#8217;t get an apples to apples here&#8230; I&#8217;ll ask&#8230;</p>
<p>There was a ratio of 100 views per innodb table. The system _used_ to be 40,000+ innodb and/or myisam tables, which then changed to 80,000+ views then a few hundred innodb tables. The views used 600 megabytes over that bit.</p>
<p>It would bloat as the views were first accessed, then the memory never went down. Temp tables/files?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: peter</title>
		<link>http://www.mysqlperformanceblog.com/2007/09/28/using-view-to-reduce-number-of-tables-used/comment-page-1/#comment-173494</link>
		<dc:creator>peter</dc:creator>
		<pubDate>Sat, 29 Sep 2007 08:08:13 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2007/09/28/using-view-to-reduce-number-of-tables-used/#comment-173494</guid>
		<description>Dormando,

Also I just checked by creating 500.000 views - and using all of them - there does not seems to be any memory hog related to that. 

May be besides views you had Innodb tables ?  These consume memory if created in numbers.</description>
		<content:encoded><![CDATA[<p>Dormando,</p>
<p>Also I just checked by creating 500.000 views &#8211; and using all of them &#8211; there does not seems to be any memory hog related to that. </p>
<p>May be besides views you had Innodb tables ?  These consume memory if created in numbers.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: peter</title>
		<link>http://www.mysqlperformanceblog.com/2007/09/28/using-view-to-reduce-number-of-tables-used/comment-page-1/#comment-173492</link>
		<dc:creator>peter</dc:creator>
		<pubDate>Sat, 29 Sep 2007 07:58:47 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2007/09/28/using-view-to-reduce-number-of-tables-used/#comment-173492</guid>
		<description>Oh yes. You need to forget about a lot of data in INFORMATION SCHEMA if you have a lot of objects if it is table or views.</description>
		<content:encoded><![CDATA[<p>Oh yes. You need to forget about a lot of data in INFORMATION SCHEMA if you have a lot of objects if it is table or views.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: dormando</title>
		<link>http://www.mysqlperformanceblog.com/2007/09/28/using-view-to-reduce-number-of-tables-used/comment-page-1/#comment-173489</link>
		<dc:creator>dormando</dc:creator>
		<pubDate>Sat, 29 Sep 2007 07:27:28 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2007/09/28/using-view-to-reduce-number-of-tables-used/#comment-173489</guid>
		<description>I like the trigger idea for writebacks to the base table!

A developer tried this one a development machine I managed a few months ago. We ended up with over 80,000 views in a single instance and MySQL was not exactly happy. It soaked an extra 600 megabytes without having them in use, and querying the INFORMATION_SCHEMA at all became uselessly slow. The DB was not under high query load, but each developer had an &quot;instance&quot; of several thousand views emulating production, with sometimes several instances.

If I had more time I&#039;d test these methods, wondering if they hammer MySQL less hard?</description>
		<content:encoded><![CDATA[<p>I like the trigger idea for writebacks to the base table!</p>
<p>A developer tried this one a development machine I managed a few months ago. We ended up with over 80,000 views in a single instance and MySQL was not exactly happy. It soaked an extra 600 megabytes without having them in use, and querying the INFORMATION_SCHEMA at all became uselessly slow. The DB was not under high query load, but each developer had an &#8220;instance&#8221; of several thousand views emulating production, with sometimes several instances.</p>
<p>If I had more time I&#8217;d test these methods, wondering if they hammer MySQL less hard?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: peter</title>
		<link>http://www.mysqlperformanceblog.com/2007/09/28/using-view-to-reduce-number-of-tables-used/comment-page-1/#comment-173485</link>
		<dc:creator>peter</dc:creator>
		<pubDate>Sat, 29 Sep 2007 07:01:38 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2007/09/28/using-view-to-reduce-number-of-tables-used/#comment-173485</guid>
		<description>Jeffrey,

For simple views like this indexes will be used just fine because simply  where clause will be appended to all the queries you run on this view using &quot;merge&quot; algorithm.

Indeed I did not note about indexes - if you go with such solution you also redo indexes on the base table to include user_id as a prefix as it is now always specified in your queries.</description>
		<content:encoded><![CDATA[<p>Jeffrey,</p>
<p>For simple views like this indexes will be used just fine because simply  where clause will be appended to all the queries you run on this view using &#8220;merge&#8221; algorithm.</p>
<p>Indeed I did not note about indexes &#8211; if you go with such solution you also redo indexes on the base table to include user_id as a prefix as it is now always specified in your queries.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: peter</title>
		<link>http://www.mysqlperformanceblog.com/2007/09/28/using-view-to-reduce-number-of-tables-used/comment-page-1/#comment-173484</link>
		<dc:creator>peter</dc:creator>
		<pubDate>Sat, 29 Sep 2007 06:59:51 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2007/09/28/using-view-to-reduce-number-of-tables-used/#comment-173484</guid>
		<description>Pabloj -  There are a lot of these applications both open source and in house, just written for few users but used at massive scale. 

WordPress is good example.  basically any forum applications - PHPBB etc, if you&#039;re to run isolated installations.</description>
		<content:encoded><![CDATA[<p>Pabloj &#8211;  There are a lot of these applications both open source and in house, just written for few users but used at massive scale. </p>
<p>WordPress is good example.  basically any forum applications &#8211; PHPBB etc, if you&#8217;re to run isolated installations.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
