Solved

Strange SQL view, where clause, COALESCE perfomance issue

Posted on 2014-12-18
7
139 Views
Last Modified: 2014-12-28
I have a weird sql issue.
I don't have control over these SQL statements.  They are created and executed (behind the scenes) by an application (Sage CRM).
So please don't ask me to change the SQL statement because I can't and the product manufacturer (Sage CRM) says the issue is with my sql server...

The only reason I know what the sql statement looks like is because it times out and an error gets written to a log file.

This SQL statement takes 4 minutes to return 792 records -

SELECT * FROM z_vmailinglistreport WHERE
(( COALESCE(comp_type, '') = N'rescontractor' ) )
AND ((pers_secterr = -2034921070))
AND coalesce(pers_cstatus,'') = 'Active'

If I use the SELECT statement from the view and add the WHERE clause it runs in 1 second.

If I remove either one of COALESCE functions it runs in 1 second.

Any ideas?

Thanks,

Kevin.
0
Comment
Question by:kevinvw1
[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
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 500 total points
ID: 40507893
The COALESCE (or any other function) used on a column will prevent SQL from doing an index seek for that column.  Thus, even if the tables are perfectly indexed for the query, SQL won't be able to take advantage of it.

The only way to get the best performance out of such queries is remove the unnecessary COALESCE (NULL will never be =, <>, < or > any value anyway, so converting null to '' isn't needed):

WHERE
 ( comp_type = N'rescontractor'  )
 AND ((pers_secterr = -2034921070))
 AND pers_cstatus = 'Active'
0
 

Author Comment

by:kevinvw1
ID: 40507935
Thanks for the info.  

Any idea why the query runs almost instantly when I take the select statement from the underlying view and add the where clause to it (with the COALESCE) ?

Or if I add the where clause to the view it runs instantly as well.

Thanks, Kevin.
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 40507950
No, unless there are other WHERE conditions or JOINs in the view itself.
0
Independent Software Vendors: 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!

 
LVL 25

Expert Comment

by:chaau
ID: 40508205
You need to create a function based index. But there is a catch, SQL Server does not support it natively like it is in Oracle or Postgresql. There is a workaround. Create two computed column. Here is an example for one, comp_type:
ALTER TABLE z_vmailinglistreport ADD COMPUTED_comp_type AS COALESCE(comp_type, '');
-- And then create an index on the computed column:
CREATE INDEX ix_z_vmailinglistreport_COMPUTED_comp_type ON [z_vmailinglistreport](COMPUTED_comp_type);

Open in new window

BTW, tell the software vendors to stop using non-sargable queries and stop blaming you
0
 
LVL 25

Expert Comment

by:Lee Savidge
ID: 40512805
Which version and patch level of Sage CRM is this?
0
 

Author Comment

by:kevinvw1
ID: 40513359
Hi Lee, it is 7.2e

Just to clarify, the issue happens even when just running the query in SQL Server Management Studio.
So unless Sage changes the way they use the COALESCE feature I may be stuck.

I actually changed the view around to pre-filter the on pers_cstatus = "Active".
This took that criteria out of the report and resolved the issue for now.

But I'm still curious about why the view and where clause behave the way they are behaving.

Thanks,

Kevin.
0
 
LVL 25

Expert Comment

by:Lee Savidge
ID: 40513373
Believe me I know all about Sage using the coalesce function. It's caused me no end of problems in other areas of CRM. I'm guessing this is a report and the fields that CRM is having issues with are the search criteria fields in the report builder.

Are you allowing the users to choose their own values for the search criteria or are they fixed?
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
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…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

738 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