|
AskSQLTeam
Ask SQLTeam Question
0 Posts |
Posted - 2002-04-26 : 11:04:53
|
| Bruce writes "I have a Linked Server Query which times out under load. Basically, I need to pull everything from the Linked Server database into a temp table, then perform the queries I need on that (I think). The joins across the network are killing this, as eveident from the execution plan and trace. Here is my code, which puts the fields from the Linked Server database tables into a temp table then performs the joins and conditional statements:CREATE PROCEDURE dbo.spELRMCcardXtionByDateTest @dcid nvarchar(255), @startDate datetime, @endDate datetimeAS-- We don't want to return the # of rows inserted-- into our temporary table, so turn NOCOUNT ONSET NOCOUNT ONGOSET ANSI_WARNINGS OFFGOSET ANSI_NULLS OFFGO--Create a temporary tableCREATE TABLE #TempItems( ID int IDENTITY, card_number varchar(10), program_number varchar(9), start_date datetime(8), end_date datetime(8), event_number numeric(17), event_number numeric(17), status int(4), budget money(8), scheduled_date datetime(8), tx_time numeric(17), purchase_amount numeric(17), merchant_name varchar(15), location_number numeric(17), location_number numeric(17))-- Insert the rows from LinkedServer into the temp. tableINSERT INTO #TempItems (card_number, program_number, start_date, end_date, event_number,event_number, status, budget,scheduled_date,tx_time,purchase_amount,merchant_name,location_number,location_number)SELECT card_number, program_number, start_date, end_date, event_number,status,budget,scheduled_date,status FROM LinkServer.MC_Card.webuser.EVENTSELECT event_number, tx_time, purchase_amount, location_number FROM LinkServer.MC_Card.webuser.POS_TX, SELECT merchant_name, location_number FROM LinkServer.MC_Card.webuser.LOCATIONSELECT STORE.[Str#], STORE.[Dcid#], STORE.[Chain], E.card_number, E.program_number, E.start_date, E.end_date, E.event_number, P.event_number, E.status, E.budget, E.scheduled_date, P.tx_time, P.purchase_amount, L.merchant_name, L.location_number, P.location_numberFROM (STORE INNER JOIN STORE.[DemoID#] = E.event_number) LEFT JOIN (L.location_number = P.location_number) ON E.event_number = P.event_number) FROM #TempItemsWHERE (((STORE.[Dcid#])= @dcid)) AND E.scheduled_date BETWEEN @startDate AND @endDateORDER BY E.program_number -- Turn NOCOUNT back OFFSET NOCOUNT OFFWhat I'm concerned about are these lines (not sure how to get all the fields in one select statement because they're from different tables on the Linked Server database:SELECT card_number, program_number, start_date, end_date, event_number,status,budget,scheduled_date,status FROM LinkServer.MC_Card.webuser.EVENTSELECT event_number, tx_time, purchase_amount, location_number FROM LinkServer.MC_Card.webuser.POS_TX, SELECT merchant_name, location_number FROM LinkServer.MC_Card.webuser.LOCATIONSo, basically my question is: AM I GOING ABOUT THIS CORRECTLY? If not, could you please provide an example of how I can correct this, either in theory and/or syntax?Thank you,Bruce " |
|