Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Microsoft SQL View Slower than Dynamic access via JDBC

Posted on 2010-11-11
10
Medium Priority
?
538 Views
Last Modified: 2012-05-10
Hi Experts
We have a program that access MS SQL via JDBC. When originally written there sql statment went dirrectly to the table and had a lot of fixed And conditions.
Example
where (doc Like"A%" or Doc Like 'B%' or DocLike'%C%')
I created a view that that erflects the above logic on the sql database
The question is why does the query over the view take a log longer than the query of teh table whith the dynalic slection and can I make the view quicker.

Cheers

Dave
0
Comment
Question by:daveslater
[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
10 Comments
 
LVL 6

Expert Comment

by:subhashpunia
ID: 34110272
Are you using the view with schema name?
0
 
LVL 8

Expert Comment

by:raulggonzalez
ID: 34110405
A view is exactly the same as the query which contains, so executing the query from the view and
SELECT * FROM view

should have the same time of response.

You can enhance your view's performance creating a clustered index

http://msdn.microsoft.com/en-en/library/ms188783.aspx

Good luck
0
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 668 total points
ID: 34114198
Are you saying the SELECT statement from JDBC is dynamically created based on the criteria selected and therefore different to the VIEW?  if so that would explain the difference.

Also how are you handling the parameters in the VIEW?
0
Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

 
LVL 14

Author Comment

by:daveslater
ID: 34212656
Hi sorry for the delay
the dynamic code is set as follows


 stmtv = 'Select arc_doctitle,'                  
 +' arc_docsubject, arc_docdesc2,arc_docname'    
 + ', arc_docdate1 from cpArchiveIndex a' ;      
   stmtv += '  where year(a.arc_docdate1) = ?'  
   +        '  and   month(a.arc_docdate1) = ?'  
   +        '  and   day(a.arc_docdate1) = ?' ;  

stmtv +=                                        
' or ((A.DOCDESC2 LIKE ''N/B%'') ' +            
' or  (A.DOCDESC2  LIKE ''%Quotation%'') ' +    
' or  (A.DOCDESC2  LIKE ''     over%'')  ' +    
' or  (A.DOCDESC2  LIKE ''%G/Enquiry%'') ' +    
' or  (A.DOCDESC2  LIKE ''Cancellation%'')' +    
' or  (A.DOCDESC2  LIKE ''%On Cover%'') )' +    
' and (A.DOCDESC2<> ''N/B upgrade'') ' ;        


JDBC_SetInt(stmt:1:SearchYY);  
JDBC_SetInt(stmt:2:SearchMM);  
JDBC_SetInt(stmt:3:SearchDD);  
0
 
LVL 8

Expert Comment

by:raulggonzalez
ID: 34212706
Hi,

I guess you cannot get rid of LIKE '%patterns%'

but try to modify

stmtv += '  where year(a.arc_docdate1) = ?'  
   +        '  and   month(a.arc_docdate1) = ?'  
   +        '  and   day(a.arc_docdate1) = ?' ;  

and compare it like

where a.arc_docdate1 = ?

this way, if you have any index created on the column, will be used for index seek.

Good luck.
0
 
LVL 14

Author Comment

by:daveslater
ID: 34213103
Hi raulggonzalez

I have already optimized the query to do that and it is several seconds faster.

I am just curious why a dynamic statement is faster than a view.

Dave
0
 
LVL 8

Expert Comment

by:raulggonzalez
ID: 34213135
Me too, because it shouldn't be...

A view is just a select (not indexed views) so it really shouldn't be faster or slower than the query which represents...

http://en.wikipedia.org/wiki/View_(database)

Please correct me if I'm wrong.

Cheers.
0
 
LVL 8

Assisted Solution

by:raulggonzalez
raulggonzalez earned 1332 total points
ID: 34213194
Hi,

the only thing my mind can figure out is that for some reason the execution plans are different using the select and the view. Have you checked how they are?

Cheers.
0
 
LVL 14

Author Comment

by:daveslater
ID: 34216654
how do i check the execution plan?

dave
0
 
LVL 8

Assisted Solution

by:raulggonzalez
raulggonzalez earned 1332 total points
ID: 34216808
Hi,

In SQL SERVER Management Studio you have the possibility of including the Actual Execution Plan, it's one of the buttons close to Execute.

When you execute the query, you will have a third tab 'Execution Plan'

You can have quick reference here...

http://www.simple-talk.com/sql/performance/execution-plan-basics/


Good luck.
0

Featured Post

How Blockchain Is Impacting Every Industry

Blockchain expert Alex Tapscott talks to Acronis VP Frank Jablonski about this revolutionary technology and how it's making inroads into other industries and facets of everyday life.

Question has a verified solution.

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

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how the fundamental information of how to create a table.

715 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