<?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>Query Optimizer &#8211; Database Research &amp; Development</title>
	<atom:link href="https://www.dbrnd.com/tag/query-optimizer/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>Fri, 22 Sep 2017 20:45:48 +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: Not Good &#8211; Cost Threshold for Parallelism Default is Five</title>
		<link>https://www.dbrnd.com/2017/11/sql-server-not-good-cost-threshold-for-parallelism-default-is-five/</link>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Wed, 29 Nov 2017 16:16:32 +0000</pubDate>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[dbrnd]]></category>
		<category><![CDATA[Execution Plan]]></category>
		<category><![CDATA[MAXDOP]]></category>
		<category><![CDATA[parallelism]]></category>
		<category><![CDATA[Query Optimizer]]></category>
		<category><![CDATA[sp_configure]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Server Administrator]]></category>
		<category><![CDATA[SQL Server Error]]></category>
		<category><![CDATA[SQL Server Monitoring]]></category>
		<category><![CDATA[SQL Server Performance Tuning]]></category>
		<category><![CDATA[SQL Server Programming]]></category>
		<category><![CDATA[SQL Server Tips and Tricks]]></category>
		<category><![CDATA[threshold]]></category>
		<category><![CDATA[TSQL]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=6150</guid>

					<description><![CDATA[<div><img width="640" height="426" src="https://www.dbrnd.com/wp-content/uploads/2017/09/ParallelProcessing2.jpg" class="attachment-post-image size-post-image wp-post-image" alt="ParallelProcessing2" decoding="async" fetchpriority="high" /></div>As per the Microsoft, Use the cost threshold for parallelism option to specify the threshold at which Microsoft SQL Server creates and runs parallel plans for queries. The Cost Threshold for Parallelism parameter is very important for Query Optimizer to prepare the query execution plan. I wrote a title like Not Good &#8211; Because the [&#8230;]]]></description>
										<content:encoded><![CDATA[<div><img width="640" height="426" src="https://www.dbrnd.com/wp-content/uploads/2017/09/ParallelProcessing2.jpg" class="attachment-post-image size-post-image wp-post-image" alt="ParallelProcessing2" decoding="async" /></div><p><a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F11%2Fsql-server-not-good-cost-threshold-for-parallelism-default-is-five%2F&amp;linkname=SQL%20Server%3A%20Not%20Good%20%E2%80%93%20Cost%20Threshold%20for%20Parallelism%20Default%20is%20Five" 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%2F11%2Fsql-server-not-good-cost-threshold-for-parallelism-default-is-five%2F&amp;linkname=SQL%20Server%3A%20Not%20Good%20%E2%80%93%20Cost%20Threshold%20for%20Parallelism%20Default%20is%20Five" 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%2F11%2Fsql-server-not-good-cost-threshold-for-parallelism-default-is-five%2F&amp;linkname=SQL%20Server%3A%20Not%20Good%20%E2%80%93%20Cost%20Threshold%20for%20Parallelism%20Default%20is%20Five" 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%2F11%2Fsql-server-not-good-cost-threshold-for-parallelism-default-is-five%2F&amp;linkname=SQL%20Server%3A%20Not%20Good%20%E2%80%93%20Cost%20Threshold%20for%20Parallelism%20Default%20is%20Five" 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%2F11%2Fsql-server-not-good-cost-threshold-for-parallelism-default-is-five%2F&amp;linkname=SQL%20Server%3A%20Not%20Good%20%E2%80%93%20Cost%20Threshold%20for%20Parallelism%20Default%20is%20Five" 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%2F11%2Fsql-server-not-good-cost-threshold-for-parallelism-default-is-five%2F&amp;linkname=SQL%20Server%3A%20Not%20Good%20%E2%80%93%20Cost%20Threshold%20for%20Parallelism%20Default%20is%20Five" 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%2F11%2Fsql-server-not-good-cost-threshold-for-parallelism-default-is-five%2F&#038;title=SQL%20Server%3A%20Not%20Good%20%E2%80%93%20Cost%20Threshold%20for%20Parallelism%20Default%20is%20Five" data-a2a-url="https://www.dbrnd.com/2017/11/sql-server-not-good-cost-threshold-for-parallelism-default-is-five/" data-a2a-title="SQL Server: Not Good – Cost Threshold for Parallelism Default is Five"></a></p><p>As per the Microsoft, Use the cost threshold for parallelism option to specify the threshold at which Microsoft SQL Server creates and runs parallel plans for queries.</p>
<p>The Cost Threshold for Parallelism parameter is very important for Query Optimizer to prepare the query execution plan.</p>
<blockquote><p>I wrote a title like Not Good &#8211; Because the default value of Cost Threshold for Parallelism is 5 which is very less now a day.</p>
<p>The value you define here which is for query optimizer where it finds a cheaper parallel execution plan, and if it fails to find parallel execution plan in given cost, it chooses the serial execution plan which degrades the query performance.</p>
<p>Now a day, we are dealing with millions/billions of data, so parallel query execution plan is good for big/complex queries so the default value 5 which is a tiny number.</p></blockquote>
<p>The parallel execution plans will give you more suggestion on missing indexes which we can take for further query optimization.</p>
<p><em>I would suggest to change the value of Cost Threshold for Parallelism from 5 to 50 or even more.</em></p>
<p><strong>How to change?</strong></p>
<p><strong>Using Script:</strong></p><pre class="crayon-plain-tag">sp_configure 'show advanced options', 1;
GO
reconfigure;
GO
sp_configure 'cost threshold for parallelism', 50;
GO
reconfigure;
GO</pre><p>
<strong>Using SSMS:</strong></p>
<p><img decoding="async" class="alignnone size-full wp-image-6152" src="https://www.dbrnd.com/wp-content/uploads/2017/09/CostThresholdForParallelism.png" alt="Cost Threshold For Parallelism" width="694" height="630" srcset="https://www.dbrnd.com/wp-content/uploads/2017/09/CostThresholdForParallelism.png 694w, https://www.dbrnd.com/wp-content/uploads/2017/09/CostThresholdForParallelism-679x616.png 679w" sizes="(max-width: 694px) 100vw, 694px" /></p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>MySQL: How Query Optimizer Read and Update the Index Statistics</title>
		<link>https://www.dbrnd.com/2017/01/mysql-how-query-optimizer-read-and-update-the-index-statistics-using-innodb_stats_on_metadata-information_schema-statistics/</link>
					<comments>https://www.dbrnd.com/2017/01/mysql-how-query-optimizer-read-and-update-the-index-statistics-using-innodb_stats_on_metadata-information_schema-statistics/#comments</comments>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Sun, 08 Jan 2017 15:29:33 +0000</pubDate>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[dbrnd]]></category>
		<category><![CDATA[Index Statistics]]></category>
		<category><![CDATA[INFORMATION_SCHEMA.STATISTICS]]></category>
		<category><![CDATA[innodb_stats_on_metadata]]></category>
		<category><![CDATA[MySQL Command]]></category>
		<category><![CDATA[MySQL Database Administrator]]></category>
		<category><![CDATA[MySQL Database Designing]]></category>
		<category><![CDATA[MySQL Database Programming]]></category>
		<category><![CDATA[MySQL Error]]></category>
		<category><![CDATA[MySQL Performance Tunning]]></category>
		<category><![CDATA[MySQL Query]]></category>
		<category><![CDATA[MySQL Tips and Tricks]]></category>
		<category><![CDATA[Query Execution Plan]]></category>
		<category><![CDATA[Query Optimizer]]></category>
		<category><![CDATA[Query Planner]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=3733</guid>

					<description><![CDATA[For all the RDBMS, Database Statistics are playing the main role to generate an accurate Query Execution Plan. A Database Administrator is also very much responsible to make sure that Database Statistics is updated. MySQL query optimizer is also preparing Query Execution Plan with the help of Statistics Information. The auto-update Statistics configuration is also [&#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%2F01%2Fmysql-how-query-optimizer-read-and-update-the-index-statistics-using-innodb_stats_on_metadata-information_schema-statistics%2F&amp;linkname=MySQL%3A%20How%20Query%20Optimizer%20Read%20and%20Update%20the%20Index%20Statistics" 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%2F01%2Fmysql-how-query-optimizer-read-and-update-the-index-statistics-using-innodb_stats_on_metadata-information_schema-statistics%2F&amp;linkname=MySQL%3A%20How%20Query%20Optimizer%20Read%20and%20Update%20the%20Index%20Statistics" 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%2F01%2Fmysql-how-query-optimizer-read-and-update-the-index-statistics-using-innodb_stats_on_metadata-information_schema-statistics%2F&amp;linkname=MySQL%3A%20How%20Query%20Optimizer%20Read%20and%20Update%20the%20Index%20Statistics" 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%2F01%2Fmysql-how-query-optimizer-read-and-update-the-index-statistics-using-innodb_stats_on_metadata-information_schema-statistics%2F&amp;linkname=MySQL%3A%20How%20Query%20Optimizer%20Read%20and%20Update%20the%20Index%20Statistics" 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%2F01%2Fmysql-how-query-optimizer-read-and-update-the-index-statistics-using-innodb_stats_on_metadata-information_schema-statistics%2F&amp;linkname=MySQL%3A%20How%20Query%20Optimizer%20Read%20and%20Update%20the%20Index%20Statistics" 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%2F01%2Fmysql-how-query-optimizer-read-and-update-the-index-statistics-using-innodb_stats_on_metadata-information_schema-statistics%2F&amp;linkname=MySQL%3A%20How%20Query%20Optimizer%20Read%20and%20Update%20the%20Index%20Statistics" 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%2F01%2Fmysql-how-query-optimizer-read-and-update-the-index-statistics-using-innodb_stats_on_metadata-information_schema-statistics%2F&#038;title=MySQL%3A%20How%20Query%20Optimizer%20Read%20and%20Update%20the%20Index%20Statistics" data-a2a-url="https://www.dbrnd.com/2017/01/mysql-how-query-optimizer-read-and-update-the-index-statistics-using-innodb_stats_on_metadata-information_schema-statistics/" data-a2a-title="MySQL: How Query Optimizer Read and Update the Index Statistics"></a></p><p>For all the RDBMS, Database Statistics are playing the main role to generate an accurate Query Execution Plan.<br />
A Database Administrator is also very much responsible to make sure that Database Statistics is updated.</p>
<blockquote><p>MySQL query optimizer is also preparing Query Execution Plan with the help of Statistics Information.<br />
The auto-update Statistics configuration is also very important to keep Statistics updated.</p></blockquote>
<p><strong>How to enable auto-update Statistics for InnoDB engine?</strong></p>
<blockquote><p>We can configure innodb_stats_on_metadata variable to enable auto-update Statistics.</p></blockquote>
<blockquote><p>When this variable is enabled InnoDB updates statistics when metadata statements such as SHOW TABLE STATUS or SHOW INDEX are run, or when accessing the INFORMATION_SCHEMA.TABLES or INFORMATION_SCHEMA.STATISTICS tables.
</p></blockquote>
<p><strong>Check innodb_stats_on_metadata is ON or OFF:</strong></p>
<pre class="crayon-plain-tag">SHOW VARIABLES LIKE 'innodb_stats_on_metadata';</pre> </p>
<p><strong>If innodb_stats_on_metadata is OFF, make it ON:</strong></p>
<pre class="crayon-plain-tag">SET GLOBAL innodb_stats_on_metadata=ON;</pre> </p>
<p>When you perform command like SHOW INDEX, Statisitcs will be update.<br />
Accuracy of statistics is depends on MySQL Server Load.</p>
<pre class="crayon-plain-tag">SHOW INDEXES FROM SchemaName.TableName;</pre> </p>
<p><strong>You can use below query to check the Index Statistics:</strong></p>
<pre class="crayon-plain-tag">SELECT 
    Table_Schema
    ,Table_Name
    ,Non_Unique
    ,Index_Schema
    ,Seq_In_Index
    ,Column_Name
    ,Collation
    ,Cardinality
    ,Index_Type
FROM information_schema.statistics 
WHERE TABLE_SCHEMA='SchemaName';</pre> </p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.dbrnd.com/2017/01/mysql-how-query-optimizer-read-and-update-the-index-statistics-using-innodb_stats_on_metadata-information_schema-statistics/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
			</item>
		<item>
		<title>SQL Server Interview: How internally it executes a SQL Statement?</title>
		<link>https://www.dbrnd.com/2017/01/sql-server-interview-how-internally-it-executes-a-sql-statement/</link>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Mon, 02 Jan 2017 18:59:12 +0000</pubDate>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Server Interview]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[Binding]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[dbrnd]]></category>
		<category><![CDATA[Parser]]></category>
		<category><![CDATA[query executor]]></category>
		<category><![CDATA[Query Optimizer]]></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>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=2419</guid>

					<description><![CDATA[The Storage Engine and Query Processor are both important core part of SQL Server. Storage Engine is responsible for data integrity and data storage in disk / memory while the Query Processor simply process the query bases on execution plan and return the result. Most of the interviewer asks this question to SQL Server DBA [&#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%2F01%2Fsql-server-interview-how-internally-it-executes-a-sql-statement%2F&amp;linkname=SQL%20Server%20Interview%3A%20How%20internally%20it%20executes%20a%20SQL%20Statement%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%2F2017%2F01%2Fsql-server-interview-how-internally-it-executes-a-sql-statement%2F&amp;linkname=SQL%20Server%20Interview%3A%20How%20internally%20it%20executes%20a%20SQL%20Statement%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%2F2017%2F01%2Fsql-server-interview-how-internally-it-executes-a-sql-statement%2F&amp;linkname=SQL%20Server%20Interview%3A%20How%20internally%20it%20executes%20a%20SQL%20Statement%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%2F2017%2F01%2Fsql-server-interview-how-internally-it-executes-a-sql-statement%2F&amp;linkname=SQL%20Server%20Interview%3A%20How%20internally%20it%20executes%20a%20SQL%20Statement%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%2F2017%2F01%2Fsql-server-interview-how-internally-it-executes-a-sql-statement%2F&amp;linkname=SQL%20Server%20Interview%3A%20How%20internally%20it%20executes%20a%20SQL%20Statement%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%2F2017%2F01%2Fsql-server-interview-how-internally-it-executes-a-sql-statement%2F&amp;linkname=SQL%20Server%20Interview%3A%20How%20internally%20it%20executes%20a%20SQL%20Statement%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%2F2017%2F01%2Fsql-server-interview-how-internally-it-executes-a-sql-statement%2F&#038;title=SQL%20Server%20Interview%3A%20How%20internally%20it%20executes%20a%20SQL%20Statement%3F" data-a2a-url="https://www.dbrnd.com/2017/01/sql-server-interview-how-internally-it-executes-a-sql-statement/" data-a2a-title="SQL Server Interview: How internally it executes a SQL Statement?"></a></p><p><em>The Storage Engine and Query Processor are both important core part of SQL Server. Storage Engine is responsible for data integrity and data storage in disk / memory while the Query Processor simply process the query bases on execution plan and return the result.</p>
<p>Most of the interviewer asks this question to SQL Server DBA that How SQL Server executes SQL Query and Statement so I have decided to prepare a short note on this.<br />
</em><br />
<strong>How SQL Server internally executes SQL Query and Statement?<br />
</strong></p>
<p><strong>Parser:</strong></p>
<blockquote><p>The Parser makes sure that the T-SQL query has a valid syntax, and translates the SQL query into an initial logical tree representation with different information such as operators, join conditions, expressions, identifiers.<br />
It sents this logical tree to Query Optimizer. </p></blockquote>
<p><strong>Binding:</strong></p>
<blockquote><p>It is a name resolution process and SQL Server makes sure that all object names are valid and they exist on a disk.<br />
It creates algebrized tree with table and column names information and sents to Query Optimizer.</p></blockquote>
<p><strong>The Query Optimizer: </strong></p>
<blockquote><p>It uses logical tree and algebrized tree generated by Parsing and Binding Process and extracts the different information like, table data, table definition, database information and other database statistics. </p></blockquote>
<blockquote><p>If we look at specific table data extraction operation, there are different approaches like: selecting few records require Index Scan, selecting all table data require Table Scan, selecting data from multiple tables require parsing of JOIN conditions. </p></blockquote>
<blockquote><p>Query Optimizer is to find an efficient execution plan for your query. Even for relatively simple queries, there may be a large number of different ways to access the data to produce the same end result.</p></blockquote>
<blockquote><p>The Query Optimizer is a cost-based optimizer. Each execution plan has associated with cost estimation stats so After extracting the information, Query Optimizer chooses the best efficient execution plan and submit a query to query execution engine.
</p></blockquote>
<blockquote><p>Query Optimizer is working within a time constraint, there’s a chance that the plan selected may be the optimal plan, but it is also likely that it may just be something close to the optimal plan.</p></blockquote>
<p><strong>Query Execution:</strong></p>
<blockquote><p>After optimization of the query, the Execution Engine selects resulting plan and retrieve the desired data.</p>
<p>It also stores generated execution plan into the memory for the query caching purpose.<br />
The stored plan cache may reuse for same query execution, which actually saves lots of time of the Query Optimizer.</p></blockquote>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>SQL Server:Warning in Query Execution Plan, &#8220;Operator used tempdb to spill data during execution with spill level 2&#8221;</title>
		<link>https://www.dbrnd.com/2016/11/sql-serverwarning-in-query-execution-plan-operator-used-tempdb-to-spill-data-during-execution-with-spill-level-2-sql-update-statistics-sp_updatestats/</link>
					<comments>https://www.dbrnd.com/2016/11/sql-serverwarning-in-query-execution-plan-operator-used-tempdb-to-spill-data-during-execution-with-spill-level-2-sql-update-statistics-sp_updatestats/#comments</comments>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Fri, 18 Nov 2016 18:07:18 +0000</pubDate>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[AUTO_CREATE_STATISTICS]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[dbrnd]]></category>
		<category><![CDATA[Execution Plan]]></category>
		<category><![CDATA[Query Optimizer]]></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[TempDB]]></category>
		<category><![CDATA[TempDB Spill error]]></category>
		<category><![CDATA[TSQL]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=3869</guid>

					<description><![CDATA[Before a few days ago, When I was performing query optimization at that time I have seen one warning in the result of SQL Server Query Execution Plan like: &#8220;Operator used tempdb to spill data during execution with spill level 2&#8221;. I performed RND on this and came to know very interesting reason this warning. [&#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%2F11%2Fsql-serverwarning-in-query-execution-plan-operator-used-tempdb-to-spill-data-during-execution-with-spill-level-2-sql-update-statistics-sp_updatestats%2F&amp;linkname=SQL%20Server%3AWarning%20in%20Query%20Execution%20Plan%2C%20%E2%80%9COperator%20used%20tempdb%20to%20spill%20data%20during%20execution%20with%20spill%20level%202%E2%80%9D" 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%2F11%2Fsql-serverwarning-in-query-execution-plan-operator-used-tempdb-to-spill-data-during-execution-with-spill-level-2-sql-update-statistics-sp_updatestats%2F&amp;linkname=SQL%20Server%3AWarning%20in%20Query%20Execution%20Plan%2C%20%E2%80%9COperator%20used%20tempdb%20to%20spill%20data%20during%20execution%20with%20spill%20level%202%E2%80%9D" 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%2F11%2Fsql-serverwarning-in-query-execution-plan-operator-used-tempdb-to-spill-data-during-execution-with-spill-level-2-sql-update-statistics-sp_updatestats%2F&amp;linkname=SQL%20Server%3AWarning%20in%20Query%20Execution%20Plan%2C%20%E2%80%9COperator%20used%20tempdb%20to%20spill%20data%20during%20execution%20with%20spill%20level%202%E2%80%9D" 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%2F11%2Fsql-serverwarning-in-query-execution-plan-operator-used-tempdb-to-spill-data-during-execution-with-spill-level-2-sql-update-statistics-sp_updatestats%2F&amp;linkname=SQL%20Server%3AWarning%20in%20Query%20Execution%20Plan%2C%20%E2%80%9COperator%20used%20tempdb%20to%20spill%20data%20during%20execution%20with%20spill%20level%202%E2%80%9D" 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%2F11%2Fsql-serverwarning-in-query-execution-plan-operator-used-tempdb-to-spill-data-during-execution-with-spill-level-2-sql-update-statistics-sp_updatestats%2F&amp;linkname=SQL%20Server%3AWarning%20in%20Query%20Execution%20Plan%2C%20%E2%80%9COperator%20used%20tempdb%20to%20spill%20data%20during%20execution%20with%20spill%20level%202%E2%80%9D" 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%2F11%2Fsql-serverwarning-in-query-execution-plan-operator-used-tempdb-to-spill-data-during-execution-with-spill-level-2-sql-update-statistics-sp_updatestats%2F&amp;linkname=SQL%20Server%3AWarning%20in%20Query%20Execution%20Plan%2C%20%E2%80%9COperator%20used%20tempdb%20to%20spill%20data%20during%20execution%20with%20spill%20level%202%E2%80%9D" 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%2F11%2Fsql-serverwarning-in-query-execution-plan-operator-used-tempdb-to-spill-data-during-execution-with-spill-level-2-sql-update-statistics-sp_updatestats%2F&#038;title=SQL%20Server%3AWarning%20in%20Query%20Execution%20Plan%2C%20%E2%80%9COperator%20used%20tempdb%20to%20spill%20data%20during%20execution%20with%20spill%20level%202%E2%80%9D" data-a2a-url="https://www.dbrnd.com/2016/11/sql-serverwarning-in-query-execution-plan-operator-used-tempdb-to-spill-data-during-execution-with-spill-level-2-sql-update-statistics-sp_updatestats/" data-a2a-title="SQL Server:Warning in Query Execution Plan, “Operator used tempdb to spill data during execution with spill level 2”"></a></p><p>Before a few days ago, When I was performing query optimization at that time I have seen one warning in the result of SQL Server Query Execution Plan like:<strong> &#8220;Operator used tempdb to spill data during execution with spill level 2&#8221;.</strong><br />
I performed RND on this and came to know very interesting reason this warning. </p>
<blockquote><p>If you don&#8217;t know, please note that Database Statistics are playing main to prepare and generate the Query Execution Plan. </p></blockquote>
<blockquote><p>SQL Server Statistics contain different types of meta data information like total number records, indexes, access path and others. </p></blockquote>
<blockquote><p>This warning occurred because my database Statistics are not updated.<br />
For example, my table has total 50000 records and Statistics contains only 5000 records information so the planner prepares the query execution plan according to 5000 records only. </p></blockquote>
<blockquote><p>While executing the query, If it finds more than 5000 records, It transfers other additional records first into TempDB and continue further process using TempDB. Because It calculated processing capacity only for 5000 records. </p></blockquote>
<blockquote><p>This process degrades the overall query performance and that&#8217;s why you can see a warning like: &#8220;Operator used tempdb to spill data during execution with spill level 2&#8221;.</p></blockquote>
<p><strong>The solution is:</strong></p>
<blockquote><p>Find Statistics data for tables and indexes and If Statistics data are not updated, update your Statistics data, so that Query Optimizer can prepare the best Query Execution Plan.
</p></blockquote>
<p><strong>Check the Statistics for particular index: </strong><br />
If you find difference between Rows and Rows Sampled, Statistics is not updated. </p>
<pre class="crayon-plain-tag">DBCC SHOW_STATISTICS ("TableName", IndexName);</pre> </p>
<p><strong>Update all Statistics for Database: </strong></p>
<pre class="crayon-plain-tag">EXEC sp_updatestats;</pre> </p>
<p><strong>Update Statistics for Table:</strong></p>
<pre class="crayon-plain-tag">UPDATE STATISTICS TABLE_NAME;</pre> </p>
<p><strong>Update Statistics for Index:</strong></p>
<pre class="crayon-plain-tag">UPDATE STATISTICS TABLE_NAME INDEX_NAME;</pre> </p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.dbrnd.com/2016/11/sql-serverwarning-in-query-execution-plan-operator-used-tempdb-to-spill-data-during-execution-with-spill-level-2-sql-update-statistics-sp_updatestats/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
			</item>
		<item>
		<title>MySQL: Controlling Query Optimizer to choose the Best Execution Plan</title>
		<link>https://www.dbrnd.com/2016/10/mysql-controlling-query-optimizer-to-choose-the-best-execution-plan-optimizer_prune_level-optimizer_search_depth/</link>
					<comments>https://www.dbrnd.com/2016/10/mysql-controlling-query-optimizer-to-choose-the-best-execution-plan-optimizer_prune_level-optimizer_search_depth/#comments</comments>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Thu, 20 Oct 2016 16:16:18 +0000</pubDate>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[dbrnd]]></category>
		<category><![CDATA[Execution Plan]]></category>
		<category><![CDATA[MySQL Command]]></category>
		<category><![CDATA[MySQL Database Administrator]]></category>
		<category><![CDATA[MySQL Database Designing]]></category>
		<category><![CDATA[MySQL Database Programming]]></category>
		<category><![CDATA[MySQL Error]]></category>
		<category><![CDATA[MySQL Performance Tunning]]></category>
		<category><![CDATA[MySQL Query]]></category>
		<category><![CDATA[MySQL Tips and Tricks]]></category>
		<category><![CDATA[optimizer_prune_level]]></category>
		<category><![CDATA[optimizer_search_depth]]></category>
		<category><![CDATA[Query Optimizer]]></category>
		<category><![CDATA[Query Planner]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=3785</guid>

					<description><![CDATA[In this post, I am sharing two important parameters which might be useful to improve query execution process in MySQL Server. The Query Optimizer is playing the main role in the Query Execution Process. It is responsible to choose best Query Execution Plan among the list of Plans. In MySQL, we can control the task [&#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%2Fmysql-controlling-query-optimizer-to-choose-the-best-execution-plan-optimizer_prune_level-optimizer_search_depth%2F&amp;linkname=MySQL%3A%20Controlling%20Query%20Optimizer%20to%20choose%20the%20Best%20Execution%20Plan" 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%2Fmysql-controlling-query-optimizer-to-choose-the-best-execution-plan-optimizer_prune_level-optimizer_search_depth%2F&amp;linkname=MySQL%3A%20Controlling%20Query%20Optimizer%20to%20choose%20the%20Best%20Execution%20Plan" 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%2Fmysql-controlling-query-optimizer-to-choose-the-best-execution-plan-optimizer_prune_level-optimizer_search_depth%2F&amp;linkname=MySQL%3A%20Controlling%20Query%20Optimizer%20to%20choose%20the%20Best%20Execution%20Plan" 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%2Fmysql-controlling-query-optimizer-to-choose-the-best-execution-plan-optimizer_prune_level-optimizer_search_depth%2F&amp;linkname=MySQL%3A%20Controlling%20Query%20Optimizer%20to%20choose%20the%20Best%20Execution%20Plan" 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%2Fmysql-controlling-query-optimizer-to-choose-the-best-execution-plan-optimizer_prune_level-optimizer_search_depth%2F&amp;linkname=MySQL%3A%20Controlling%20Query%20Optimizer%20to%20choose%20the%20Best%20Execution%20Plan" 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%2Fmysql-controlling-query-optimizer-to-choose-the-best-execution-plan-optimizer_prune_level-optimizer_search_depth%2F&amp;linkname=MySQL%3A%20Controlling%20Query%20Optimizer%20to%20choose%20the%20Best%20Execution%20Plan" 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%2Fmysql-controlling-query-optimizer-to-choose-the-best-execution-plan-optimizer_prune_level-optimizer_search_depth%2F&#038;title=MySQL%3A%20Controlling%20Query%20Optimizer%20to%20choose%20the%20Best%20Execution%20Plan" data-a2a-url="https://www.dbrnd.com/2016/10/mysql-controlling-query-optimizer-to-choose-the-best-execution-plan-optimizer_prune_level-optimizer_search_depth/" data-a2a-title="MySQL: Controlling Query Optimizer to choose the Best Execution Plan"></a></p><p>In this post, I am sharing two important parameters which might be useful to improve query execution process in MySQL Server.</p>
<blockquote><p>The Query Optimizer is playing the main role in the Query Execution Process. It is responsible to choose best Query Execution Plan among the list of Plans.<br />
In MySQL, we can control the task of Query Optimizer by setting few parameters.
</p></blockquote>
<p><strong>Why because?</strong></p>
<blockquote><p>If you have one big SQL Query (included around 40 tables), Planner generates all possible plans mostly It generates 40+ query execution plan so now Query Optimizer will take more time to SELECT best plan. </p></blockquote>
<blockquote><p>We should tune this situation by knowing about optimizer_prune_level and optimizer_search_depth parameters. </p></blockquote>
<p><strong>optimizer_prune_level:</strong> Default is ON, It tells Query Optimizer to skip certain plans based on estimated number of rows. </p>
<blockquote><p>If we have total 40 tables, Planner generates all possible 40+ plans including individual table plan which might not be needed for Query Optimizer. </p></blockquote>
<blockquote><p>If you believe that the optimizer missed a better query plan, this option can be switched off (optimizer_prune_level=0) with the risk that query compilation may take much longer.<br />
This variable we can set both GLOBAL and Session level. </p>
<pre class="crayon-plain-tag">SHOW VARIABLES LIKE 'optimizer_prune_level';

SET GLOBAL optimizer_prune_level=0; -- For global change 
SET optimizer_prune_level=0; -- For session specific change</pre> </p></blockquote>
<p><strong>optimizer_search_depth:</strong> Default value is 62, the Planner is generating multiple plans, but sometimes It also generates an incomplete plan. </p>
<blockquote><p>When Query Optimizer starts to scan the plan, we can set optimizer_search_depth value to tell how far each incomplete plan the optimizer should look to evaluate.</p></blockquote>
<blockquote><p>If we set a higher value (Max 63), Query Optimizer try to evaluate all incomplete plans which will take more time to execute. If we set lower value, Query Optimizer can skip few incomplete generated plans.</p>
<p>This variable we can set both GLOBAL and Session level. </p>
<pre class="crayon-plain-tag">SHOW VARIABLES LIKE 'optimizer_search_depth';

SET GLOBAL optimizer_search_depth=40; -- For global change 
SET optimizer_search_depth=40; -- For session specific change</pre> </p></blockquote>
]]></content:encoded>
					
					<wfw:commentRss>https://www.dbrnd.com/2016/10/mysql-controlling-query-optimizer-to-choose-the-best-execution-plan-optimizer_prune_level-optimizer_search_depth/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
		<item>
		<title>MySQL: SELECT with Index Hint option to Optimize the Query</title>
		<link>https://www.dbrnd.com/2016/10/mysql-select-with-index-hint-option-to-optimize-the-query-use-index-force-index-ignore-index/</link>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Wed, 19 Oct 2016 16:16:06 +0000</pubDate>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[dbrnd]]></category>
		<category><![CDATA[FORCE INDEX]]></category>
		<category><![CDATA[IGNORE INDEX]]></category>
		<category><![CDATA[index]]></category>
		<category><![CDATA[Index Hint]]></category>
		<category><![CDATA[index usage]]></category>
		<category><![CDATA[MySQL Command]]></category>
		<category><![CDATA[MySQL Database Administrator]]></category>
		<category><![CDATA[MySQL Database Designing]]></category>
		<category><![CDATA[MySQL Database Programming]]></category>
		<category><![CDATA[MySQL Error]]></category>
		<category><![CDATA[MySQL Performance Tunning]]></category>
		<category><![CDATA[MySQL Query]]></category>
		<category><![CDATA[MySQL Tips and Tricks]]></category>
		<category><![CDATA[Query Optimizer]]></category>
		<category><![CDATA[USE INDEX]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=3795</guid>

					<description><![CDATA[The Table Indexes are always the main focus to improve the performance of any SQL Query. In MySQL, We can provide INDEX Hint to Query Optimizer for choosing our analyzed Index. In most of the cases, Query Planner and Query Optimizer are accurate to choose perfect Index. But sometimes, still It is required to give [&#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%2Fmysql-select-with-index-hint-option-to-optimize-the-query-use-index-force-index-ignore-index%2F&amp;linkname=MySQL%3A%20SELECT%20with%20Index%20Hint%20option%20to%20Optimize%20the%20Query" 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%2Fmysql-select-with-index-hint-option-to-optimize-the-query-use-index-force-index-ignore-index%2F&amp;linkname=MySQL%3A%20SELECT%20with%20Index%20Hint%20option%20to%20Optimize%20the%20Query" 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%2Fmysql-select-with-index-hint-option-to-optimize-the-query-use-index-force-index-ignore-index%2F&amp;linkname=MySQL%3A%20SELECT%20with%20Index%20Hint%20option%20to%20Optimize%20the%20Query" 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%2Fmysql-select-with-index-hint-option-to-optimize-the-query-use-index-force-index-ignore-index%2F&amp;linkname=MySQL%3A%20SELECT%20with%20Index%20Hint%20option%20to%20Optimize%20the%20Query" 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%2Fmysql-select-with-index-hint-option-to-optimize-the-query-use-index-force-index-ignore-index%2F&amp;linkname=MySQL%3A%20SELECT%20with%20Index%20Hint%20option%20to%20Optimize%20the%20Query" 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%2Fmysql-select-with-index-hint-option-to-optimize-the-query-use-index-force-index-ignore-index%2F&amp;linkname=MySQL%3A%20SELECT%20with%20Index%20Hint%20option%20to%20Optimize%20the%20Query" 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%2Fmysql-select-with-index-hint-option-to-optimize-the-query-use-index-force-index-ignore-index%2F&#038;title=MySQL%3A%20SELECT%20with%20Index%20Hint%20option%20to%20Optimize%20the%20Query" data-a2a-url="https://www.dbrnd.com/2016/10/mysql-select-with-index-hint-option-to-optimize-the-query-use-index-force-index-ignore-index/" data-a2a-title="MySQL: SELECT with Index Hint option to Optimize the Query"></a></p><p>The Table Indexes are always the main focus to improve the performance of any SQL Query.<br />
In MySQL, We can provide INDEX Hint to Query Optimizer for choosing our analyzed Index.</p>
<blockquote><p>In most of the cases, Query Planner and Query Optimizer are accurate to choose perfect Index.<br />
But sometimes, still It is required to give Index Hint for reducing the overall Query Planning Time.</p></blockquote>
<p>If we know that this Index is good for this query, we should Provide the Hint to Query Optimizer. </p>
<p>Here, You can also access one related article.</p>
<blockquote class="wp-embedded-content" data-secret="5V4cj4uvhh"><p><a href="https://www.dbrnd.com/2015/10/mysql-script-to-find-index-usage-statistics-of-a-database/">MySQL: Script to find Index Usage Statistics of a Database</a></p></blockquote>
<p><iframe loading="lazy" class="wp-embedded-content" sandbox="allow-scripts" security="restricted"  src="https://www.dbrnd.com/2015/10/mysql-script-to-find-index-usage-statistics-of-a-database/embed/#?secret=5V4cj4uvhh" data-secret="5V4cj4uvhh" width="600" height="338" title="&#8220;MySQL: Script to find Index Usage Statistics of a Database&#8221; &#8212; Database Research &amp; Development" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe></p>
<p><strong>MySQL supports command like USE INDEX, IGNORE INDEX, FORCE INDEX, which we can use for Index Hint.</strong> </p>
<blockquote><p>The USE INDEX hint tells MySQL to use only one of the named indexes to find rows in the table.<br />
The IGNORE INDEX tells MySQL to not use some particular index or indexes.<br />
The FORCE INDEX hint acts like USE INDEX , with the addition that a table scan is assumed to be very expensive.
</p></blockquote>
<p>Prepared small demonstration on this.</p>
<p><strong>Create a sample table:</strong></p>
<pre class="crayon-plain-tag">CREATE TABLE tbl_EmployeeDetails
(
    EmpID INTEGER PRIMARY KEY 
    ,EmpName VARCHAR(50)
    ,EmpSalary BIGINT
    ,DepartmentName VARCHAR(50)
);</pre> </p>
<p><strong>Insert few sample Records:</strong></p>
<pre class="crayon-plain-tag">INSERT INTO tbl_EmployeeDetails
VALUES 
(1,'Anvesh',80000,'Sales')
,(2,'Neevan',90000,'Sales')
,(3,'Jenny',50000,'Production')
,(4,'Roy',60000,'Production')
,(5,'Martin',30000,'Research')
,(6,'Mahi',85000,'Research')
,(7,'Kruti',45000,'Research')
,(8,'Manish',75000,'Research');</pre> </p>
<p><strong>Create few sample Indexes:</strong></p>
<pre class="crayon-plain-tag">CREATE INDEX idx_tbl_EmployeeDetails_EmpName ON tbl_EmployeeDetails (EmpName);
CREATE INDEX idx_tbl_EmployeeDetails_DepartmentName ON tbl_EmployeeDetails (DepartmentName);
CREATE INDEX idx_tbl_EmployeeDetails_EmpName_EmpSalary ON tbl_EmployeeDetails (EmpName,EmpSalary);</pre> </p>
<p><strong>Using USE INDEX:</strong></p>
<pre class="crayon-plain-tag">SELECT *FROM tbl_EmployeeDetails
USE INDEX (idx_tbl_EmployeeDetails_EmpName)
WHERE EmpName = 'Roy';

SELECT *FROM tbl_EmployeeDetails
USE INDEX (idx_tbl_EmployeeDetails_EmpName_EmpSalary)
WHERE EmpName = 'Roy' AND EmpSalary &gt; 50000;</pre> </p>
<p><strong>Using IGNORE INDEX:</strong></p>
<pre class="crayon-plain-tag">SELECT *FROM tbl_EmployeeDetails
IGNORE INDEX (idx_tbl_EmployeeDetails_EmpName)
WHERE EmpName = 'Roy' AND EmpSalary &gt; 50000;</pre> </p>
<p><strong>Using FORCE INDEX:</strong></p>
<pre class="crayon-plain-tag">SELECT *FROM tbl_EmployeeDetails
FORCE INDEX (idx_tbl_EmployeeDetails_EmpName)
WHERE EmpName = 'Roy' AND EmpSalary &gt; 50000;</pre> </p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>PostgreSQL: Using EXPLAIN ANALYZE, Know your Query Execution Plan</title>
		<link>https://www.dbrnd.com/2016/09/postgresql-using-explain-analyze-know-your-query-execution-plan/</link>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Thu, 08 Sep 2016 20:10: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[Execution Plan]]></category>
		<category><![CDATA[EXPLAIN]]></category>
		<category><![CDATA[Explain Analyze]]></category>
		<category><![CDATA[Genetic Query Optimizer]]></category>
		<category><![CDATA[GEQO]]></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[Query Optimizer]]></category>
		<category><![CDATA[Query Plan]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=3616</guid>

					<description><![CDATA[Like any other RDBMS, PostgreSQL generates a Query Execution Plan for each query that it receives. It is also used Genetic Query Optimizer (GEQO) that does query planning for heuristic search and It reduce query planning time for large and complex queries. An Optimizer always tries to choose a good Query Execution Plan. The structure [&#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%2F09%2Fpostgresql-using-explain-analyze-know-your-query-execution-plan%2F&amp;linkname=PostgreSQL%3A%20Using%20EXPLAIN%20ANALYZE%2C%20Know%20your%20Query%20Execution%20Plan" 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%2F09%2Fpostgresql-using-explain-analyze-know-your-query-execution-plan%2F&amp;linkname=PostgreSQL%3A%20Using%20EXPLAIN%20ANALYZE%2C%20Know%20your%20Query%20Execution%20Plan" 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%2F09%2Fpostgresql-using-explain-analyze-know-your-query-execution-plan%2F&amp;linkname=PostgreSQL%3A%20Using%20EXPLAIN%20ANALYZE%2C%20Know%20your%20Query%20Execution%20Plan" 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%2F09%2Fpostgresql-using-explain-analyze-know-your-query-execution-plan%2F&amp;linkname=PostgreSQL%3A%20Using%20EXPLAIN%20ANALYZE%2C%20Know%20your%20Query%20Execution%20Plan" 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%2F09%2Fpostgresql-using-explain-analyze-know-your-query-execution-plan%2F&amp;linkname=PostgreSQL%3A%20Using%20EXPLAIN%20ANALYZE%2C%20Know%20your%20Query%20Execution%20Plan" 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%2F09%2Fpostgresql-using-explain-analyze-know-your-query-execution-plan%2F&amp;linkname=PostgreSQL%3A%20Using%20EXPLAIN%20ANALYZE%2C%20Know%20your%20Query%20Execution%20Plan" 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%2F09%2Fpostgresql-using-explain-analyze-know-your-query-execution-plan%2F&#038;title=PostgreSQL%3A%20Using%20EXPLAIN%20ANALYZE%2C%20Know%20your%20Query%20Execution%20Plan" data-a2a-url="https://www.dbrnd.com/2016/09/postgresql-using-explain-analyze-know-your-query-execution-plan/" data-a2a-title="PostgreSQL: Using EXPLAIN ANALYZE, Know your Query Execution Plan"></a></p><p>Like any other RDBMS, PostgreSQL generates a Query Execution Plan for each query that it receives. </p>
<blockquote><p>It is also used Genetic Query Optimizer (GEQO) that does query planning for heuristic search and It reduce query planning time for large and complex queries. </p></blockquote>
<p>An Optimizer always tries to choose a good Query Execution Plan.</p>
<blockquote><p>The structure of Query Execution Plan is like a Tree and It contains different information like: type of scan details, join details, sorting details, aggregation details, Estimated rows details, Estimated start-up cost, Estimated total cost and others.</p></blockquote>
<p>To know about the Query Execution Plan, is very important for all Database Professionals because It helps us to improve the Query Performance. </p>
<p>With the help of Query Execution Plan, We can make our decisions on Index tuning, Join tuning and other. </p>
<blockquote><p>In PostgreSQL, we have EXPLAIN and EXPLAIN ANALYZE command to know about the Query Execution Plan.</p></blockquote>
<p>Here, I have used one table tbl_itemtransactions which you can create using below post.</p>
<p><strong>Example of EXPLAIN: </strong>It generates the Estimated Query Execution Plan with out executing an actual SQL Query. </p>
<pre class="crayon-plain-tag">EXPLAIN SELECT COUNT(1) FROM tbl_itemtransactions;

/*
Result: 
"Aggregate  (cost=2555525.00..2555525.01 rows=1 width=0)"
"  -&gt;  Seq Scan on tbl_itemtransactions  
(cost=0.00..2216945.00 rows=135432000 width=0)"
*/</pre> </p>
<p><strong>Example of EXPLAIN ANALYZE:</strong> It first executes the SQL Query and than It generates the actual Query Execution Plan. </p>
<pre class="crayon-plain-tag">EXPLAIN ANALYZE SELECT COUNT(1) FROM tbl_itemtransactions;

/*
Result: 
"Aggregate  (cost=2555525.00..2555525.01 rows=1 width=0) 
(actual time=52660.132..52660.133 rows=1 loops=1)"
"  -&gt;  Seq Scan on tbl_itemtransactions  
(cost=0.00..2216945.00 rows=135432000 width=0) 
(actual time=0.027..46193.484 rows=135432001 loops=1)"
"Planning time: 0.204 ms"
"Execution time: 52660.237 ms"
*/</pre> </p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
