Solved

optimize query

Posted on 2014-10-08
8
75 Views
Last Modified: 2014-10-27
Hello,

How can I optimize this query :
Hello,

How can I optimize this query :
SELECT F.ExpDate, IF.ReferenceId, C.CategoryName, IF.Label, IF.Quantity, IF.Amo*ABS(IF.Quantity) MHT, IF.TotalFactItemAmount*ABS(IF.Quantity) MontantTTC, ClientId
FROM Fact I (NOLOCK)
INNER JOIN T_FactItems IF (NOLOCK)
ON IF.FactId = F.FactId
INNER JOIN LNK_SQL02.[VFR].[dbo].[Oper] O WITH(NOLOCK)
ON O.OperationCode = F.OperationCode
INNER JOIN LNK_SQL02.[VFR].[dbo].[T_Categories] C WITH(NOLOCK)
ON O.CategoryId = C.Id
WHERE IF.FactItemTypeRefId = 4
AND IF.IsAccountedFor = 1
AND F.ExpDate >='20140101'
AND F.ExpDate < '20141001'
AND F.OperationCode NOT LIKE 'RZD[_]%'
AND F.SiteId = 1
AND C.CategoryName IN ('J / JO','SPORTS','LIB', 'BABY')
AND (IF.Label LIKE '%DVD%'
      OR IF.Label LIKE '%HS%'
      OR IF.Label LIKE '%blu%ray%')

Without creating the missing index :

CREATE NONCLUSTERED INDEX [IX_Fact_SITEID_EXPDATE]
  ON [dbo].[T_Facts] ([siteid], [expeditiondate])
  include ([FactId], [ClientId], [OperationCode])

go

Thanks

Regards
0
Comment
Question by:bibi92
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
8 Comments
 
LVL 34

Expert Comment

by:ste5an
ID: 40369053
This is a non-sargeable expression:

	AND (IF.Label LIKE '%DVD%'
			OR IF.Label LIKE '%HS%'
			OR IF.Label LIKE '%blu%ray%')

Open in new window


Thus no index can be used at all for this condition.

And it seems you're using a linked server:

FROM	Fact I (NOLOCK)
	INNER JOIN T_FactItems IF (NOLOCK) ON IF.FactId = F.FactId
	INNER JOIN LNK_SQL02.[VFR].[dbo].[Oper] O WITH(NOLOCK) ON O.OperationCode = F.OperationCode
	INNER JOIN LNK_SQL02.[VFR].[dbo].[T_Categories] C WITH(NOLOCK) ON O.CategoryId = C.Id

Open in new window


Which also reduces the possibilities. I would create this view with appropriate indices on your linked server:

SELECT	C.CategoryName,
		O.OperationCode
FROM	[VFR].[dbo].[Oper] O WITH(NOLOCK) 
	INNER JOIN [VFR].[dbo].[T_Categories] C WITH(NOLOCK) ON O.CategoryId = C.Id
WHERE	C.CategoryName IN ('J / JO','SPORTS','LIB', 'BABY');

Open in new window


When this is not possible, use this query to fill a temporary table and use the temporary table instead of the linked server tables.
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 40369643
Just a small observation: don't use "IF" as an alias it is a reserved word

Where is the alias "F" declared?
How is [dbo].[T_Facts] related to this query?
Is the query using one or more views?
Why can't you create an index?
0
 

Author Comment

by:bibi92
ID: 40370059
Can you explain  This is a non-sargeable expression:

AND (IF.Label LIKE '%DVD%'
OR IF.Label LIKE '%HS%'
OR IF.Label LIKE '%blu%ray%')
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:bibi92
ID: 40370078
•Where is the alias "F" declared --> I have modified select statement


•How is [dbo].[T_Facts] related to this query? --> I have modFIied the create index


•Is the query using one or more views? ---> only table


•Why can't you create an index? ---> it's a VLDB prod

SELECT F.ExpDate, FI.ReferenceId, C.CategoryName, FI.Label, FI.Quantity, FI.Amo*ABS(FI.Quantity) MHT, FI.TotalFactItemAmount*ABS(FI.Quantity) MontantTTC, ClientId
FROM Fact F (NOLOCK)
INNER JOIN T_FactItems FI (NOLOCK)
ON FI.FactId = F.FactId
INNER JOIN LNK_SQL02.[VFR].[dbo].[Oper] O WITH(NOLOCK)
ON O.OperationCode = F.OperationCode
INNER JOIN LNK_SQL02.[VFR].[dbo].[T_Categories] C WITH(NOLOCK)
ON O.CategoryId = C.Id
WHERE FI.FactItemTypeRefId = 4
AND FI.IsAccountedFor = 1
AND F.ExpDate >='20140101'
AND F.ExpDate < '20141001'
AND F.OperationCode NOT LIKE 'RZD[_]%'
AND F.SiteId = 1
AND C.CategoryName IN ('J / JO','SPORTS','LIB', 'BABY')
AND (FI.Label LIKE '%DVD%'
      OR FI.Label LIKE '%HS%'
      OR FI.Label LIKE '%blu%ray%')

CREATE NONCLUSTERED INDEX [IX_Fact_SITEID_EXPDATE]
  ON [dbo].[Fact] ([siteid], [expeditiondate])
  include ([FactId], [ClientId], [OperationCode])

Thanks
0
 
LVL 48

Accepted Solution

by:
PortletPaul earned 500 total points
ID: 40370102
SARGABLE
a predicate is "sargable" if a dbms can use index(es) to help with execution efficiency

When searching for strings with wildcards on both sides it is not possible to use indexes; therefore it is not sargable

Those double sided wildcards cause a full scan of the text fields.

so, the following is a cause of slowness:

AND (IF.Label LIKE '%DVD%'
OR IF.Label LIKE '%HS%'
OR IF.Label LIKE '%blu%ray%')
0
 

Author Comment

by:bibi92
ID: 40370348
so how can I modify :
AND (IF.Label LIKE '%DVD%'
OR IF.Label LIKE '%HS%'
OR IF.Label LIKE '%blu%ray%')
0
 
LVL 34

Expert Comment

by:ste5an
ID: 40370413
We you need this kind of search: you cannot modify it as long as you use normal indices.

You may use a fulltext index, but this requires different steps to setup.

The only I see here: "Label" sounds like "Tag". So the question is why searching for patterns at all?
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 40370421
Improve the data so you avoid this type of filter.

Otherwise you could try full text indexing (but that's a lot of work/effort, and a big change).

Or, live with the performance.

There just is no magic bullet to make those double sided wildcards faster.
0

Featured Post

Why You Need a DevOps Toolchain

IT needs to deliver services with more agility and velocity. IT must roll out application features and innovations faster to keep up with customer demands, which is where a DevOps toolchain steps in. View the infographic to see why you need a DevOps toolchain.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

737 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question