1

Function :

create function .[data_by_date_district](@rev_datetime datetime)
returns table
as
return
(
SELECT 
     ro.Region,[DATETIME],     
      sum(datain) 'datain',
      sum(dataout) 'dataout'
  FROM 
 data.inoutdata cr 
 join 
 structure.site ro  on
 ro.site = substring(replace([siteunqid],'**',''),1,LEN(siteunqid)-1)
 where  [DATETIME]  =@rev_datetime
 group by Region,[DATETIME]
);

Executing this function with any date takes 00:00:00.

Now when I join this with another table it takes 4 sec (this table is 75 rows) :

SELECT 
     Region,[DATETIME],datain,dataout,(cr.datain+cr.dataout) 'total',SP_GEOMETRY,MI_STYLE,MI_PRINX
  FROM 
  data.data_by_date_district(DATEADD(DAY,-1,cast(GETDATE() as DATE)) ) cr
  join 
  datamap.VectorMaps.REGION_BND db
  on db.Name =  cr.Region

The first table has 1,700,000 rows of data which increases daily by 170,000 but other table has just 75 rows. Is there any way i could decrease the query time? The result of function is 75 rows.

2
  • Please attach your execution plan. Commented Sep 30, 2013 at 11:31
  • @Devart Is image good because xml is too huge. Commented Sep 30, 2013 at 11:39

2 Answers 2

3

I think you should use common table expression instead of function to get your result set and replace function by cte in from clause. I hope this helps!

Sign up to request clarification or add additional context in comments.

1 Comment

nice one Pratik CTE indeed helps
2

Have you tried using the body of the function straight in your query (replacing the function call)? Functions are not very good for performance, I think.

Something like that (sorry, formatting isn't best):

SELECT Region,
       [DATETIME],
       datain,
       dataout,
       (cr.datain + cr.dataout) 'total',
       SP_GEOMETRY,
       MI_STYLE,
       MI_PRINX
FROM (
    SELECT ro.Region,
           [DATETIME],
           SUM(datain) 'datain',
           SUM(dataout) 'dataout'
    FROM data.inoutdata cr
    JOIN structure.site ro ON ro.site = SUBSTRING(REPLACE([siteunqid], '**', ''), 1, LEN(siteunqid) - 1)
    WHERE [DATETIME] = DATEADD(DAY, -1, CAST(GETDATE() AS DATE))
    GROUP BY Region,
             [DATETIME]
) cr
JOIN datamap.VectorMaps.REGION_BND db ON db.Name = cr.Region

5 Comments

actually I am joining with this table to receive geometry column from it. I can't directly join it as it gives "Type geometry is not comparable it can't be used in group by clause"
okay that's cool it now takes 1 sec any way to tune it better because it's just 75 rows :( .
also i see that now clustered index seek cost is reduced from 75% to 46% on table inoutdata which is having action on 176133 rows.
Functions are only really good when applied in SELECT clause, I think. Otherwise they impact on performance.
I used CTE and the query ran in 00:00:00. It's fun. I don't know how this works but.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.