<?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>HOT &#8211; Database Research &amp; Development</title>
	<atom:link href="https://www.dbrnd.com/tag/hot/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>Sun, 25 Dec 2016 13:28:47 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.0.4</generator>
	<item>
		<title>PostgreSQL: Increase the speed of UPDATE query using HOT UPDATE (Heap only tuple)</title>
		<link>https://www.dbrnd.com/2016/12/postgresql-increase-the-speed-of-update-query-using-hot-update-heap-only-tuple-mvcc-fill-factor-vacuum-fragmentation/</link>
					<comments>https://www.dbrnd.com/2016/12/postgresql-increase-the-speed-of-update-query-using-hot-update-heap-only-tuple-mvcc-fill-factor-vacuum-fragmentation/#comments</comments>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Sun, 25 Dec 2016 13:28:47 +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[Fill Factor]]></category>
		<category><![CDATA[fragmentation]]></category>
		<category><![CDATA[HOT]]></category>
		<category><![CDATA[Hot only tuple]]></category>
		<category><![CDATA[MVCC]]></category>
		<category><![CDATA[performance]]></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[UPDATE]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=3708</guid>

					<description><![CDATA[What is HOT UPDATE (Heap Only Tuple) and How It helps to increase the performance of UPDATE Query? In this post, I am going to explain the answer of this question. This is one of the internal of PostgreSQL, but I am sure that most of you guys don&#8217;t know about PostgreSQL HOT. PostgreSQL requires [&#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-increase-the-speed-of-update-query-using-hot-update-heap-only-tuple-mvcc-fill-factor-vacuum-fragmentation%2F&amp;linkname=PostgreSQL%3A%20Increase%20the%20speed%20of%20UPDATE%20query%20using%20HOT%20UPDATE%20%28Heap%20only%20tuple%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-increase-the-speed-of-update-query-using-hot-update-heap-only-tuple-mvcc-fill-factor-vacuum-fragmentation%2F&amp;linkname=PostgreSQL%3A%20Increase%20the%20speed%20of%20UPDATE%20query%20using%20HOT%20UPDATE%20%28Heap%20only%20tuple%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-increase-the-speed-of-update-query-using-hot-update-heap-only-tuple-mvcc-fill-factor-vacuum-fragmentation%2F&amp;linkname=PostgreSQL%3A%20Increase%20the%20speed%20of%20UPDATE%20query%20using%20HOT%20UPDATE%20%28Heap%20only%20tuple%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-increase-the-speed-of-update-query-using-hot-update-heap-only-tuple-mvcc-fill-factor-vacuum-fragmentation%2F&amp;linkname=PostgreSQL%3A%20Increase%20the%20speed%20of%20UPDATE%20query%20using%20HOT%20UPDATE%20%28Heap%20only%20tuple%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-increase-the-speed-of-update-query-using-hot-update-heap-only-tuple-mvcc-fill-factor-vacuum-fragmentation%2F&amp;linkname=PostgreSQL%3A%20Increase%20the%20speed%20of%20UPDATE%20query%20using%20HOT%20UPDATE%20%28Heap%20only%20tuple%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-increase-the-speed-of-update-query-using-hot-update-heap-only-tuple-mvcc-fill-factor-vacuum-fragmentation%2F&amp;linkname=PostgreSQL%3A%20Increase%20the%20speed%20of%20UPDATE%20query%20using%20HOT%20UPDATE%20%28Heap%20only%20tuple%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-increase-the-speed-of-update-query-using-hot-update-heap-only-tuple-mvcc-fill-factor-vacuum-fragmentation%2F&#038;title=PostgreSQL%3A%20Increase%20the%20speed%20of%20UPDATE%20query%20using%20HOT%20UPDATE%20%28Heap%20only%20tuple%29" data-a2a-url="https://www.dbrnd.com/2016/12/postgresql-increase-the-speed-of-update-query-using-hot-update-heap-only-tuple-mvcc-fill-factor-vacuum-fragmentation/" data-a2a-title="PostgreSQL: Increase the speed of UPDATE query using HOT UPDATE (Heap only tuple)"></a></p><p><strong>What is HOT UPDATE (Heap Only Tuple) and How It helps to increase the performance of UPDATE Query?</strong><br />
In this post, I am going to explain the answer of this question. </p>
<blockquote><p>This is one of the internal of PostgreSQL, but I am sure that most of you guys don&#8217;t know about PostgreSQL HOT. </p></blockquote>
<p><strong>PostgreSQL requires HOT because of MVCC architecture.</strong><br />
Please read below articles, before start to knowing about the HOT. </p>
<blockquote><p><a href="https://www.dbrnd.com/2015/05/what-is-mvcc-multi-version-concurrency-control/" target="_blank"><strong>What is MVCC?</strong></a></p>
<p><a href="https://www.dbrnd.com/2016/03/postgresql-the-awesome-table-fillfactor-to-speedup-update-and-select-statement/" target="_blank"><strong>What is Fillfactor in PostgreSQL?</strong></a></p>
<p><a href="https://www.dbrnd.com/2015/11/postgresql-short-note-on-vacuum-vacuum-full-and-analyze/" target="_blank"><strong>What is Vacuum in PostgreSQL?</strong></a>
</p></blockquote>
<blockquote><p>In the MVCC architecture, When we update any record, internally MVCC architecture inserts a new record with new values and marks old record as deleted or discarded.<br />
When we vacuum, we can clean that deleted records and utilize that space.
</p></blockquote>
<p><strong>What is HOT (Hot Only Tuple)?</strong></p>
<blockquote><p>Now imagine that, we have total 500 data pages and we are updating record of page number 3, but unfortunately new record insert in page number 500. </p></blockquote>
<blockquote><p>It looks like very big internal fragmentation because of MVCC architecture.<br />
But actually this is not happening.</p></blockquote>
<blockquote><p>PostgreSQL has a concept of HOT, With a HOT dead tuple space can be automatically reclaimed at the time of INSERT or UPDATE if no changes are made to indexed columns. This allows for more consistent performance.</p></blockquote>
<blockquote><p>HOT means, creating a new update tuple if possible on the same page as the old tuple, and maintaining a chain of updated tuples linking a new version to the old tuple.</p></blockquote>
<blockquote><p>HOT updates can only reuse dead tuples from previous transactions directly, not from the same or concurrent ones.<br />
If you have big updates, changing large portions of the table at once, you might want to split them up in a couple of chunks.</p></blockquote>
<blockquote><p>HOT updates only work when the changed columns are not involved in indexes in any way because the indexes pointing the the old tuples need to point to the new version of it as of transaction id. </p></blockquote>
<blockquote><p>You might be blocking HOT updates with indexes on updated columns. If those are expendable, you might get better overall performance without them.</p></blockquote>
<p><strong>Let&#8217;s understand PostgreSQL HOT practically.</strong></p>
<p><strong>Create a table with Sample data: </strong></p>
<pre class="crayon-plain-tag">CREATE TABLE tbl_Employees
(
	EmpID INT
	,EmpName CHARACTER VARYING 	
	,EmpDepartment CHARACTER VARYING
	,EmpSalary INT
);
 
INSERT INTO tbl_Employees
VALUES 
(1,'Anvesh','Database',90000)
,(2,'Jenny','JAVA',65000)
,(3,'Martin','PHP',85000)
,(4,'Roy','PHP',94000)
,(5,'Eric','PHP',70000)
,(6,'Rajesh','Animation',50000)
,(7,'Mahi','Database',40000)
,(8,'Sofia','JAVA',60000);</pre> </p>
<p><strong>Delete one record:</strong></p>
<pre class="crayon-plain-tag">DELETE FROM tbl_Employees WHERE EmpID = 7;</pre> </p>
<p><strong>Insert one New Record:</strong></p>
<pre class="crayon-plain-tag">INSERT INTO tbl_Employees
VALUES 
(7,'Junik','Database',10000);</pre> </p>
<p><strong>Please check If any HOT Update or not on table:</strong></p>
<pre class="crayon-plain-tag">SELECT pg_stat_get_tuples_hot_updated('tbl_employees'::regclass::oid);
/*
Result:
pg_stat_get_tuples_hot_updated
------------------------------
0
*/</pre> </p>
<p><strong>Please update the statestic of that table:</strong></p>
<pre class="crayon-plain-tag">ANALYZE tbl_Employees;</pre> </p>
<p><strong>Now execute one more update command:</strong></p>
<pre class="crayon-plain-tag">UPDATE tbl_Employees SET EmpName = 'Robort' WHERE EmpID=7;</pre> </p>
<p><strong>Please check If any HOT Update or not on table:</strong></p>
<pre class="crayon-plain-tag">SELECT pg_stat_get_tuples_hot_updated('tbl_employees'::regclass::oid);
/*
Result:
pg_stat_get_tuples_hot_updated
------------------------------
1
*/</pre> </p>
<p><strong>Again, do more update:</strong></p>
<pre class="crayon-plain-tag">UPDATE tbl_Employees SET EmpName = 'Manish' WHERE EmpID=8;
UPDATE tbl_Employees SET EmpName = 'Cerlin' WHERE EmpID=4;</pre> </p>
<p><strong>Please check If any change in number of HOT Update of a table:</strong></p>
<pre class="crayon-plain-tag">SELECT pg_stat_get_tuples_hot_updated('tbl_employees'::regclass::oid);
/*
Result:
pg_stat_get_tuples_hot_updated
------------------------------
3
*/</pre> </p>
<p><strong>Now Apply, Index on EmpName Column:</strong></p>
<pre class="crayon-plain-tag">CREATE INDEX idx_tbl_employees_empname 
ON tbl_employees USING BTREE (EmpName);</pre> </p>
<p><strong>After Index, execute few more update:</strong></p>
<pre class="crayon-plain-tag">UPDATE tbl_Employees SET EmpName = 'Kulin' WHERE EmpID=3;
UPDATE tbl_Employees SET EmpName = 'Priya' WHERE EmpID=5;</pre> </p>
<p><strong>After Index, Please check If any change in number of HOT Update of a table:</strong><br />
The result is same because If column has an Index, HOT Update does not work.</p>
<pre class="crayon-plain-tag">SELECT pg_stat_get_tuples_hot_updated('tbl_employees'::regclass::oid);
/*
Result:
pg_stat_get_tuples_hot_updated
------------------------------
3
*/</pre> </p>
<p><strong>You can also check number of HOT update of current transaction:</strong></p>
<pre class="crayon-plain-tag">SELECT pg_stat_get_xact_tuples_hot_updated('tbl_employees'::regclass::oid);</pre> </p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.dbrnd.com/2016/12/postgresql-increase-the-speed-of-update-query-using-hot-update-heap-only-tuple-mvcc-fill-factor-vacuum-fragmentation/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
	</channel>
</rss>
