Solved

Microsoft SQL View Slower than Dynamic access via JDBC

Posted on 2010-11-11
10
535 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 167 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
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 333 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 333 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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
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 how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

617 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