<?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>Web side of life &#187; mysql</title>
	<atom:link href="http://blog.ivanjovanovic.com/topic/mysql/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.ivanjovanovic.com</link>
	<description>by Ivan Jovanovic</description>
	<lastBuildDate>Tue, 13 Sep 2011 14:17:27 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Preserving ordering with WHERE IN() clause in MySQL</title>
		<link>http://blog.ivanjovanovic.com/2008/04/01/preserving-ordering-with-where-in-clause-in-mysql/</link>
		<comments>http://blog.ivanjovanovic.com/2008/04/01/preserving-ordering-with-where-in-clause-in-mysql/#comments</comments>
		<pubDate>Tue, 01 Apr 2008 23:42:56 +0000</pubDate>
		<dc:creator>Ivan Jovanovic</dc:creator>
				<category><![CDATA[mysql]]></category>

		<guid isPermaLink="false">http://ivan/?p=19</guid>
		<description><![CDATA[Today I ran into the code that pulls from database list of elements with SELECT * FROM table WHERE id IN(2,5,3,16,22,56,48) where order of ids is important to be preserved after results found. When using SELECT this way you will get the results for every id but ordered by id in ascending order. I was [...]]]></description>
			<content:encoded><![CDATA[<p>Today I ran into the code that pulls from database list of elements with <code>SELECT * FROM table WHERE id IN(2,5,3,16,22,56,48)</code> where order of ids is important to be preserved after results found. When using SELECT this way you will get the results for every id but ordered by id in ascending order. I was looking around and found neat solution to make MySQL work for you to preserve the order of returned results. With ORDER BY FIELD clause like here <strong>SELECT * FROM table WHERE id IN (1,5,2,13,4) ORDER BY FIELD(id, 1,5,2,13,4)</strong> you can keep the initial ordering.</p>
<p>What is the catch. This way <strong>id</strong> field can not be used as indexed. Therefore, <strong>EXPLAIN SELECT * FROM table WHERE id IN (1,5,2,13,4) ORDER BY FIELD(id, 1,5,2,13,4)</strong> would give you the information that it is using <strong>filesort</strong> for results ordering which is not so nice. In general, when you have <strong>id</strong>s as the array with the ordering you need, it probably needs less resources to reorganize them in memory instead of letting MySQL doing it with <strong>filesort.</strong></p>
<p>I&#8217;ll take some banchmarking tests on this to see about the concrete numbers that confirm or negate my presumption.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.ivanjovanovic.com/2008/04/01/preserving-ordering-with-where-in-clause-in-mysql/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

