Query genereated by Entity Framework using PatIndex not working - entity-framework-5

I have a query generated by EF5 code where I'm using SqlFunctions.PatIndex and the query takes 5 minutes to run. If I make a change to the SQL to use like operator instead then it works sub-second time. From what I've read PatIndex is supposed to work at least as well as the like operator. This appears to be some interaction with the row_number() OVER portion because if you execute just the innermost select with the patindex clause it comes back in sub-second time as well. Can anyone tell me why the row_number() OVER is killing the PATINDEX query but not the one using like?
This one works:
exec sp_executesql N'SELECT TOP (10)
[Project1].[C1] AS [C1],
[Project1].[PersonId] AS [PersonId],
[Project1].[FirstName] AS [FirstName],
[Project1].[MiddleName] AS [MiddleName],
[Project1].[LastName] AS [LastName],
[Project1].[Suffix] AS [Suffix],
[Project1].[DateOfBirth] AS [DateOfBirth],
[Project1].[Ssn] AS [Ssn],
[Project1].[CenterNumber] AS [CenterNumber],
[Project1].[GuarantorNumber] AS [GuarantorNumber],
[Project1].[GUNumber] AS [GUNumber],
[Project1].[IndustrialClientNumber] AS [IndustrialClientNumber],
[Project1].[Employer] AS [Employer]
FROM ( SELECT [Project1].[PersonId] AS [PersonId], [Project1].[CenterNumber] AS [CenterNumber], [Project1].[GuarantorNumber] AS [GuarantorNumber], [Project1].[GUNumber] AS [GUNumber], [Project1].[IndustrialClientNumber] AS [IndustrialClientNumber], [Project1].[Employer] AS [Employer], [Project1].[FirstName] AS [FirstName], [Project1].[MiddleName] AS [MiddleName], [Project1].[LastName] AS [LastName], [Project1].[Suffix] AS [Suffix], [Project1].[DateOfBirth] AS [DateOfBirth], [Project1].[Ssn] AS [Ssn], [Project1].[C1] AS [C1], row_number() OVER (ORDER BY [Project1].[FirstName] ASC) AS [row_number]
FROM ( SELECT
[Extent1].[PersonId] AS [PersonId],
[Extent1].[CenterNumber] AS [CenterNumber],
[Extent1].[GuarantorNumber] AS [GuarantorNumber],
[Extent1].[GUNumber] AS [GUNumber],
[Extent1].[IndustrialClientNumber] AS [IndustrialClientNumber],
[Extent1].[Employer] AS [Employer],
[Extent2].[FirstName] AS [FirstName],
[Extent2].[MiddleName] AS [MiddleName],
[Extent2].[LastName] AS [LastName],
[Extent2].[Suffix] AS [Suffix],
[Extent2].[DateOfBirth] AS [DateOfBirth],
[Extent2].[Ssn] AS [Ssn],
''0X0X'' AS [C1]
FROM [dbo].[vGuarantor] AS [Extent1]
INNER JOIN [dbo].[vPerson] AS [Extent2] ON [Extent1].[PersonId] = [Extent2].[PersonId]
WHERE ([Extent1].[CenterNumber] = #p__linq__0) AND ***([Extent1].[GuarantorNumber] like #p__linq__1)***
) AS [Project1]
) AS [Project1]
WHERE [Project1].[row_number] > 0
ORDER BY [Project1].[FirstName] ASC
',N'#p__linq__0 nvarchar(4000),#p__linq__1 nvarchar(4000)',#p__linq__0=N'2',#p__linq__1=N'13100%'
This one doesn’t:
exec sp_executesql N'SELECT TOP (10)
[Project1].[C1] AS [C1],
[Project1].[PersonId] AS [PersonId],
[Project1].[FirstName] AS [FirstName],
[Project1].[MiddleName] AS [MiddleName],
[Project1].[LastName] AS [LastName],
[Project1].[Suffix] AS [Suffix],
[Project1].[DateOfBirth] AS [DateOfBirth],
[Project1].[Ssn] AS [Ssn],
[Project1].[CenterNumber] AS [CenterNumber],
[Project1].[GuarantorNumber] AS [GuarantorNumber],
[Project1].[GUNumber] AS [GUNumber],
[Project1].[IndustrialClientNumber] AS [IndustrialClientNumber],
[Project1].[Employer] AS [Employer]
FROM ( SELECT [Project1].[PersonId] AS [PersonId], [Project1].[CenterNumber] AS [CenterNumber], [Project1].[GuarantorNumber] AS [GuarantorNumber], [Project1].[GUNumber] AS [GUNumber], [Project1].[IndustrialClientNumber] AS [IndustrialClientNumber], [Project1].[Employer] AS [Employer], [Project1].[FirstName] AS [FirstName], [Project1].[MiddleName] AS [MiddleName], [Project1].[LastName] AS [LastName], [Project1].[Suffix] AS [Suffix], [Project1].[DateOfBirth] AS [DateOfBirth], [Project1].[Ssn] AS [Ssn], [Project1].[C1] AS [C1], row_number() OVER (ORDER BY [Project1].[FirstName] ASC) AS [row_number]
FROM ( SELECT
[Extent1].[PersonId] AS [PersonId],
[Extent1].[CenterNumber] AS [CenterNumber],
[Extent1].[GuarantorNumber] AS [GuarantorNumber],
[Extent1].[GUNumber] AS [GUNumber],
[Extent1].[IndustrialClientNumber] AS [IndustrialClientNumber],
[Extent1].[Employer] AS [Employer],
[Extent2].[FirstName] AS [FirstName],
[Extent2].[MiddleName] AS [MiddleName],
[Extent2].[LastName] AS [LastName],
[Extent2].[Suffix] AS [Suffix],
[Extent2].[DateOfBirth] AS [DateOfBirth],
[Extent2].[Ssn] AS [Ssn],
''0X0X'' AS [C1]
FROM [dbo].[vGuarantor] AS [Extent1]
INNER JOIN [dbo].[vPerson] AS [Extent2] ON [Extent1].[PersonId] = [Extent2].[PersonId]
WHERE ([Extent1].[CenterNumber] = #p__linq__0) AND ***(PATINDEX(#p__linq__1, [Extent1].[GuarantorNumber]) > 0)***
) AS [Project1]
) AS [Project1]
WHERE [Project1].[row_number] > 0
ORDER BY [Project1].[FirstName] ASC
',N'#p__linq__0 nvarchar(4000),#p__linq__1 nvarchar(4000)',#p__linq__0=N'2',#p__linq__1=N'13100%'
Here are the two tables:
/****** Object: Table [Person] Script Date: 5/30/2013 8:17:15 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [Person](
[PersonId] [uniqueidentifier] NOT NULL,
[FirstName] [nvarchar](255) NULL,
[MiddleName] [nvarchar](255) NULL,
[LastName] [nvarchar](255) NULL,
[DateOfBirth] [datetime] NULL,
[DateOfBirthIsGuess] [bit] NULL,
[NickName] [nvarchar](255) NULL,
[Suffix] [nchar](10) NULL,
[Gender] [char](1) NULL,
[SSN] [numeric](9, 0) NULL,
CONSTRAINT [PK__Person__04E4BC85] PRIMARY KEY CLUSTERED
(
[PersonId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 95) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object: Index [PK__Person__04E4BC85] Script Date: 7/18/2013 8:19:16 AM ******/
ALTER TABLE [PatientFirst].[Person] ADD CONSTRAINT [PK__Person__04E4BC85] PRIMARY KEY CLUSTERED
(
[PersonId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 95) ON [PRIMARY]
GO
/****** Object: Index [IDX_Person.LastName] Script Date: 7/18/2013 8:19:24 AM ******/
CREATE NONCLUSTERED INDEX [IDX_Person.LastName] ON [PatientFirst].[Person]
(
[LastName] ASC
)
INCLUDE ( [DateOfBirth],
[FirstName],
[MiddleName],
[PersonId],
[SSN],
[Suffix]) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
/****** Object: Index [IDX_Person.FirstName] Script Date: 7/18/2013 8:19:37 AM ******/
CREATE NONCLUSTERED INDEX [IDX_Person.FirstName] ON [PatientFirst].[Person]
(
[FirstName] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
/****** Object: Index [IDX_Person.DOB] Script Date: 7/18/2013 8:19:47 AM ******/
CREATE NONCLUSTERED INDEX [IDX_Person.DOB] ON [PatientFirst].[Person]
(
[DateOfBirth] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
/****** Object: Table [Guarantor] Script Date: 5/30/2013 8:17:11 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [Guarantor](
[GuarantorId] [uniqueidentifier] NOT NULL,
[CenterNumber] [nvarchar](10) NOT NULL,
[GuarantorNumber] [nvarchar](10) NOT NULL,
[IndustrialClientNumber] [int] NULL,
[Employer] [varchar](4000) NULL,
[ImportDate] [datetime] NULL,
[GUNumber] AS (([CenterNumber]+'*')+[GuarantorNumber]),
CONSTRAINT [PK__Guarantor__44FF419A] PRIMARY KEY CLUSTERED
(
[GuarantorId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 95) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
ALTER TABLE [Guarantor] ADD CONSTRAINT [DF__Guarantor__Guara__45F365D3] DEFAULT (newid()) FOR [GuarantorId]
GO
ALTER TABLE [Guarantor] ADD CONSTRAINT [DF_Guarantor_ImportDate] DEFAULT (getdate()) FOR [ImportDate]
GO
ALTER TABLE [Guarantor] WITH CHECK ADD CONSTRAINT [FK_Guarantor_Person] FOREIGN KEY([GuarantorId])
REFERENCES [Person] ([PersonId])
GO
ALTER TABLE [Guarantor] CHECK CONSTRAINT [FK_Guarantor_Person]
GO
/****** Object: Index [PK__Guarantor__44FF419A] Script Date: 7/18/2013 8:17:43 AM ******/
ALTER TABLE [PatientFirst].[Guarantor] ADD CONSTRAINT [PK__Guarantor__44FF419A] PRIMARY KEY CLUSTERED
(
[GuarantorId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 95) ON [PRIMARY]
GO
/****** Object: Index [IDX_Guarantor_CenterNumber] Script Date: 7/18/2013 8:16:40 AM ******/
CREATE NONCLUSTERED INDEX [IDX_Guarantor_CenterNumber] ON [PatientFirst].[Guarantor]
(
[CenterNumber] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
/****** Object: Index [IDX_Guarantor_CenterNumberGuarantorNumber] Script Date: 7/18/2013 8:16:45 AM ******/
CREATE UNIQUE NONCLUSTERED INDEX [IDX_Guarantor_CenterNumberGuarantorNumber] ON [PatientFirst].[Guarantor]
(
[CenterNumber] ASC,
[GuarantorNumber] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 95) ON [PRIMARY]
GO
/****** Object: Index [IDX_Guarantor_GuarantorNumber] Script Date: 7/18/2013 8:17:01 AM ******/
CREATE NONCLUSTERED INDEX [IDX_Guarantor_GuarantorNumber] ON [PatientFirst].[Guarantor]
(
[GuarantorNumber] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
/****** Object: Index [IDX_Guarantor_GUNumber] Script Date: 7/18/2013 8:17:09 AM ******/
CREATE NONCLUSTERED INDEX [IDX_Guarantor_GUNumber] ON [PatientFirst].[Guarantor]
(
[GUNumber] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 95) ON [PRIMARY]
GO
/****** Object: Index [IDX_Guarantor_ImportDate] Script Date: 7/18/2013 8:17:21 AM ******/
CREATE NONCLUSTERED INDEX [IDX_Guarantor_ImportDate] ON [PatientFirst].[Guarantor]
(
[ImportDate] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 95) ON [PRIMARY]
GO
/****** Object: Index [IDX_Guarantor_IndustrialClientNumber] Script Date: 7/18/2013 8:17:30 AM ******/
CREATE NONCLUSTERED INDEX [IDX_Guarantor_IndustrialClientNumber] ON [PatientFirst].[Guarantor]
(
[IndustrialClientNumber] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 95) ON [PRIMARY]
GO
The views used in the queries are just select * from the underlying tables.
Execution Plan for the like operator:
https://docs.google.com/file/d/0B4uS27e7ZMfCOTl5LS1UaHBSOTQ/edit?usp=sharing
Patindex plan:
https://docs.google.com/file/d/0B4uS27e7ZMfCVTljNml4R0xjT28/edit?usp=sharing

I suspect the performance of both queries for a "contains" search (eg: %3%00%) will be equally bad. However, if the majority of your searches include a prefix (eg: 131%00%), you might be able to improve the performance by adding a StartsWith condition.
For example:
var query = context.Guarantors.Where(g => g.CenterNumber == centerNumber);
int index = guarantorNumber.IndexOfAny(new[]{ '%', '_', '[' });
switch (index)
{
case -1:
{
query = query.Where(g => g.GuarantorNumber == guarantorNumber);
break;
}
case 0:
{
// Nothing we can do here. The query performance will suck.
query = query.Where(g => SqlFunctions.PatIndex(guarantorNumber, g.GuarantorNumber) > 0);
break;
}
default:
{
string prefix = guarantorNumber.Substring(0, index);
query = query.Where(g => g.GuarantorNumber.StartsWith(prefix));
if (index != guarantorNumber.Length - 1 || guarantorNumber[index] != '%')
{
query = query.Where(g => SqlFunctions.PatIndex(guarantorNumber, g.GuarantorNumber) > 0);
}
break;
}
}
NB: You could also try updating your statistics. The execution plan is showing a significant difference between the estimated and actual rows for both tables.
Guarantor: Estimated 7188, actual 107345
Person: Estimated 7102, actual 6858694
From the huge difference, I'm guessing you have the Auto Update Statistics option turned off, and you haven't scheduled a job to manually update the statistics.
Try the following commands:
UPDATE STATISTICS Person;
UPDATE STATISTICS Guarantor;
That should give SQL enough information to choose a much saner execution plan.
UPDATE STATISTICS topic on MSDN

Related

Could not add table 'SELECT('

Error given when adding query that runs in SQL developer but not in MS Query. Seems to not like my nested query.
Code I am using:
SELECT ORDER_DATE
,SALES_ORDER_NO
,CUSTOMER_PO_NUMBER
,DELIVER_TO
,STATUS
,ITEM_NUMBER
,DESCRIPTION
,ORD_QTY
,SUM(QUANTITY) AS ON_HAND
,PACKAGE_ID
,PACKAGE_STATUS
,MAX(TRAN_DATE) AS LAST_TRANSACTION
,MIN(DAYS) AS DAYS
FROM (
SELECT TRUNC(SH.ORDER_DATE) AS ORDER_DATE
,SH.SALES_ORDER_NO
,SH.CUSTOMER_PO_NUMBER
,SH.SHIP_CODE AS DELIVER_TO
,SH.STATUS
,SB.ITEM_NUMBER
,IM.DESCRIPTION
,SB.ORD_QTY
,BID.QUANTITY
,SPM.PACKAGE_ID
,CASE
WHEN SPM.SHIPPED = 'Y'
THEN 'SHIPPED'
WHEN SPM.STATUS = 'C'
THEN 'PACKED'
WHEN BID.QUANTITY IS NOT NULL
THEN 'AVAILABLE'
WHEN BID.QUANTITY IS NULL
THEN 'UNAVAILABLE'
END AS PACKAGE_STATUS
,CASE
WHEN SPM.SHIPPED = 'Y'
THEN TRUNC(SPM.BILLING_DATE)
WHEN SPM.STATUS = 'C'
THEN TRUNC(SPM.END_TIME)
WHEN BID.QUANTITY IS NOT NULL
THEN TRUNC(BID.ACTIVATION_TIME)
END AS TRAN_DATE
,CASE
WHEN SPM.SHIPPED = 'Y'
THEN ROUND(SYSDATE - SPM.BILLING_DATE, 0)
WHEN SPM.STATUS = 'C'
THEN ROUND(SYSDATE - SPM.END_TIME, 0)
WHEN BID.QUANTITY IS NOT NULL
THEN ROUND(SYSDATE - BID.ACTIVATION_TIME, 0)
END AS DAYS
FROM SO_HEADER SH
LEFT JOIN SO_BODY SB ON SB.SO_HEADER_TAG = SH.SO_HEADER_TAG
LEFT JOIN SO_PACKAGE_MASTER SPM ON SPM.PACKAGE_ID = SB.PACKAGE_ID
LEFT JOIN ITEM_MASTER IM ON IM.ITEM_NUMBER = SB.ITEM_NUMBER
LEFT JOIN V_BIN_ITEM_DETAIL BID ON BID.ITEM_NUMBER = SB.ITEM_NUMBER
WHERE SH.ORDER_TYPE = 'MSR'
AND (
SB.REASON_CODE IS NULL
OR SB.REASON_CODE NOT LIKE 'CANCEL%'
)
AND (
IM.DESCRIPTION NOT LIKE '%CKV%'
OR IM.DESCRIPTION IS NULL
AND IM.ITEM_NUMBER IS NOT NULL
)
)
WHERE PACKAGE_STATUS <> 'SHIPPED'
GROUP BY ORDER_DATE
,SALES_ORDER_NO
,CUSTOMER_PO_NUMBER
,DELIVER_TO
,STATUS
,ITEM_NUMBER
,DESCRIPTION
,ORD_QTY
,PACKAGE_ID
,PACKAGE_STATUS
ORDER BY (
CASE PACKAGE_STATUS
WHEN 'AVAILABLE'
THEN 1
WHEN 'UNAVAILABLE'
THEN 2
WHEN 'PACKED'
THEN 3
END
)
,LAST_TRANSACTION;
Is there any option I can select that will allow me to run this query?

timeout when running report in Acumatica ERP system

I have a problem when I'm running customized report,
I used this query View to generate the report: https://drive.google.com/open?id=0B0Aenr4I_Yz9RWRSN0RtaXhoSG8
After Executing finished, report doesn't work and show the timeout error.
and then I tried to trace this query in the system, and get this query:
SELECT vTranPeriodMultiCury.[BatchNbr], vTranPeriodMultiCury.[RefNbr], vTranPeriodMultiCury.[InvoiceNbr], vTranPeriodMultiCury.[JobOrderNbr], vTranPeriodMultiCury.[Customer_Vendor_ID], vTranPeriodMultiCury.[SourceCredit], vTranPeriodMultiCury.[SourceDebit], vTranPeriodMultiCury.[BaseCredit], vTranPeriodMultiCury.[BaseDebit], vTranPeriodMultiCury.[Module], vTranPeriodMultiCury.[TranDate], vTranPeriodMultiCury.[TranType], vTranPeriodMultiCury.[Customer_Vendor_CD], vTranPeriodMultiCury.[Customer_Vendor_Name], vTranPeriodMultiCury.[TranDesc], vTranPeriodMultiCury.[Curyid], vTranPeriodMultiCury.[CuryRate], vTranPeriodMultiCury.[FinPeriodID], vTranPeriodMultiCury.[AccountCD], vTranPeriodMultiCury.[AccountDesc], vTranPeriodMultiCury.[BaseBegBalSummary], vTranPeriodMultiCury.[BaseEndBalSummary], vTranPeriodMultiCury.[SourceBegBalSummary], vTranPeriodMultiCury.[SourceEndBalSummary], vTranPeriodMultiCury.[BaseBegBalIDR], vTranPeriodMultiCury.[BaseEndBalIDR], vTranPeriodMultiCury.[SourceBegBalIDR], vTranPeriodMultiCury.[SourceEndBalIDR], vTranPeriodMultiCury.[BaseBegBalJPY], vTranPeriodMultiCury.[BaseEndBalJPY], vTranPeriodMultiCury.[SourceBegBalJPY], vTranPeriodMultiCury.[SourceEndBalJPY], vTranPeriodMultiCury.[BaseBegBalUSD], vTranPeriodMultiCury.[BaseEndBalUSD], vTranPeriodMultiCury.[SourceBegBalUSD], vTranPeriodMultiCury.[SourceEndBalUSD], Account.[AccountCD], Account.[Type], Account.[NoteID], NULL, NULL, NULL, Batch.[Module], Batch.[BatchNbr], Batch.[CuryInfoID], Batch.[NoteID], NULL, NULL, NULL FROM vTranPeriodMultiCury vTranPeriodMultiCury LEFT JOIN Account Account ON (Account.CompanyID = 2) AND [Account].[DeletedDatabaseRecord] = 0 AND (vTranPeriodMultiCury.[AccountID] = Account.[AccountID]) LEFT JOIN Batch Batch ON (Batch.CompanyID = 2) AND [Batch].[DeletedDatabaseRecord] = 0 AND (vTranPeriodMultiCury.[BatchNbr] = Batch.[BatchNbr]) WHERE (vTranPeriodMultiCury.CompanyID = 2) AND (vTranPeriodMultiCury.BranchID IS NULL OR vTranPeriodMultiCury.BranchID IN (1, 2, 3)) AND (((vTranPeriodMultiCury.[AccountCD] >= NULL OR NULL IS NULL ) AND (vTranPeriodMultiCury.[AccountCD] <= NULL OR NULL IS NULL ) AND (vTranPeriodMultiCury.[FinPeriodID] = '052017' OR '052017' IS NULL ))) ORDER BY vTranPeriodMultiCury.[AccountCD] ASC, vTranPeriodMultiCury.[Curyid] ASC, vTranPeriodMultiCury.[FinPeriodID] ASC, vTranPeriodMultiCury.[TranDate] ASC, vTranPeriodMultiCury.[BatchNbr] ASC OPTION(OPTIMIZE FOR UNKNOWN)
My question is in which part of web.config should be possible to change the timeout of executing sql query of report designer ?
Probably you should increase this executionTimeOut in Location->system.web->httpRuntime in web.config:
<httpRuntime executionTimeout="300" requestValidationMode="2.0" maxRequestLength="1048576" />
Also try to execute the Following SQL Query on your SQL Server for your Acumatica ERP's database:
use YOUR_DATABASE_NAME
go
exec sp_configure 'remote query timeout (s)',6000
RECONFIGURE WITH OVERRIDE;

Linq to Entities, Operation timeout when I try to use group .. by

I have the following code:
var statList = (from i in _dbContext.Screenshots
where EntityFunctions.TruncateTime(i.dateTimeServer.Value) >= startDate && EntityFunctions.TruncateTime(i.dateTimeServer.Value) <= endDate
group i by EntityFunctions.TruncateTime(i.dateTimeServer.Value)
into g
select new ScreenshotStatistic()
{
Date = g.Key.Value,
AllScreenshots = g.Count(),
ScreenshotsNoSilent = g.Where(p => p.version.IndexOf("silent") == 0).Count(),
ScreenshotsNoSilentWithViews = g.Where(p => p.version.IndexOf("silent") == 0 && p.viewsPage + p.viewsOriginal > 0).Count(),
ScreenshotsOnlySilent = g.Where(p => p.version.IndexOf("silent") >= 0).Count(),
ScreenshotsOnlySilentWithViews = g.Where(p => p.version.IndexOf("silent") >= 0 && p.viewsPage + p.viewsOriginal > 0).Count(),
ScreenshotsOnlyUploadViaSite = g.Where(p => p.version.IndexOf("UPLOAD_VIA_SITE") >= 0).Count(),
ScreenshotsOnlyUploadViaSiteWithViews = g.Where(p => p.version.IndexOf("UPLOAD_VIA_SITE") >= 0 && p.viewsPage + p.viewsOriginal > 0).Count()
}).ToList();
It works carefully for my local database, but I get "Operation timeout" when I try to connect to SQL Azure. As I understand, my request is not optimized. How can I do the request better?
table has the following structure:
CREATE TABLE [dbo].[Screenshots](
[id] [int] IDENTITY(1,1) NOT NULL,
[dateTimeClient] [datetime] NOT NULL,
[name] [nvarchar](500) NOT NULL,
[username] [varchar](50) NULL,
[filename] [nvarchar](50) NULL,
[description] [nvarchar](500) NULL,
[version] [varchar](50) NULL,
[lang] [varchar](50) NULL,
[dateTimeServer] [datetime] NULL CONSTRAINT [DF_Screenshots_dateTimeServer] DEFAULT (getdate()),
[isPublic] [bit] NOT NULL CONSTRAINT [DF_Screenshots_isPublic] DEFAULT ((0)),
[viewsPage] [int] NOT NULL CONSTRAINT [DF_Screenshots_viewsPage_1] DEFAULT ((0)),
[viewsThumb] [int] NOT NULL CONSTRAINT [DF_Screenshots_viewsThumb_1] DEFAULT ((0)),
[viewsOriginal] [int] NOT NULL CONSTRAINT [DF_Screenshots_viewsOriginal_1] DEFAULT ((0)),
[statusID] [int] NOT NULL,
CONSTRAINT [PK_Screenshots] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
ALTER TABLE [dbo].[Screenshots] WITH CHECK ADD CONSTRAINT [FK_Screenshots_ScreenshotStatuses] FOREIGN KEY([statusID])
REFERENCES [dbo].[ScreenshotStatuses] ([ID])
ON UPDATE CASCADE
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[Screenshots] CHECK CONSTRAINT [FK_Screenshots_ScreenshotStatuses]
GO
EntityFunctions.TruncateTime is being translated into:
convert (datetime2, convert(varchar(255), [dateTimeServer], 102) , 102)
This is causing your query to be non-sargable, resulting in slow performance and timeout. I recommend making the following changes
First off, I do not see why you need to truncate the time in your where clause. I suggest you make this change:
where i.dateTimeServer.Value >= startDate && i.dateTimeServer.Value <= endDate
This will prevent the database from having to run the function on every record.
If the query still times out, I would change the group by to:
group i by new {
i.dateTimeServer.Value.Year,
i.dateTimeServer.Value.Month,
i.dateTimeServer.Value.Day
}
Functionally this is the same, but I believe it produces a more friendly translation:
DATEPART (year, dateTimeServer), DATEPART (month, dateTimeServer) etc..
Add an index to dateTimeServer
If all else fails, you will need to add a column to your table with the time portion truncated. Index it and use it in your query.

SubSonic 3 ignoring columns in Select()

I have a table like so..
CREATE TABLE [dbo].[Locations_Hours](
[LocationID] [int] NOT NULL,
[sun_open] [nvarchar](10) NULL,
[sun_close] [nvarchar](10) NULL,
[mon_open] [nvarchar](10) NULL,
[mon_close] [nvarchar](10) NULL,
[tue_open] [nvarchar](10) NULL,
[tue_close] [nvarchar](10) NULL,
[wed_open] [nvarchar](10) NULL,
[wed_close] [nvarchar](10) NULL,
[thu_open] [nvarchar](10) NULL,
[thu_close] [nvarchar](10) NULL,
[fri_open] [nvarchar](10) NULL,
[fri_close] [nvarchar](10) NULL,
[sat_open] [nvarchar](10) NULL,
[sat_close] [nvarchar](10) NULL,
[StoreNumber] [int] NULL,
[LocationHourID] [int] IDENTITY(1,1) NOT NULL,
CONSTRAINT [PK_Locations_Hours] PRIMARY KEY CLUSTERED
(
[LocationHourID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
And SubSonic 3 is generating a class with the following properties
int LocationID
string monopen
string monclose
string tueopen
string tueclose
string wedopen
string wedclose
string thuopen
string thuclose
string friopen
string friclose
string satopen
string satclose
string sunopen
string sunclose
int? StoreNumber
int LocationHourID
When I try to perform a query against this class like so..
var result = DB.LocationHours.Where(o => o.LocationID == _locationId);
This is the resulting SQL query that SubSonic generates.
SELECT [t0].[LocationHourID], [t0].[LocationID], [t0].[StoreNumber]
FROM [dbo].[Locations_Hours] AS t0
WHERE ([t0].[LocationID] = 4019)
I cannot figure out why SubSonic is omitting the nvarchar fields when it generates the SELECT statement. Anyone got any ideas?
I wasn't ever able to solve this problem. I ended up just executing a plain old DataReader and 'manually' populated my objects.
If anyone comes along later with an answer, I will change the accepted answer.

Subsonic2.2 and NEWSEQUENTIALID() Primary Key Column

Subsonic is returning 00000000-0000-0000-0000-000000000000 when I insert a record and try to get it's key after insert.
product.Save();
GUID = product.ProdID;
The record is inserted correctly with correct GUIDs.
Any idea on how to resolve this? I am using version 2.2.0.0
This is my table schema
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[ISA_810_ControlTracking](
[ISAID] [uniqueidentifier] ROWGUIDCOL NOT NULL CONSTRAINT [DF_ISA_810_ControlTracking_ISAID] DEFAULT (newsequentialid()),
[ISA000_01_Authorization_Information_Qualifier] [varchar](2) NOT NULL,
[ISA000_02_Authorization_Information] [varchar](10) NOT NULL,
[ISA000_03_Security_Information_Qualifier] [varchar](2) NOT NULL,
[ISA000_04_Security_Information] [varchar](10) NOT NULL,
[ISA000_05_Interchange_Id_Qualifier] [varchar](2) NOT NULL,
[ISA000_06_Interchange_Sender_Id] [varchar](15) NOT NULL,
[ISA000_07_Interchange_Id_Qualifier] [varchar](2) NOT NULL,
[ISA000_08_Interchange_Receiver_Id] [varchar](15) NOT NULL,
[ISA000_09_Interchange_Date] [datetime] NOT NULL,
[ISA000_10_Interchange_Time] [datetime] NOT NULL,
[ISA000_11_Interchange_Control_Standards_Identifier] [varchar](1) NOT NULL,
[ISA000_12_Interchange_Control_Version_Number] [varchar](5) NOT NULL,
[ISA000_13_Interchange_Control_Number] [int] NOT NULL,
[ISA000_14_Acknowledgment_Requested] [varchar](1) NOT NULL,
[ISA000_15_Usage_Indicator] [varchar](1) NOT NULL,
[ISA000_16_Component_Element_Separator] [varchar](1) NOT NULL,
[IEA000_01_Number_Of_Included_Functional_Groups] [int] NOT NULL,
[IEA000_02_Interchange_Control_Number] [int] NOT NULL,
CONSTRAINT [PK_ISA_810_ControlTrackingIndex] PRIMARY KEY CLUSTERED
(
[ISAID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY],
CONSTRAINT [IX_ISA_810_ControlTracking] UNIQUE NONCLUSTERED
(
[ISA000_06_Interchange_Sender_Id] ASC,
[ISA000_08_Interchange_Receiver_Id] ASC,
[ISA000_13_Interchange_Control_Number] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
Unlike IDENTITY types, applications have no way of determining the generated GUID on insert. While this is possible in T-SQL by using OUTPUT clause: INSERT ... OUTPUT inserted.$ROWGUIDCOL VALUES(...) most ORMs won't know how to do this. Given that the guid is a guid and doesn't matter who generates it, I'd recommend you generate it in the client prior of saving a new record, using UuidCreateSequential.

Resources