<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Everything MySQL</title>
	<atom:link href="http://everythingmysql.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://everythingmysql.wordpress.com</link>
	<description>Everything related to and concerning MySQL</description>
	<lastBuildDate>Mon, 13 Oct 2008 08:15:27 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='everythingmysql.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Everything MySQL</title>
		<link>http://everythingmysql.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://everythingmysql.wordpress.com/osd.xml" title="Everything MySQL" />
	<atom:link rel='hub' href='http://everythingmysql.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Using Multiple Sequences in a Table</title>
		<link>http://everythingmysql.wordpress.com/2008/09/17/using-multiple-sequences-in-a-table/</link>
		<comments>http://everythingmysql.wordpress.com/2008/09/17/using-multiple-sequences-in-a-table/#comments</comments>
		<pubDate>Tue, 16 Sep 2008 18:32:15 +0000</pubDate>
		<dc:creator>Rishi Agarwal</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[auto_increment]]></category>
		<category><![CDATA[sequences]]></category>

		<guid isPermaLink="false">http://everythingmysql.wordpress.com/?p=11</guid>
		<description><![CDATA[Recently, I wanted to use multiple sequences in a table.  I wanted to generate a new sequence for every event for the id of its registrants. The table structure was something like this : CREATE TABLE registrants ( event_id INT NOT NULL, id INT AUTO_INCREMENT NOT NULL, name VARCHAR(100), email VARCHAR(100), address VARCHAR(100), PRIMARY KEY [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=everythingmysql.wordpress.com&amp;blog=3990911&amp;post=11&amp;subd=everythingmysql&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Recently, I wanted to use multiple sequences in a table.  I wanted to generate a new sequence for every event for the id of its registrants. The table structure was something like this :</p>
<blockquote><p>CREATE TABLE registrants (<br />
event_id INT NOT NULL,<br />
id INT AUTO_INCREMENT NOT NULL,<br />
name VARCHAR(100),<br />
email VARCHAR(100),<br />
address VARCHAR(100),<br />
PRIMARY KEY ( event_id, id )<br />
) ;</p></blockquote>
<p>But I ran into a problem. This works only for MyISAM and BDB tables. You can use AUTO_INCREMENT on a secondary column in a multiple-column index only for MyISAM and BDB tables. BDB isn&#8217;t supported any longer from MySQL 5.1 and I didn&#8217;t want to use MyISAM as my whole schema is based on InnoDB engine and also for MyISAM having its own table-corruption issues.  After lot of trials, debates and finally referring Planet MySQL, I went ahead with this <a href="http://www.mysqlperformanceblog.com/2008/04/02/stored-function-to-generate-sequences" target="_blank">suggestion</a> by Peter from MySQL Performance Blog and some help from <a href="http://arjen-lentz.livejournal.com/34627.html">here</a>.</p>
<blockquote><p>CREATE TABLE sequences (<br />
event_id INT(10)  NOT NULL,<br />
val int(10)  NOT NULL,<br />
PRIMARY KEY  (event_id)<br />
) ENGINE=MyISAM DEFAULT CHARACTER SET = utf8  COLLATE = utf8_general_ci;</p></blockquote>
<blockquote><p>delimiter $$</p>
<p>CREATE FUNCTION generate_registrant_id(event_id INT)<br />
RETURNS INT<br />
BEGIN</p>
<p>INSERT INTO sequences VALUES (event_id, LAST_INSERT_ID(1))<br />
ON DUPLICATE KEY UPDATE val=LAST_INSERT_ID(val+1);</p>
<p>RETURN IF(LAST_INSERT_ID()=0, 1, LAST_INSERT_ID());</p>
<p>END$$</p>
<p>delimiter ;</p></blockquote>
<p>I removed AUTO_INCREMENT from the table and call this function to generate the id. The same could be used for maintaining any other sequences in your schema.</p>
<p>Community to the rescue&#8230;  again.</p>
<p>For more help refer : <a href="http://dev.mysql.com/doc/refman/5.0/en/example-auto-increment.html">Using AUTO_INCREMENT </a></p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/everythingmysql.wordpress.com/11/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/everythingmysql.wordpress.com/11/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/everythingmysql.wordpress.com/11/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/everythingmysql.wordpress.com/11/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/everythingmysql.wordpress.com/11/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/everythingmysql.wordpress.com/11/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/everythingmysql.wordpress.com/11/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/everythingmysql.wordpress.com/11/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/everythingmysql.wordpress.com/11/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/everythingmysql.wordpress.com/11/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/everythingmysql.wordpress.com/11/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/everythingmysql.wordpress.com/11/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/everythingmysql.wordpress.com/11/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/everythingmysql.wordpress.com/11/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/everythingmysql.wordpress.com/11/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/everythingmysql.wordpress.com/11/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=everythingmysql.wordpress.com&amp;blog=3990911&amp;post=11&amp;subd=everythingmysql&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://everythingmysql.wordpress.com/2008/09/17/using-multiple-sequences-in-a-table/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/2710ffaa6c1a0833743e2c9952b40801?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">agarwalrishi</media:title>
		</media:content>
	</item>
		<item>
		<title>&#8216;EXISTS&#8217; is better than &#8216;IN&#8217;</title>
		<link>http://everythingmysql.wordpress.com/2008/07/11/exists-is-better-than-in/</link>
		<comments>http://everythingmysql.wordpress.com/2008/07/11/exists-is-better-than-in/#comments</comments>
		<pubDate>Fri, 11 Jul 2008 13:55:47 +0000</pubDate>
		<dc:creator>Rishi Agarwal</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[query  optimization]]></category>

		<guid isPermaLink="false">http://everythingmysql.wordpress.com/?p=7</guid>
		<description><![CDATA[While working on my last project, I was facing a query which was taking 30 second to execute. The number of tables involved were 10 and total records were around 20k.  It was a search functionality for a web-app project.  There were lot of sub-queries involved in the first draft, all of them using &#8216;IN&#8217;. [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=everythingmysql.wordpress.com&amp;blog=3990911&amp;post=7&amp;subd=everythingmysql&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>While working on my last project, I was facing a query which was taking 30 second to execute. The number of tables involved were 10 and total records were around 20k.  It was a search functionality for a web-app project.  There were lot of sub-queries involved in the first draft, all of them using &#8216;IN&#8217;. After doing some research ( within MySQL Community ) , I just replaced all &#8216;IN&#8217;  by &#8216;EXISTS&#8217; and voila !  The query took 11 seconds to execute.</p>
<p>For example, you could change this query  :</p>
<blockquote><p>SELECT *</p>
<p>FROM invitees i</p>
<p>WHERE i.event_id <strong>IN</strong> ( SELECT e.id</p>
<p>FROM event e</p>
<p>WHERE e.city <span style="text-decoration:line-through;">LIKE</span> = &#8216;ABC&#8217; )</p></blockquote>
<p>TO</p>
<blockquote><p>SELECT *</p>
<p>FROM invitees i</p>
<p>WHERE <strong>EXISTS</strong> ( SELECT 1</p>
<p>FROM event e</p>
<p>WHERE e.city <span style="text-decoration:line-through;">LIKE</span> = &#8216;ABC&#8217;</p>
<p>AND e.id = i.event_id ).</p></blockquote>
<p>Try it and let me know if you find the same difference in performance as I have. There were many more modifications  to the original query to bring it further down to 1 sec. But I will talk about that later.  For more information on  EXISTS and IN Sub Queries see Reference below.</p>
<p>References :</p>
<p><a href="http://dev.mysql.com/doc/refman/5.0/en/in-subquery-optimization.html" target="_blank">http://dev.mysql.com/doc/refman/5.0/en/optimizing-subqueries.html</a></p>
<p><a href="http://dev.mysql.com/doc/refman/5.0/en/in-subquery-optimization.html">http://dev.mysql.com/doc/refman/5.0/en/in-subquery-optimization.html</a></p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/everythingmysql.wordpress.com/7/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/everythingmysql.wordpress.com/7/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/everythingmysql.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/everythingmysql.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/everythingmysql.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/everythingmysql.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/everythingmysql.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/everythingmysql.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/everythingmysql.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/everythingmysql.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/everythingmysql.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/everythingmysql.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/everythingmysql.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/everythingmysql.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/everythingmysql.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/everythingmysql.wordpress.com/7/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=everythingmysql.wordpress.com&amp;blog=3990911&amp;post=7&amp;subd=everythingmysql&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://everythingmysql.wordpress.com/2008/07/11/exists-is-better-than-in/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/2710ffaa6c1a0833743e2c9952b40801?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">agarwalrishi</media:title>
		</media:content>
	</item>
		<item>
		<title>Use ANSI Style for MySQL</title>
		<link>http://everythingmysql.wordpress.com/2008/07/05/use-ansi-style-for-mysql/</link>
		<comments>http://everythingmysql.wordpress.com/2008/07/05/use-ansi-style-for-mysql/#comments</comments>
		<pubDate>Sat, 05 Jul 2008 09:10:44 +0000</pubDate>
		<dc:creator>Rishi Agarwal</dc:creator>
				<category><![CDATA[Coding Practice]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[joins]]></category>
		<category><![CDATA[style]]></category>

		<guid isPermaLink="false">http://everythingmysql.wordpress.com/?p=4</guid>
		<description><![CDATA[I prefer to use ANSI style for writing MySQL queries.  When I started working on SQL, I used the old style aka Theta style for all my queries. Example of a query in  Theta style : SELECT  e.title, i.first_name, i.response FROM invitees i, events e WHERE e.id = i.event_id AND e.date = &#8217;2008-07-04&#8242;; which , [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=everythingmysql.wordpress.com&amp;blog=3990911&amp;post=4&amp;subd=everythingmysql&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:left;">I prefer to use ANSI style for writing MySQL queries.  When I started working on SQL, I used the old style aka Theta style for all my queries. Example of a query in  Theta style :</p>
<blockquote>
<p style="text-align:left;">SELECT  e.title, i.first_name, i.response</p>
<p style="text-align:left;">FROM invitees i,</p>
<p style="text-align:left;">events e</p>
<p style="text-align:left;">WHERE e.id = i.event_id</p>
<p style="text-align:left;">AND e.date = &#8217;2008-07-04&#8242;;</p>
</blockquote>
<p>which , in ANSI style is equivalent to :</p>
<blockquote><p>SELECT e.title, i.name, i.response</p>
<p>FROM invitees i</p>
<p>INNER JOIN events e on e.id = i.event_id</p>
<p>WHERE e.date = &#8217;2008-07-04&#8242;</p></blockquote>
<p>Many developers prefer using the old style. But believe me, ANSI style is lot better and advantageous.</p>
<p>First, MySQL  supports OUTER JOINs in ANSI style. Second, ANSI style forces you to think about your joins  clearly. Your code is lot more readable in ANSI style. For example, all the joins go in the FROM clause, and the WHERE clause is left with only statements which filter out the result set of the tables in the join.  You get a clear picture of how the tables are linked with each other.</p>
<p>For more information on Joins, see Reference below.</p>
<p>Reference : <a href="http://dev.mysql.com/doc/refman/5.0/en/join.html" target="_blank">Join Syntax in MySQL </a></p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/everythingmysql.wordpress.com/4/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/everythingmysql.wordpress.com/4/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/everythingmysql.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/everythingmysql.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/everythingmysql.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/everythingmysql.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/everythingmysql.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/everythingmysql.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/everythingmysql.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/everythingmysql.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/everythingmysql.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/everythingmysql.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/everythingmysql.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/everythingmysql.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/everythingmysql.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/everythingmysql.wordpress.com/4/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=everythingmysql.wordpress.com&amp;blog=3990911&amp;post=4&amp;subd=everythingmysql&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://everythingmysql.wordpress.com/2008/07/05/use-ansi-style-for-mysql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/2710ffaa6c1a0833743e2c9952b40801?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">agarwalrishi</media:title>
		</media:content>
	</item>
	</channel>
</rss>
