Solved

how to make the date wise report query faster

Posted on 2014-04-28
18
2,076 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
ID: 40027403
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
ID: 40027446
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
ID: 40027461
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
ID: 40027468
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
ID: 40027482
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
ID: 40031195
created indexes.still taking same time.
0
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 40033983
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
ID: 40039210
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
Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

 
LVL 30

Expert Comment

by:hnasr
ID: 40039371
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
ID: 40039939
Qlemo,

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

Expert Comment

by:Qlemo
ID: 40040084
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
ID: 40040258
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
ID: 40040268
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
ID: 40040345
No feedback on my comment: http:#a40039371
0
 
LVL 68

Expert Comment

by:Qlemo
ID: 40040358
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
ID: 40040969
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
ID: 40041834
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

910 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

18 Experts available now in Live!

Get 1:1 Help Now