Solved

how to make the date wise report query faster

Posted on 2014-04-28
18
2,058 Views
Last Modified: 2014-06-11
Below query displays the datewise report from '2014-04-01' to '2014-04-30'.but query is taking 1 min time to display the report.


i used SQL profiler and also database tuned advisor but its not giving recommendations.
i am able to write big queries but not efficient ones.

i want some tutorials or videos on how to make the queries fast?how to know which condition is taking lot of time and what index needs to created.i have seen execution plan but could not get much out of it.

below conditions are mainly taking lot of time to display the query.how to rewrite it to make it fast?plz note that i created the Non clustered index on EMP_ID in  the EMP_INSTANCES table and included the columns(CREATE_DATE,PURGE_DATE) in that index.

D.[Date] >= CONVERT(VARCHAR(10), EI.CREATE_DATE, 101)  AND
(EI.PURGE_DATE IS NULL OR D.[Date] <= CONVERT(VARCHAR(10), EI.PURGE_DATE, 101))


DECLARE
      @STARTDATE DATETIME ,
      @ENDDATE DATETIME ,
      @CUSTOMERID INTEGER
     
SET @STARTDATE='2014-04-01 00:00'
SET @ENDDATE='2014-04-30 00:00'
SET @CUSTOMERID=1234567
 
;WITH Dates AS
(
    SELECT DATEADD(DAY,number,@STARTDATE) [Date]
    FROM master.dbo.spt_values
    WHERE type = 'P'
    AND number >= 0
    AND DATEADD(DAY,number,@STARTDATE) <= @ENDDATE
)
SELECT D.[Date] AS [DATE], LOC.NAME, sum(A.FILESIZE) FILESIZE
FROM Dates D,
EMP_INSTANCES EI ,
EMP A ,
POLICIES P, 
CUSTOMERS C,
LOCATIONS LOC
WHERE
a.id=ai.EMP_id and
a.policy_id=p.id and
c.id=p.customer_id and
sl.id=ai.location_id and
C.ID = @CUSTOMERID AND
D.[Date] >= CONVERT(VARCHAR(10), EI.CREATE_DATE, 101)  AND
(EI.PURGE_DATE IS NULL OR D.[Date] <= CONVERT(VARCHAR(10), EI.PURGE_DATE, 101))
GROUP BY D.[Date], LOC.NAME

Open in new window

0
Comment
Question by:chaitu chaitu
  • 6
  • 4
  • 3
  • +3
18 Comments
 
LVL 22

Expert Comment

by:plusone3055
Comment Utility
your query looks pretty good, I wouldn't rrally write it a differnt way at this time.
My suggestion would be to index the tables associated with the query to see if that improves performance... Have you tried that yet ?
0
 
LVL 20

Author Comment

by:chaitu chaitu
Comment Utility
i created the Non clustered index on EMP_ID in  the EMP_INSTANCES table and included the columns(CREATE_DATE,PURGE_DATE) in that index.

do you want me to create any other indexes on EMP_INSTANCES table?remaining indexes will be created by defauly if it is a primay key.
0
 
LVL 22

Expert Comment

by:plusone3055
Comment Utility
i would create NC indexes on every table associated with the query to be honest. I am willing to wager that it will cut the time down significantly
0
 
LVL 20

Author Comment

by:chaitu chaitu
Comment Utility
may i know NC indexes on which columns?

you mean  policy_id in emp table
and customer_id  in policies table.


do i missing anything?
0
 
LVL 22

Expert Comment

by:plusone3055
Comment Utility
My fault My communication was poor :)

I would create NC indexes on Every PK in the tables from which you are referencing for your query
0
 
LVL 20

Author Comment

by:chaitu chaitu
Comment Utility
created indexes.still taking same time.
0
 
LVL 20

Author Comment

by:chaitu chaitu
Comment Utility
if i comment this condition output is coming below 30 secs.but if i put this condition its taking more than 2 mins.

D.[Date] >= CONVERT(VARCHAR(10), EI.CREATE_DATE, 101)  AND
0
 
LVL 68

Expert Comment

by:Qlemo
Comment Utility
Why are you doing the CONVERT in the first place? It removes the ability to use an index on CREATE_DATE and PURGE_DATE.
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 30

Expert Comment

by:hnasr
Comment Utility
Try to use joins on related fields.

Leave extra limiting conditions in the where clause.

Check this article on joins.

http://www.experts-exchange.com/Database/MS_Access/A_3597-INNER-JOIN-a-Number-Of-Tables.html

I see you are using 6 tables. Assuming an average of 10 records in each table.
In the current case you are using a cartesian join where each record from table 1 is repeated with every record of table 2, giving 100 records.
The next table will make the set 1000 records. Including all 6 tables, you are dealing with 1,000,000 record set loaded in memory, then applying the logic and conversion.

With joins the records are limited by each join, and a few number of records are in memory to apply the extra logic and conversion.
0
 
LVL 20

Author Comment

by:chaitu chaitu
Comment Utility
Qlemo,

i need to take date only not datetime thats why used CONVERT function.
0
 
LVL 68

Expert Comment

by:Qlemo
Comment Utility
And you can't manage to store dates only in those two fields? CREATE_DATE sounds like there is no time portion.
Otherwise you can create computed columns on both datetime fields to set an index on them.
0
 
LVL 20

Author Comment

by:chaitu chaitu
Comment Utility
but we are time portion as well.what do you mean by computed columns on both datetime fields?
0
 
LVL 68

Expert Comment

by:Qlemo
Comment Utility
You can create "virtual" columns as expressions in a table, and the DBMS will manage their content whenever the column(s) they are based on are changed. In this case you would create one column for convert(CREATE_DATE, 101) and convert(PURGE_DATE, 101).
After that, you can index those new columns. That should speed up search, but introduces higher update/insert costs for maintaining those columns and indexes.
0
 
LVL 30

Expert Comment

by:hnasr
Comment Utility
No feedback on my comment: http:#a40039371
0
 
LVL 68

Expert Comment

by:Qlemo
Comment Utility
hnasr,

If the Query Optimizer does its job well, it doesn't matter for inner joins whether to include conditions in ON or WHERE. However, it is good style to clearly separate join conditions from filter conditions.
For outer joins it is necessary to do so, because that makes a difference.
0
 
LVL 18

Accepted Solution

by:
vasto earned 500 total points
Comment Utility
Convert to Date instead of Varchar

D.[Date] >= CONVERT(Date, EI.CREATE_DATE)  AND
(EI.PURGE_DATE IS NULL OR D.[Date] <= CONVERT(Date, EI.PURGE_DATE))
0
 
LVL 16

Expert Comment

by:DcpKing
Comment Utility
vasto is right - in your comparisons you're comparing a datetime (d.Date) with a varchar ( CONVERT(VARCHAR(10), EI.PURGE_DATE, 101) ). At the very least, convert  ei.PurgeDate into a datetime, not a varchar. If it's already a date then just leave it alone!
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Creating and Managing Databases with phpMyAdmin in cPanel.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now