<?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>Optimization &#8211; Database Research &amp; Development</title>
	<atom:link href="https://www.dbrnd.com/tag/optimization/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.dbrnd.com</link>
	<description>BIGData &#124; NoSQL &#124; MSSQL &#124; MySQL &#124; PostgreSQL</description>
	<lastBuildDate>Thu, 23 Nov 2017 12:54:16 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.0.6</generator>
	<item>
		<title>PostgreSQL: Create Index using ORDER BY (ASC/DESC)</title>
		<link>https://www.dbrnd.com/2017/12/postgresql-create-index-using-order-by-ascdesc/</link>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Sat, 16 Dec 2017 16:16:31 +0000</pubDate>
				<category><![CDATA[PostgreSQL]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[dbrnd]]></category>
		<category><![CDATA[index]]></category>
		<category><![CDATA[index order by]]></category>
		<category><![CDATA[Optimization]]></category>
		<category><![CDATA[plpgsql]]></category>
		<category><![CDATA[Postgres Query]]></category>
		<category><![CDATA[postgresql]]></category>
		<category><![CDATA[PostgreSQL Administrator]]></category>
		<category><![CDATA[PostgreSQL Error]]></category>
		<category><![CDATA[PostgreSQL Monitoring]]></category>
		<category><![CDATA[PostgreSQL Performance Tuning]]></category>
		<category><![CDATA[PostgreSQL Programming]]></category>
		<category><![CDATA[PostgreSQL Tips and Tricks]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=6816</guid>

					<description><![CDATA[Recently, I received a message like &#8220;ORDER BY clause is not working in PostgreSQL Index&#8221;. One of the users created an index with ORDER BY DESC and whenever he was selecting data at that time Query optimizer was not using INDEX SCAN ONLY. So here, I demonstrate and validate this: If we know in which [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F12%2Fpostgresql-create-index-using-order-by-ascdesc%2F&amp;linkname=PostgreSQL%3A%20Create%20Index%20using%20ORDER%20BY%20%28ASC%2FDESC%29" title="Facebook" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_twitter" href="https://www.addtoany.com/add_to/twitter?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F12%2Fpostgresql-create-index-using-order-by-ascdesc%2F&amp;linkname=PostgreSQL%3A%20Create%20Index%20using%20ORDER%20BY%20%28ASC%2FDESC%29" title="Twitter" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_linkedin" href="https://www.addtoany.com/add_to/linkedin?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F12%2Fpostgresql-create-index-using-order-by-ascdesc%2F&amp;linkname=PostgreSQL%3A%20Create%20Index%20using%20ORDER%20BY%20%28ASC%2FDESC%29" title="LinkedIn" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pinterest" href="https://www.addtoany.com/add_to/pinterest?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F12%2Fpostgresql-create-index-using-order-by-ascdesc%2F&amp;linkname=PostgreSQL%3A%20Create%20Index%20using%20ORDER%20BY%20%28ASC%2FDESC%29" title="Pinterest" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_reddit" href="https://www.addtoany.com/add_to/reddit?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F12%2Fpostgresql-create-index-using-order-by-ascdesc%2F&amp;linkname=PostgreSQL%3A%20Create%20Index%20using%20ORDER%20BY%20%28ASC%2FDESC%29" title="Reddit" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pocket" href="https://www.addtoany.com/add_to/pocket?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F12%2Fpostgresql-create-index-using-order-by-ascdesc%2F&amp;linkname=PostgreSQL%3A%20Create%20Index%20using%20ORDER%20BY%20%28ASC%2FDESC%29" title="Pocket" rel="nofollow noopener" target="_blank"></a><a class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F12%2Fpostgresql-create-index-using-order-by-ascdesc%2F&#038;title=PostgreSQL%3A%20Create%20Index%20using%20ORDER%20BY%20%28ASC%2FDESC%29" data-a2a-url="https://www.dbrnd.com/2017/12/postgresql-create-index-using-order-by-ascdesc/" data-a2a-title="PostgreSQL: Create Index using ORDER BY (ASC/DESC)"></a></p><p>Recently, I received a message like &#8220;ORDER BY clause is not working in PostgreSQL Index&#8221;.</p>
<p>One of the users created an index with ORDER BY DESC and whenever he was selecting data at that time Query optimizer was not using INDEX SCAN ONLY.</p>
<p><strong>So here, I demonstrate and validate this: </strong></p>
<blockquote><p>
If we know in which order we are going to SELECT data, we should apply for proper ORDER BY ASC/DESC in the index.</p></blockquote>
<p><strong>Please check the below full demonstration, and then validate your self:</strong></p>
<p><strong>Create a table with sample records:</strong></p>
<pre class="crayon-plain-tag">CREATE TABLE tbl_testIndex(a INTEGER, b INTEGER);

INSERT INTO tbl_testIndex
SELECT x, x FROM generate_series(1, 5000) AS f(x);</pre> </p>
<p><strong>Create a sample index without any ORDER BY:</strong></p>
<pre class="crayon-plain-tag">CREATE INDEX tbl_testIndex_idx ON tbl_testIndex (a, b);</pre> </p>
<p><strong>Check the execution plan:</strong><br />
Index only scan is working&#8230;</p>
<pre class="crayon-plain-tag">EXPLAIN 
SELECT *FROM tbl_testIndex
ORDER BY a,b;

'Index Only Scan using 
tbl_testindex_idx on tbl_testindex  
(cost=0.28..194.16 rows=5000 width=8)'</pre> </p>
<p><strong>Check the below execution plan:</strong><br />
Sequential Scan of the index which is not good.<br />
Because the index is not available for ORDER BY b DESC. </p>
<pre class="crayon-plain-tag">EXPLAIN 
SELECT *FROM tbl_testIndex
ORDER BY a,b DESC;

'Sort  (cost=380.19..392.69 rows=5000 width=8)'
'  Sort Key: a, b DESC'
'  -&gt;  Seq Scan on tbl_testindex  
(cost=0.00..73.00 rows=5000 width=8)'</pre> </p>
<p><strong>Now, drop the old index:</strong></p>
<pre class="crayon-plain-tag">DROP INDEX tbl_testIndex_idx;</pre> </p>
<p><strong>Create a new index with b DESC:</strong></p>
<pre class="crayon-plain-tag">CREATE INDEX tbl_testIndex_idx ON tbl_testIndex (a, b DESC);</pre> </p>
<p><strong>Check the below execution plan:</strong><br />
Index scan is working&#8230;</p>
<pre class="crayon-plain-tag">EXPLAIN 
SELECT *FROM tbl_testIndex
ORDER BY a,b DESC;

'Index Only Scan using 
tbl_testindex_idx on tbl_testindex  
(cost=0.28..194.16 rows=5000 width=8)'</pre> </p>
<p><strong>Check the below execution plans:</strong><br />
You can find sequential scan of index because we are not using only ORDER BY b DESC</p>
<pre class="crayon-plain-tag">EXPLAIN 
SELECT *FROM tbl_testIndex
ORDER BY a,b;

EXPLAIN 
SELECT *FROM tbl_testIndex
ORDER BY a DESC,b DESC;

'Sort  (cost=380.19..392.69 rows=5000 width=8)'
'  Sort Key: a DESC, b DESC'
'  -&gt;  Seq Scan on tbl_testindex  
(cost=0.00..73.00 rows=5000 width=8)'</pre> </p>
<p><strong>Now, check the below execution plan:</strong><br />
You can find like Index scan backward, where we gave ORDER By a DESC and internally, it is using backward index scan.</p>
<pre class="crayon-plain-tag">EXPLAIN 
SELECT *FROM tbl_testIndex
ORDER BY a DESC,b;

'Index Only Scan Backward using 
tbl_testindex_idx on tbl_testindex  
(cost=0.28..194.16 rows=5000 width=8)'</pre> </p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
