Speeding up my query using sql server 2008.Alternative to cross apply

user9969

I have a function that is performing very slow.I am working a database that I need to migrate data and I have NO control over!. Ideally I would like to use a view directly since this function is called by a view ,but I could only seem to be able to do it by calling a function.

=== A view should return whatever is in the dummytable by orderNo.If an orderNo has a paymenttype of "Interest" than the balance should be interest,if "tax" should be tax

In real life I will have 200000 rows and more,by using cross apply it seems to slow down quite a lot.

Is there a better way to get the data rather than using CrossApply?

Noddy Sample here(data and datatypes are just fictious for semplicity of the example)

CREATE DATABASE DummyDB
GO

use DummyDB
IF object_id(N'DummyTable', 'U') IS NOT NULL
DROP TABLE DummyTable
GO
CREATE TABLE DummyTable
(
Id int,
OrderNo varchar(255),
PaymentType varchar(255),
Credit varchar(255),
Debit varchar(255),
Balance varchar(255)
)
GO
INSERT INTO [dbo].[DummyTable]([Id], [OrderNo], [PaymentType], [Credit], [Debit], [Balance])
SELECT 1, N'200', N'Interest', N'10', N'5', N'5' UNION ALL
SELECT 2, N'201', N'Deposit', N'400', N'30', N'370' UNION ALL
SELECT 3, N'202', N'Tax', N'20', N'10', N'10' UNION ALL
SELECT 4, N'202', N'Tax', N'50', N'10', N'10'



--my sample attempt not performing

    use DummyDB
    select * from DummyTable

    Declare @OrderNo int
    set @OrderNo=202

    SELECT 
    Tax=tx.Tax,
    Interest=tx1.Interest,
    Deposit=tx2.Deposit
    FROM DummyTable T1
    CROSS APPLY(SELECT
               Tax=sum(cast(T2.Balance as money))
               FROM DummyTable T2 
               WHERE T2.OrderNo=@OrderNo
               AND  PaymentType='Tax')as tx         
    CROSS APPLY(select 
               Interest=sum(cast(T2.Balance as money))
               FROM DummyTable T2 
               WHERE T2.OrderNo=@OrderNo
               AND  PaymentType='Interest')as tx1           
    CROSS APPLY(select 
               Deposit=sum(cast(T2.Balance as money))
               FROM DummyTable T2 
               WHERE T2.OrderNo=@OrderNo
               AND  PaymentType='Deposit')as tx2

    WHERE T1.OrderNo=@OrderNo


    Any Suggestion of using something more efficient than cross apply?

    Many thanks
Mikael Eriksson

This will do almost the same as your sample query. It will give you one row with the result where your query will repeat the values for all rows that match @OrderNo.

select sum(case when T1.PaymentType = 'Tax' then cast(T1.Balance as money) else 0 end) as Tax,
       sum(case when T1.PaymentType = 'Interest' then cast(T1.Balance as money) else 0 end) as Interest,
       sum(case when T1.PaymentType = 'Deposit' then cast(T1.Balance as money) else 0 end) as Deposit
from DummyTable as T1
where T1.OrderNo = @OrderNo

BTW, you should make sure that the data type for OrderNo in the table is the same as the variable @OrderNo. It looks like you are dealing with integers so you should change the table. If that is not possible for you then you need to change @OrderNo to varchar(255) if you want to use an index on OrderNo.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Need Help Speeding Up a SQL Server Query

From Dev

Speeding up SQL Server 2008 Standard edition - best practices?

From Dev

Solution for speeding up a slow SELECT DISTINCT query SQL Server

From Dev

Speeding up a SQL query with indexes

From Dev

CROSS APPLY SQL Server query on Hive

From Dev

Speed up SQL Server 2008 Insert Into Query

From Dev

Speeding up a sql query that uses exists

From Dev

Speeding up $or query in pymongo

From Dev

SQL Query Optimization for Cross Apply

From Dev

Speeding up Pandas apply function

From Dev

SQL Server cross apply not working?

From Dev

SQL Server using CROSS APPLY to avoid duplicate records in SELECT statement

From Dev

SQL Server String split using cross apply returning an error

From Dev

Speeding up using itertools

From Dev

Cross-server query does not work in SQL Server 2008R2

From Dev

SQL Server 2008 cross domain MSDTC

From Dev

Need help Speeding up this Query

From Dev

Speeding up a linq entitiy query

From Dev

SQL Server 2008: Query performance using inner join

From Dev

how to CREATE and UPDATE VIEW in SQL Server 2008 using single query

From Dev

Using query export SQL Server 2008 table to Excel Sheet

From Dev

SQL Server 2008: Query performance using inner join

From Dev

Pivot table query using SQL Server 2008 R2

From Dev

Retrieving query data from four tables using SQL Server 2008?

From Dev

Speeding up Visual Studio 2008 with Resharper

From Dev

Recursive query in SQL Server 2008

From Dev

SQL Server 2008 - SELECT query

From Dev

SQL Server alternative to dynamic query

From Dev

Outer apply in sql server makes my query works slowly