I have two large tables (order and invoices) each with about 35mill rows of data in them. I am capture the latest date between the two tables and looking for the most efficent way to do this. I am thinking union-ing two 35 million row tables is not the best!
Here is my current method (that I inherited!):
----------------------------------------------------------------------------------------
insert AsOfDate
select
max(dt) AsOfDate
, year(max(dt)) YYYY
, month(max(dt)) MM
, day(max(dt)) DD
, day(dateadd(day, -1,dateadd(month, 1, dateadd(day, (day(max(dt))-1) *-1,max(dt))))) DaysInMonth from ( (select max(dtCreatedOn) dt from Invoices) union all (select max(dtCreatedOn) dt from Orders)
) q
GO
----------------------------------------------------------------------------------------
FYI- Here is the DDL for the Orders and Invoices tables:
----------------------------------------------------------------------------------------
CREATE TABLE [dbo].[Invoices](
[BillingID] [nchar](10) NOT NULL,
[LineID] [nchar](6) NOT NULL,
[AccountingID] [nchar](10) NOT NULL,
[SoldToID] [nchar](10) NULL,
[LclChnID] [nchar](3) NULL,
[dtInvoiceDate] [date] NULL,
[dtCreatedOn] [date] NULL,
[SalesOrgID] [nchar](4) NULL,
[DistChanID] [nchar](2) NULL,
[SalesDistrict] [nchar](6) NULL,
[MatID] [nchar](18) NULL,
[Qty] [decimal](17, 2) NULL,
[CasesQty] [decimal](17, 3) NULL,
[EachesQty] [decimal](17, 3) NULL,
[ParentMatID] [nchar](18) NULL,
[ParentQty] [decimal](18, 5) NULL,
[CurrKey] [nchar](5) NULL,
[Amt1_LC] [money] NULL,
[Amt2_LC] [money] NULL,
[Amt3_LC] [money] NULL,
[Amt4_LC] [money] NULL,
[Amt5_LC] [money] NULL,
[Amt6_LC] [money] NULL,
[Amt2_6_LC] [money] NULL,
[Amt2_6_USD] [money] NULL,
[Amt4_3_LC] [money] NULL,
[NetValue_LC] [money] NULL,
[ExRate] [decimal](9, 5) NULL,
[BillTypeID] [nchar](4) NOT NULL,
[InvoiceStatusID] [int] NOT NULL,
[SalesRegionID] [int] NULL,
[Cust_Group] [nchar](20) NULL,
CONSTRAINT [PK_Invoices] PRIMARY KEY CLUSTERED
(
[BillingID] ASC,
[LineID] ASC,
[AccountingID] 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
[OrderID] [nchar](10) NOT NULL,
[LineID] [nchar](6) NOT NULL,
[SoldToID] [nchar](10) NULL,
[DistChanID] [nchar](2) NULL,
[LclChnID] [nchar](3) NULL,
[CompanyID] [nchar](6) NULL,
[SalesOrgID] [nchar](4) NULL,
[DivisionID] [nchar](2) NULL,
[ShipToID] [nchar](10) NULL,
[CurrKey] [nchar](5) NULL,
[OrderType] [nchar](4) NULL,
[SalesDistrict] [nchar](6) NULL,
[MatID] [nchar](18) NULL,
[dtCreatedOn] [date] NULL,
[dtGoodsIssue] [date] NULL,
[ParentID] [nchar](6) NULL,
[ParentMatID] [nchar](18) NULL,
[ParentQty] [decimal](18, 5) NULL,
[Qty] [decimal](13, 2) NULL,
[SalesUnit] [nchar](3) NULL,
[RejectID] [nchar](2) NULL,
[BaseQty] [decimal](17, 3) NULL,
[SalesQty] [decimal](17, 3) NULL,
[EachesQty] [decimal](17, 3) NULL,
[CaseQty] [decimal](17, 3) NULL,
[Amt1_LC] [money] NULL,
[Amt2_LC] [money] NULL,
[Amt3_LC] [money] NULL,
[Amt4_LC] [money] NULL,
[Amt5_LC] [money] NULL,
[Amt6_LC] [money] NULL,
[Amt2_6_LC] [money] NULL,
[Amt2_6_USD] [money] NULL,
[Amt4_3_LC] [money] NULL,
[NetValue_LC] [money] NULL,
[ExRate] [decimal](9, 5) NULL,
[OrderStatus] [nchar](1) NULL,
[ShipStatus] [nchar](1) NULL,
[BillStatus] [nchar](1) NULL,
[GrossWt] [decimal](17, 3) NULL,
[NetWt] [decimal](17, 3) NULL,
[UnitWt] [nchar](3) NULL,
[OrderStatusID] [int] NOT NULL,
[SalesRegionID] [int] NULL,
[Cust_Group] [nchar](20) NULL,
CONSTRAINT [PK_Orders] PRIMARY KEY CLUSTERED
(
[OrderID] ASC,
[LineID] 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