?
Solved

optimize query

Posted on 2014-10-08
8
Medium Priority
?
77 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 35

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 49

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
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 

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 49

Accepted Solution

by:
PortletPaul earned 2000 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 35

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 49

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

Technology Partners: 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!

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
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 combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Suggested Courses

764 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