IT tutorials
 
Technology
 

SQL Server 2012 : The Path of the Query (part 5) - Filter by Unordered Composite Index, Non-SARG-Able Expressions

11/6/2013 8:36:25 PM
- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019

Query Path 9 — Filter by Unordered Composite Index

One common indexing myth is that the order of the index key columns doesn't matter, that is, SQL Server can use an index so long as the column is anywhere in the index. Like most myths, it's a half truth.

Searching an index requires the leading index key column to be present in the search predicate. Searching for col1, col2 works great when the index includes col1 as the leading index key with col2 following it. However, searching solely for col2 without col1 in the predicate requires scanning all the leaf level data if another suitable index is not present.

Query Path 9 demonstrates the inefficiency of filtering on an index column that is not the leading index key column.

StartDate is the second key in the composite index, so the data is there. Will the query use the index?

SELECT WorkOrderID
FROM Production.WorkOrder
WHERE StartDate = ‘2006-01-04';

The query optimizer uses the IX_WorkOrder_ProductID composite nonclustered index, as shown in Figure 12, because it's narrower than the clustered index, so more rows fit on a page. Because the filter is by the second column, it can't use the index; instead SQL Server is forced to scan every row and filter (in the scan operation) to select the correct rows. Essentially, it's doing the same operation as manually scanning a telephone book for everyone with a first name of Tim.

Figure 12 Filtering by the second key column of an index forces an index scan.

45.16

Query Path 10 — Non-SARG-Able Expressions

SQL Server's Query Optimizer examines the conditions within the query's predicates to determine which indexes are useful. If SQL Server can optimize the criteria statements, such as a WHERE clause, using an index, the condition is referred to as a search argument (SARG). However, not every condition is a “SARG-able” search argument:

The final query path walks through a series of antipatterns, designing WHERE clauses with conditions that can't use index seek operations for one or more reasons. The result is an index scan, when an index seek is more advantageous. The following is a list of common types of “non-SARG-able” expressions:

  • Including the table search column in an expression forces SQL Server to evaluate the outcome of the expression for every row before it can determine if the row passes the WHERE clause criteria:
SELECT WorkOrderID
FROM Production.WorkOrder
WHERE ProductID + 2 = 759;
  • The solution to this non-SARG-able issue is to rewrite the query so that the expression is no longer dependent on the table column.
SELECT WorkOrderID
FROM Production.WorkOrder
WHERE ProductID = 759 - 2;
  • Multiple inclusive criteria is typically SARG-able; however, the optimizer may have a more difficult time creating a seekable plan with criteria composed of OR logic.
SELECT WorkOrderID, StartDate
FROM Production.WorkOrder
WHERE ProductID = 757
OR StartDate = ‘2006-01-04';
  • Negative search conditions (<>, !>, !<, Not Exists, Not In, Not Like) are not easily optimized. It's easy to prove that a row exists, but to prove it doesn't exist requires examining every row.
SELECT WorkOrderID, StartDate
FROM Production.WorkOrder
WHERE ProductID NOT IN (400,800, 950);
  • It is possible that exclusive criteria can be SARG-able, so it's worth testing. Often, it's the number of rows returned that forces a scan, not the exclusive criteria.
  • Search predicates that begin with wildcards aren't SARG-able. An index can quickly locate WorkOrderID = 757, but must scan every row to find any WorkOrderID's ending in 7:
SELECT WorkOrderID, StartDate
FROM Production.WorkOrder
WHERE WorkOrderID like ‘%7';
  • If the predicate includes a function, such as a string function, a scan is required so that every row can be evaluated with the function before the final criteria is applied to the function output.
SELECT WorkOrderID, StartDate
FROM Production.WorkOrder
WHERE DateName(dw, StartDate) = ‘Monday';

SQL Server 2008 does include some optimizations that can avoid the scan when working with the Date data type when conversions are included in the predicate

Note
The type of access (index scan versus index seek) not only impacts the performance of reading data from the single table, but it also impacts join performance. The type of join chosen by SQL Server depends on whether the data is ordered (among other things). Merge joins require ordered result sets as inputs. If the optimizer determines that a merge join is the most efficient join method to satisfy a query, a sort operation may be required to sort the inputs. In such a case, a memory grant is required and potentially tempdb space to store the intermediate result sets. This is another example of why indexing is important.
 
Others
 
- SQL Server 2012 : The Path of the Query (part 4) - Filter by 2 x NC Indexes, Filter by Ordered Composite Index
- SQL Server 2012 : The Path of the Query (part 3) - Bookmark Lookup
- SQL Server 2012 : The Path of the Query (part 2) - Range Seek Query, Filter by Nonkey Column
- SQL Server 2012 : The Path of the Query (part 1) - Fetch All, Clustered Index Seek
- SQL Server 2012 : Indexing Basics (part 2) - Index Selectivity, Query Operators
- SQL Server 2012 : Indexing Basics (part 1) - The B-Tree Index, Clustered Indexes, Nonclustered Indexes
- Windows 7 : Using Internet Explorer 8 - Using Multimedia Browsing and Downloading (part 3)
- Windows 7 : Using Internet Explorer 8 - Using Multimedia Browsing and Downloading (part 2)
- Windows 7 : Using Internet Explorer 8 - Using Multimedia Browsing and Downloading (part 1)
- Windows Server 2012 : Highly available, easy-to-manage multi-server platform - Management efficiency (part 3) - PowerShell 3.0
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
Technology FAQ
- Is possible to just to use a wireless router to extend wireless access to wireless access points?
- Ruby - Insert Struct to MySql
- how to find my Symantec pcAnywhere serial number
- About direct X / Open GL issue
- How to determine eclipse version?
- What SAN cert Exchange 2010 for UM, OA?
- How do I populate a SQL Express table from Excel file?
- code for express check out with Paypal.
- Problem with Templated User Control
- ShellExecute SW_HIDE
programming4us programming4us