我在查询下面运行,它返回大多数25条记录。但这需要将近20秒。此外,表中的记录总数还少于400,000。
SELECT * FROM Tickets
LEFT OUTER JOIN HouseAccounts ON (Tickets.lHouseAccount_ID = HouseAccounts.lAccountID)
LEFT OUTER JOIN Customers ON (Tickets.lCustomerID = Customers.lCustomerID)
LEFT OUTER JOIN Vehicles ON (Tickets.lVehicleID = Vehicles.lVehicleID)
WHERE (Tickets.sTicket_Number) NOT LIKE 'ADJ%' AND dbo.DateOnly(Tickets.dtCreated) between DATEADD(day, -60, dbo.DateOnly(GETDATE()))
and dbo.DateOnly(GETDATE()) AND (Tickets.bDeleted = 0 or Tickets.bDeleted IS NULL)
以下是Tickets表格结构
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Tickets](
[Ticket_ID] [int] IDENTITY(1,1) NOT NULL,
[lLocationID] [int] NULL,
[dtCreated] [datetime] NULL,
[dtUpdated] [datetime] NULL,
[dtIn] [datetime] NULL,
[dtOut] [datetime] NULL,
[sTicket_Number] [nvarchar](10) NULL,
[dblTotal] [float] NULL,
[dblTaxes] [float] NULL,
[dblTendered] [float] NULL,
[dblChangeDue] [float] NULL,
[bPaid] [smallint] NULL,
[bCash] [smallint] NULL,
[bCreditCard] [smallint] NULL,
[bGiftCard] [smallint] NULL,
[bHouseAccount] [smallint] NULL,
[lHouseAccount_ID] [int] NULL,
[sUserName] [nvarchar](25) NULL,
[lUserID] [int] NULL,
[lShiftNumber] [int] NULL,
[imgSignature] [image] NULL,
[sSignatureFileName] [nvarchar](25) NULL,
[sPlate] [nvarchar](10) NULL,
[sMake] [nvarchar](20) NULL,
[sCarNumber] [nvarchar](25) NULL,
[sDriverName] [nvarchar](64) NULL,
[sZipcode] [nvarchar](5) NULL,
[sAge] [nvarchar](10) NULL,
[sGender] [nvarchar](10) NULL,
[sFleetCard] [nvarchar](25) NULL,
[sFleetCardExp] [nvarchar](8) NULL,
[bCheck] [smallint] NULL,
[lVIPAccountID] [int] NULL,
[lPointsThisVisit] [float] NULL,
[lGreeterID] [int] NULL,
[lCustomerID] [int] NULL,
[lVehicleID] [int] NULL,
[lWorkOrderID] [int] NULL,
[sWorkOrderNumber] [nvarchar](8) NULL,
[sVehicleMake] [nvarchar](20) NULL,
[sVehicleColor] [nvarchar](20) NULL,
[sVehicleState] [nvarchar](2) NULL,
[sVehiclePlate] [nvarchar](9) NULL,
[sVehicleDamage] [nvarchar](100) NULL,
[sCustomerName] [nvarchar](25) NULL,
[dtReturnDate] [datetime] NULL,
[lOdometer] [int] NULL,
[sRoomNumber] [nvarchar](6) NULL,
[sSpaceNumber] [nvarchar](50) NULL,
[bExpressTicket] [smallint] NULL,
[lRateStructureId] [int] NULL,
[sRateStructure] [nvarchar](25) NULL,
[mRate] [money] NULL,
[mSurcharge] [money] NULL,
[mValidation] [money] NULL,
[mPrepaid] [money] NULL,
[mRefund] [money] NULL,
[mMisc] [money] NULL,
[bVoided] [smallint] NULL,
[bCheckedOut] [smallint] NULL,
[bClosedOut] [smallint] NULL,
[bRefunded] [smallint] NULL,
[lParkerId] [int] NULL,
[bUpdated] [smallint] NULL,
[bIndoor] [smallint] NULL,
[iTimesPrinted] [smallint] NULL,
[bAudit] [bit] NULL,
[bArchived] [bit] NULL,
[lCounterId] [int] NULL,
[bPaymentOther] [bit] NULL,
[sPaymentDescription] [nvarchar](50) NULL,
[bScanned] [bit] NULL,
[bPrinted] [bit] NULL,
[bReversed] [bit] NULL,
[sCashierTerminal] [nvarchar](50) NULL,
[sGreeterTerminal] [nvarchar](50) NULL,
[bLocked] [bit] NULL,
[bWash] [bit] NULL,
[bDeleted] [bit] NULL,
[sDeletedBy] [nvarchar](125) NULL,
[dtClosed] [datetime] NULL,
[lCloserId] [int] NULL,
[lCloserId2] [int] NULL,
[bBarcodeScanned] [bit] NULL,
CONSTRAINT [aaaaaTickets_PK] PRIMARY KEY NONCLUSTERED
(
[Ticket_ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 65) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
ALTER TABLE [dbo].[Tickets] ADD CONSTRAINT [DF__Tickets__dblTota__014935CB] DEFAULT ((0)) FOR [dblTotal]
GO
ALTER TABLE [dbo].[Tickets] ADD CONSTRAINT [DF__Tickets__bPaid__023D5A04] DEFAULT ((0)) FOR [bPaid]
GO
ALTER TABLE [dbo].[Tickets] ADD CONSTRAINT [DF__Tickets__bCash__03317E3D] DEFAULT ((0)) FOR [bCash]
GO
ALTER TABLE [dbo].[Tickets] ADD CONSTRAINT [DF__Tickets__bCredit__0425A276] DEFAULT ((0)) FOR [bCreditCard]
GO
ALTER TABLE [dbo].[Tickets] ADD CONSTRAINT [DF__Tickets__bGiftCa__0519C6AF] DEFAULT ((0)) FOR [bGiftCard]
GO
而且,这是索引和执行计划 -
我已经尝试过使用Update Statistics和更新索引。然而,没有任何帮助。请建议,我该如何提高查询的性能。
答案 0 :(得分:0)
如果可能,您应该避免使用标量和多语句UDF,因为它们很慢。而且你应该在条件中避免它们,因为它们不是可以理解的。用dbo.DateOnly(
替换Convert(date,
应该会有所帮助。
我很好奇 - 未来会有门票吗?如果有,你真的想跳过它们吗?有可能,您可以使用简单between
替换Tickets.dtCreated >= convert(date, DATEADD(day, -60, GETDATE()))
。