Slow BAQ - Execution plan shows join on Territory for some reason

,

Hello there,

I’m back with another question. I’m troubleshooting a slow BAQ. It takes around 25 seconds to execute on our database. I’ve attached the BAQ and execution plan. If I’m reading this correctly (and I might not be), a large portion of the execution time is spent looking up the TerritoryID for some reason. The BAQ makes no reference to the TerritoryID–it just counts customers by when they were added. I had to do some convoluted dateadd()'s and datediff()'s to get the YTD by month values. Any help speeding this query up would be appreciated.

Thank you

CK_NewLeadsYTD.zip (47.6 KB)

Territories add an extra layer as there are some security checks that are hardwired into E10

Like Calvin said, BAQs add extra security for Territory, Site, Company, and Tennant (for Multi-Tennant).

If you’re not using Territory security, you might be able to eliminate the clause by going to Company Configuration | Modules | Sales | CRM

image

And clearing the box.

Hi Mark. Thanks for the advice but the box is already cleared. Furthermore, all of our salespeople have “View all territories” enabled on their Work Force entries. I have uploaded a similar BAQ which pulls the values for MTD instead of YTD. I composed the MTD query first, then copied and modified it to create the YTD query. The MTD query executes in less than a second. I’m at a loss why one would get stuck on pulling TerritoryID while the other doesn’t, when the only differences are how I’m calculating the date range and how I’m joining the subquery. Is it because of the weird subquery join, where I’m joining based on an expression and a calculated field?

CK_NewLeadsMTD.baq (25.8 KB)

That very well could be. Your linked fields (destination) should be indexed otherwise it’s doing a very slow lookup.

Search the list for generalized BAQ performance tips.

1 Like

Thanks. I think I got it. I was able to speed it up drastically by creating a calculated field MonthMatch in the CTE subquery, defined as datepart(month, Customer1.CreatedDate_c) and using a matching datepart(month, Customer.CreatedDate_c) as the expression to join the CTE with the top-level subquery:

It executes in about 2 seconds now.

It would seem the convoluted dateadd/datediff to get the date of the first of the month may be what was slowing things down.

1 Like