Solved

Microsoft SQL View Slower than Dynamic access via JDBC

Posted on 2010-11-11
10
529 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
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 
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

Microsoft Certification Exam 74-409

VeeamĀ® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

I have a large data set and a SSIS package. How can I load this file in multi threading?
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
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 to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

786 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