Avatar of kevinvw1
kevinvw1

asked on 

Strange SQL view, where clause, COALESCE perfomance issue

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.
Microsoft SQL ServerMicrosoft SQL Server 2008SQL

Avatar of undefined
Last Comment
Lee
ASKER CERTIFIED SOLUTION
Avatar of Scott Pletcher
Scott Pletcher
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of kevinvw1
kevinvw1

ASKER

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.
Avatar of Scott Pletcher
Scott Pletcher
Flag of United States of America image

No, unless there are other WHERE conditions or JOINs in the view itself.
Avatar of chaau
chaau
Flag of Australia image

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
Avatar of Lee
Lee
Flag of United Kingdom of Great Britain and Northern Ireland image

Which version and patch level of Sage CRM is this?
Avatar of kevinvw1
kevinvw1

ASKER

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.
Avatar of Lee
Lee
Flag of United Kingdom of Great Britain and Northern Ireland image

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?
Microsoft SQL Server
Microsoft SQL Server

Microsoft SQL Server is a suite of relational database management system (RDBMS) products providing multi-user database access functionality.SQL Server is available in multiple versions, typically identified by release year, and versions are subdivided into editions to distinguish between product functionality. Component services include integration (SSIS), reporting (SSRS), analysis (SSAS), data quality, master data, T-SQL and performance tuning.

171K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo