Solved

Microsoft SQL View Slower than Dynamic access via JDBC

Posted on 2010-11-11
10
526 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
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
 
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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Need help subtracting a value within my script 7 42
tempdb latch contention 12 47
Permissions on Database 11 36
SQL Login 17 37
Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

947 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now