<?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>Index Performance &#8211; Database Research &amp; Development</title>
	<atom:link href="https://www.dbrnd.com/tag/index-performance/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>Tue, 16 Jan 2018 10:51:12 +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>SQL Server: Script to find Unused Indexes of Database</title>
		<link>https://www.dbrnd.com/2018/01/sql-server-script-to-find-unused-indexes-of-database/</link>
					<comments>https://www.dbrnd.com/2018/01/sql-server-script-to-find-unused-indexes-of-database/#comments</comments>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Tue, 30 Jan 2018 16:16:30 +0000</pubDate>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Server DBA Script]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[dbrnd]]></category>
		<category><![CDATA[duplicate index]]></category>
		<category><![CDATA[index fragmentation]]></category>
		<category><![CDATA[Index Performance]]></category>
		<category><![CDATA[index usage]]></category>
		<category><![CDATA[indexes]]></category>
		<category><![CDATA[missing index]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Server Administrator]]></category>
		<category><![CDATA[SQL Server Monitoring]]></category>
		<category><![CDATA[SQL Server Performance Tunning]]></category>
		<category><![CDATA[SQL Server Tips and Tricks]]></category>
		<category><![CDATA[TSQL]]></category>
		<category><![CDATA[unused indexes]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=4081</guid>

					<description><![CDATA[As I am preparing important scripts for SQL Server DBA, so here I am also sharing one more vital script for finding the unused indexes of SQL Server Database. The management and maintenance of database index is a day to day exercise for a Database Administrator. The wrong or unused index can create performance issues [&#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%2F2018%2F01%2Fsql-server-script-to-find-unused-indexes-of-database%2F&amp;linkname=SQL%20Server%3A%20Script%20to%20find%20Unused%20Indexes%20of%20Database" 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%2F2018%2F01%2Fsql-server-script-to-find-unused-indexes-of-database%2F&amp;linkname=SQL%20Server%3A%20Script%20to%20find%20Unused%20Indexes%20of%20Database" 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%2F2018%2F01%2Fsql-server-script-to-find-unused-indexes-of-database%2F&amp;linkname=SQL%20Server%3A%20Script%20to%20find%20Unused%20Indexes%20of%20Database" 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%2F2018%2F01%2Fsql-server-script-to-find-unused-indexes-of-database%2F&amp;linkname=SQL%20Server%3A%20Script%20to%20find%20Unused%20Indexes%20of%20Database" 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%2F2018%2F01%2Fsql-server-script-to-find-unused-indexes-of-database%2F&amp;linkname=SQL%20Server%3A%20Script%20to%20find%20Unused%20Indexes%20of%20Database" 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%2F2018%2F01%2Fsql-server-script-to-find-unused-indexes-of-database%2F&amp;linkname=SQL%20Server%3A%20Script%20to%20find%20Unused%20Indexes%20of%20Database" 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%2F2018%2F01%2Fsql-server-script-to-find-unused-indexes-of-database%2F&#038;title=SQL%20Server%3A%20Script%20to%20find%20Unused%20Indexes%20of%20Database" data-a2a-url="https://www.dbrnd.com/2018/01/sql-server-script-to-find-unused-indexes-of-database/" data-a2a-title="SQL Server: Script to find Unused Indexes of Database"></a></p><p>As I am preparing important scripts for SQL Server DBA, so here I am also sharing one more vital script for finding the unused indexes of SQL Server Database.</p>
<blockquote><p>The management and maintenance of database index is a day to day exercise for a Database Administrator. The wrong or unused index can create performance issues for a frequently used table.
</p></blockquote>
<blockquote><p>At every insert and update, the data of an index are also changing, and it requires some IO operations. Better to find the unused index and delete it.<br />
I also added &#8220;DropStatement&#8221; column in script for dropping unused Indexes.</p></blockquote>
<pre class="crayon-plain-tag">SELECT 
	OBJECT_NAME(i.OBJECT_ID) AS ObjectName
	,i.name AS UnusedIndexName	
	,8 * SUM(au.used_pages) AS IndexSizeInKB
	,CASE
		WHEN i.type = 0 THEN 'Heap'  
		WHEN i.type= 1 THEN 'Clustered' 
		WHEN i.type=2 THEN 'Non-Clustered'   
		WHEN i.type=3 THEN 'XML'   
		WHEN i.type=4 THEN 'Spatial'  
		WHEN i.type=5 THEN 'Clustered columnstore index'   
		WHEN i.type=6 THEN 'Nonclustered columnstore index'  
		WHEN i.type=7 THEN 'Nonclustered hash index.'
	END index_type
	,'DROP INDEX ' + i.name + ' ON ' + OBJECT_NAME(i.OBJECT_ID) AS DropStatement
FROM sys.indexes AS i 
LEFT JOIN sys.dm_db_index_usage_stats AS dius 
	ON dius.OBJECT_ID = i.OBJECT_ID 
		AND i.index_id = dius.index_id 
		AND dius.database_id = DB_ID() 
INNER JOIN sys.partitions AS p 
	ON p.OBJECT_ID = i.OBJECT_ID 
		AND p.index_id = i.index_id 
INNER JOIN sys.allocation_units AS au 
	ON au.container_id = p.partition_id 
WHERE OBJECTPROPERTY(i.OBJECT_ID, 'IsIndexable') = 1 
	AND OBJECTPROPERTY(i.OBJECT_ID, 'IsIndexed') = 1 
	AND dius.index_id IS NULL
	OR (dius.user_updates &gt; 0 
		AND dius.user_seeks = 0 
		AND dius.user_scans = 0 
		AND dius.user_lookups = 0)
GROUP BY OBJECT_NAME(i.OBJECT_ID), i.name, i.type 
ORDER BY OBJECT_NAME(i.OBJECT_ID)</pre> </p>
<p><strong>Other SQL Server Index related DBA Scripts:</strong></p>
<blockquote class="wp-embedded-content" data-secret="S1i20bnCFt"><p><a href="https://www.dbrnd.com/2016/11/sql-server-script-to-find-redundant-and-duplicate-indexes/">SQL Server: Script to find Redundant and Duplicate Indexes</a></p></blockquote>
<p><iframe class="wp-embedded-content" sandbox="allow-scripts" security="restricted"  src="https://www.dbrnd.com/2016/11/sql-server-script-to-find-redundant-and-duplicate-indexes/embed/#?secret=S1i20bnCFt" data-secret="S1i20bnCFt" width="600" height="338" title="&#8220;SQL Server: Script to find Redundant and Duplicate Indexes&#8221; &#8212; Database Research &amp; Development" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe></p>
<blockquote class="wp-embedded-content" data-secret="ODjqcfmtkP"><p><a href="https://www.dbrnd.com/2016/03/sql-server-script-to-find-index-average-fragmentation-in-percentage/">SQL Server: Script to find Index Average Fragmentation in Percentage</a></p></blockquote>
<p><iframe class="wp-embedded-content" sandbox="allow-scripts" security="restricted"  src="https://www.dbrnd.com/2016/03/sql-server-script-to-find-index-average-fragmentation-in-percentage/embed/#?secret=ODjqcfmtkP" data-secret="ODjqcfmtkP" width="600" height="338" title="&#8220;SQL Server: Script to find Index Average Fragmentation in Percentage&#8221; &#8212; Database Research &amp; Development" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe></p>
<blockquote class="wp-embedded-content" data-secret="9sUF7xBYiV"><p><a href="https://www.dbrnd.com/2015/11/sql-server-script-to-find-missing-indexes/">SQL Server: Script to find Missing Indexes</a></p></blockquote>
<p><iframe class="wp-embedded-content" sandbox="allow-scripts" security="restricted"  src="https://www.dbrnd.com/2015/11/sql-server-script-to-find-missing-indexes/embed/#?secret=9sUF7xBYiV" data-secret="9sUF7xBYiV" width="600" height="338" title="&#8220;SQL Server: Script to find Missing Indexes&#8221; &#8212; Database Research &amp; Development" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe></p>
<blockquote class="wp-embedded-content" data-secret="LqPIXi45yO"><p><a href="https://www.dbrnd.com/2015/10/sql-server-script-to-find-index-operational-stats/">SQL Server: Script to find Index Operational Stats</a></p></blockquote>
<p><iframe loading="lazy" class="wp-embedded-content" sandbox="allow-scripts" security="restricted"  src="https://www.dbrnd.com/2015/10/sql-server-script-to-find-index-operational-stats/embed/#?secret=LqPIXi45yO" data-secret="LqPIXi45yO" width="600" height="338" title="&#8220;SQL Server: Script to find Index Operational Stats&#8221; &#8212; Database Research &amp; Development" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe></p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.dbrnd.com/2018/01/sql-server-script-to-find-unused-indexes-of-database/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
			</item>
		<item>
		<title>PostgreSQL: CLUSTER &#8211; Improve Index Performance (No default cluster index)</title>
		<link>https://www.dbrnd.com/2016/12/postgresql-cluster-improve-index-performance-no-default-cluster-index-explicit-lock-physical-order-data/</link>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Fri, 02 Dec 2016 04:22:42 +0000</pubDate>
				<category><![CDATA[PostgreSQL]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[CLUSTER]]></category>
		<category><![CDATA[Cluster Index]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[dbrnd]]></category>
		<category><![CDATA[explicit lock]]></category>
		<category><![CDATA[fragmentation]]></category>
		<category><![CDATA[Index Performance]]></category>
		<category><![CDATA[Order of data]]></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 Programming]]></category>
		<category><![CDATA[PostgreSQL Tips and Tricks]]></category>
		<category><![CDATA[Primary key]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=3964</guid>

					<description><![CDATA[I have received a few emails related to PostgreSQL default cluster index and I have received from the developers of Microsoft SQL Server. If you are Microsoft SQL Server developer, you know that Primary key of the Table is working as Cluster Index and resides in the same data page where data actually stored. In [&#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%2F2016%2F12%2Fpostgresql-cluster-improve-index-performance-no-default-cluster-index-explicit-lock-physical-order-data%2F&amp;linkname=PostgreSQL%3A%20CLUSTER%20%E2%80%93%20Improve%20Index%20Performance%20%28No%20default%20cluster%20index%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%2F2016%2F12%2Fpostgresql-cluster-improve-index-performance-no-default-cluster-index-explicit-lock-physical-order-data%2F&amp;linkname=PostgreSQL%3A%20CLUSTER%20%E2%80%93%20Improve%20Index%20Performance%20%28No%20default%20cluster%20index%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%2F2016%2F12%2Fpostgresql-cluster-improve-index-performance-no-default-cluster-index-explicit-lock-physical-order-data%2F&amp;linkname=PostgreSQL%3A%20CLUSTER%20%E2%80%93%20Improve%20Index%20Performance%20%28No%20default%20cluster%20index%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%2F2016%2F12%2Fpostgresql-cluster-improve-index-performance-no-default-cluster-index-explicit-lock-physical-order-data%2F&amp;linkname=PostgreSQL%3A%20CLUSTER%20%E2%80%93%20Improve%20Index%20Performance%20%28No%20default%20cluster%20index%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%2F2016%2F12%2Fpostgresql-cluster-improve-index-performance-no-default-cluster-index-explicit-lock-physical-order-data%2F&amp;linkname=PostgreSQL%3A%20CLUSTER%20%E2%80%93%20Improve%20Index%20Performance%20%28No%20default%20cluster%20index%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%2F2016%2F12%2Fpostgresql-cluster-improve-index-performance-no-default-cluster-index-explicit-lock-physical-order-data%2F&amp;linkname=PostgreSQL%3A%20CLUSTER%20%E2%80%93%20Improve%20Index%20Performance%20%28No%20default%20cluster%20index%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%2F2016%2F12%2Fpostgresql-cluster-improve-index-performance-no-default-cluster-index-explicit-lock-physical-order-data%2F&#038;title=PostgreSQL%3A%20CLUSTER%20%E2%80%93%20Improve%20Index%20Performance%20%28No%20default%20cluster%20index%29" data-a2a-url="https://www.dbrnd.com/2016/12/postgresql-cluster-improve-index-performance-no-default-cluster-index-explicit-lock-physical-order-data/" data-a2a-title="PostgreSQL: CLUSTER – Improve Index Performance (No default cluster index)"></a></p><p><em>I have received a few emails related to PostgreSQL default cluster index and I have received from the developers of Microsoft SQL Server.<br />
If you are Microsoft SQL Server developer, you know that Primary key of the Table is working as Cluster Index and resides in the same data page where data actually stored. </em></p>
<p><strong>In PostgreSQL, There is no concept like: Table Primary Key means default Cluster Index of that table. </strong><br />
<strong>In PostgreSQL, we have one CLUSTER command which is similar to Cluster Index.</strong> </p>
<blockquote><p>Once you create your table primary key or any other Index, you can execute the CLUSTER command by specifying that Index name to achieve the physical order of the Table Data. </p></blockquote>
<blockquote><p>CLUSTER instructs PostgreSQL to cluster the table specified by table_name based on the index specified by index_name. The index must already have been defined on table_name.</p></blockquote>
<blockquote><p>A clustering on an index forces the physical ordering of the data and you can have only one clustered index per table and should carefully pick which index you will use to cluster.</p></blockquote>
<blockquote><p>When a table is clustered, it is physically reordered based on the index information. Clustering is a one-time operation: when the table is subsequently updated, the changes are not clustered. That is, no attempt is made to store new or updated rows according to their index order. </p></blockquote>
<blockquote><p>If you wish, you can periodically recluster by issuing the CLUSTER command again and again. </p></blockquote>
<blockquote><p>A executing of CLUSTER command requires an ACCESS EXCLUSIVE lock on the table because It reorders your data. You can also get some free space by issuing CLUSTER because It rearranges the data in a particular order. </p></blockquote>
<blockquote><p>When you execute the CLUSTER command, It creates a temporary copy of the table therefore, you need free space on disk at least equal to the original table size. </p></blockquote>
<blockquote><p>When an index scan is used, a temporary copy of the table is created that contains the table data in the index order. Temporary copies of each index on the table are created as well. Therefore, you need free space on disk at least equal to the sum of the table size and the index sizes.</p></blockquote>
<blockquote><p>It is advisable to set maintenance_work_mem to a reasonably large value before clustering.</p></blockquote>
<blockquote><p>Once you are done with your CLUSTER, you should execute the ANALYZE command on that table because planner records statistics about the ordering of tables.
</p></blockquote>
<p><strong>Syntax of Cluster:</strong><br />
First time you must execute CLUSTER using the Index Name.</p>
<pre class="crayon-plain-tag">CLUSTER table_name USING index_name;</pre> </p>
<p><strong>Cluster the table:</strong><br />
Once you have executed CLUSTER with Index, next time you should execute only CLUSTER TABLE because It knows that which index already defined as CLUSTER.</p>
<pre class="crayon-plain-tag">CLUSTER table_name;</pre> </p>
<p><strong>Cluster all tables of database:</strong></p>
<pre class="crayon-plain-tag">CLUSTER;</pre> </p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>PostgreSQL: How we can create Index on Expression?</title>
		<link>https://www.dbrnd.com/2016/10/postgresql-how-we-can-create-index-on-expression-brin-btree-partial-index/</link>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Mon, 03 Oct 2016 16:16:58 +0000</pubDate>
				<category><![CDATA[PostgreSQL]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[BRIN Index]]></category>
		<category><![CDATA[Btree Index]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[dbrnd]]></category>
		<category><![CDATA[Expression Index]]></category>
		<category><![CDATA[Index Performance]]></category>
		<category><![CDATA[Partial Index]]></category>
		<category><![CDATA[plpgsql]]></category>
		<category><![CDATA[Postgres Query]]></category>
		<category><![CDATA[postgresql]]></category>
		<category><![CDATA[postgresql 9.5]]></category>
		<category><![CDATA[PostgreSQL Administrator]]></category>
		<category><![CDATA[PostgreSQL Error]]></category>
		<category><![CDATA[PostgreSQL Programming]]></category>
		<category><![CDATA[PostgreSQL Tips and Tricks]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=3744</guid>

					<description><![CDATA[If you are searching about, What is Expression Index, this is the one of the right article for you. We know about the different types of PostgreSQL Index. If you don&#8217;t about this, please visit below few links. What is BRIN (Block Range Index )? What is Partial Index? What is BTree Index? Most of [&#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%2F2016%2F10%2Fpostgresql-how-we-can-create-index-on-expression-brin-btree-partial-index%2F&amp;linkname=PostgreSQL%3A%20How%20we%20can%20create%20Index%20on%20Expression%3F" 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%2F2016%2F10%2Fpostgresql-how-we-can-create-index-on-expression-brin-btree-partial-index%2F&amp;linkname=PostgreSQL%3A%20How%20we%20can%20create%20Index%20on%20Expression%3F" 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%2F2016%2F10%2Fpostgresql-how-we-can-create-index-on-expression-brin-btree-partial-index%2F&amp;linkname=PostgreSQL%3A%20How%20we%20can%20create%20Index%20on%20Expression%3F" 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%2F2016%2F10%2Fpostgresql-how-we-can-create-index-on-expression-brin-btree-partial-index%2F&amp;linkname=PostgreSQL%3A%20How%20we%20can%20create%20Index%20on%20Expression%3F" 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%2F2016%2F10%2Fpostgresql-how-we-can-create-index-on-expression-brin-btree-partial-index%2F&amp;linkname=PostgreSQL%3A%20How%20we%20can%20create%20Index%20on%20Expression%3F" 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%2F2016%2F10%2Fpostgresql-how-we-can-create-index-on-expression-brin-btree-partial-index%2F&amp;linkname=PostgreSQL%3A%20How%20we%20can%20create%20Index%20on%20Expression%3F" 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%2F2016%2F10%2Fpostgresql-how-we-can-create-index-on-expression-brin-btree-partial-index%2F&#038;title=PostgreSQL%3A%20How%20we%20can%20create%20Index%20on%20Expression%3F" data-a2a-url="https://www.dbrnd.com/2016/10/postgresql-how-we-can-create-index-on-expression-brin-btree-partial-index/" data-a2a-title="PostgreSQL: How we can create Index on Expression?"></a></p><p>If you are searching about, What is Expression Index, this is the one of the right article for you. </p>
<p>We know about the different types of PostgreSQL Index.<br />
If you don&#8217;t about this, please visit below few links.</p>
<blockquote><p><a href="https://www.dbrnd.com/2016/08/postgresql-9-5-introduced-brin-block-range-index-with-detail-performance-test-report-on-6gb-table/" target="_blank"><strong>What is BRIN (Block Range Index )?</strong></a></p>
<p><a href="https://www.dbrnd.com/2016/08/postgresql-example-of-partial-unique-composite-index-to-avoid-duplicate-records/" target="_blank"><strong>What is Partial Index?</strong></a></p>
<p><a href="https://en.wikipedia.org/wiki/B-tree" target="_blank"><strong>What is BTree Index?</strong></a></p></blockquote>
<blockquote><p>Most of the Database Administrators or Developers are doing a common mistake,<br />
When you create any Index on Table Column, dose not mean that you can use the Indexed column in any expression and Index will work as per the expectation. </p></blockquote>
<blockquote><p>When you used Indexed column in any kind of expression, query planner simply skips the scanning of Indexes of that column. </p></blockquote>
<blockquote><p>For example,<br />
We have created one index on Date column and in WHERE are extracting days from this Date column.<br />
In this situation, the Index will not work on Date column and for such specific requirement, we have to create the Expression Index of PostgreSQL. </p></blockquote>
<p><strong>Let me demonstrate this.</strong></p>
<p><strong>First create one sample table:</strong></p>
<pre class="crayon-plain-tag">CREATE TABLE tbl_ItemTransactions 
(
	TranID SERIAL
	,TransactionDate TIMESTAMPTZ
	,TransactionName TEXT
);</pre> </p>
<p><strong>Generate sample data for testing the performance of Indexes:</strong></p>
<pre class="crayon-plain-tag">INSERT INTO tbl_ItemTransactions 
(TransactionDate, TransactionName)
SELECT x, 'dbrnd' 
FROM generate_series('2015-01-01 00:00:00'::timestamptz, '2016-08-01 00:00:00'::timestamptz,'2 seconds'::interval) a(x);</pre> </p>
<p><strong>Total inserted record count is 24969601:</strong></p>
<pre class="crayon-plain-tag">SELECT COUNT(1) FROM tbl_ItemTransactions;</pre> </p>
<p><strong>Now create index on TransactionDate column:</strong></p>
<pre class="crayon-plain-tag">CREATE INDEX idx_tbl_ItemTransactions_TransactionDate
ON tbl_ItemTransactions (TransactionDate);</pre> </p>
<p><strong>Lets see the plan of query with date filter:</strong></p>
<pre class="crayon-plain-tag">EXPLAIN ANALYZE
SELECT *FROM 
tbl_ItemTransactions 
WHERE TransactionDate BETWEEN '20150808' AND '20160108';
/*
"Index Scan using idx_tbl_itemtransactions_transactiondate on tbl_itemtransactions  
(cost=0.44..249472.54 rows=6681405 width=18) 
(actual time=61.558..17765.979 rows=6609601 loops=1)"
"Index Cond: ((transactiondate &gt;= '2015-08-08 00:00:00+05:30'::timestamp with time zone) AND 
(transactiondate &lt;= '2016-01-08 00:00:00+05:30'::timestamp with time zone))"
"Planning time: 87.751 ms"
"Execution time: 29691.279 ms"
*/</pre> </p>
<p><strong>Check Index usage by above query<br />
(Result is one index scan):</strong></p>
<pre class="crayon-plain-tag">SELECT *
FROM pg_stat_user_indexes
WHERE indexrelname='idx_tbl_itemtransactions_transactiondate';</pre> </p>
<p><strong>Lets see the plan of query with date function filter:</strong></p>
<pre class="crayon-plain-tag">EXPLAIN ANALYZE
SELECT *FROM 
tbl_ItemTransactions 
WHERE EXTRACT(day FROM TransactionDate) = 8;
/*
"Seq Scan on tbl_itemtransactions  
(cost=0.00..533587.00 rows=124848 width=18) 
(actual time=1246.093..37028.883 rows=820800 loops=1)"
"  Filter: (date_part('day'::text, transactiondate) = '8'::double precision)"
"  Rows Removed by Filter: 24148801"
"Planning time: 0.396 ms"
"Execution time: 58230.847 ms"
*/</pre> </p>
<p><strong>Check Index usage by above query:</strong><br />
The Result is one index scan, means above query processed without scaning any index.<br />
We created one index on TransactionDate, but when we use this column with any of default function, planner skips the index fot that column. </p>
<pre class="crayon-plain-tag">SELECT *
FROM pg_stat_user_indexes
WHERE indexrelname='idx_tbl_itemtransactions_transactiondate';</pre> </p>
<p><strong>Now we should create one expression Index:<br />
Please provide your require timezone otherwise It produces error like:</strong></p>
<pre class="crayon-plain-tag">ERROR: functions in index expression must be marked IMMUTABLE</pre> </p>
<pre class="crayon-plain-tag">CREATE INDEX idx_tbl_ItemTransactions_TransactionDate_day
ON tbl_ItemTransactions ((EXTRACT(day FROM TransactionDate AT TIME ZONE 'UTC')));</pre> </p>
<p><strong>Lets execute same query with date function filter:</strong></p>
<pre class="crayon-plain-tag">EXPLAIN ANALYZE
SELECT *FROM 
tbl_ItemTransactions 
WHERE EXTRACT(day FROM TransactionDate AT TIME ZONE 'UTC') = 8;
/*
"Bitmap Heap Scan on tbl_itemtransactions  (cost=2340.01..160893.01 rows=124848 width=18) 
(actual time=367.653..2181.489 rows=820800 loops=1)"
"  Recheck Cond: (date_part('day'::text, 
(transactiondate)::timestamp without time zone) = '8'::double precision)"
"  Heap Blocks: exact=5248"
"  -&gt;  Bitmap Index Scan on idx_tbl_itemtransactions_transactiondate_day  
(cost=0.00..2308.80 rows=124848 width=0) 
(actual time=365.386..365.386 rows=820800 loops=1)"
"        Index Cond: (date_part('day'::text, 
(transactiondate)::timestamp without time zone) = '8'::double precision)"
"Planning time: 0.297 ms"
"Execution time: 3608.713 ms"
*/</pre> </p>
<p><strong>Check created new Expression Index usage by above query<br />
(Result is one index scan):</strong></p>
<pre class="crayon-plain-tag">SELECT *
FROM pg_stat_user_indexes
WHERE indexrelname='idx_tbl_itemtransactions_transactiondate_day';</pre> </p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
